-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added a web socket sample - improved readme and other misc fixes
- Loading branch information
AWS
committed
May 16, 2016
1 parent
af7642c
commit 9632580
Showing
19 changed files
with
984 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.amazonaws.demo.androidpubsubwebsocket" | ||
android:versionCode="1" | ||
android:versionName="1.0" > | ||
|
||
<uses-sdk | ||
android:minSdkVersion="10" | ||
android:targetSdkVersion="10" /> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@drawable/ic_launcher" | ||
android:label="@string/app_name" > | ||
<activity android:name="com.amazonaws.demo.androidpubsubwebsocket.PubSubActivity" > | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Android PubSub with WebSockets Sample | ||
|
||
This sample demonstrates use of the AWS IoT APIs to securely publish to and subscribe from MQTT topics with a WebSocket. Authentication of the WebSocket connection is done with Amazon Cognito. Once a connection to the AWS IoT platform has been established, the application presents a simple UI to publish and subscribe over MQTT. | ||
|
||
## Requirements | ||
|
||
* AndroidStudio or Eclipse | ||
* Android API 10 or greater | ||
|
||
## Using the Sample | ||
|
||
1. Import the AndroidPubSubWebSocket project into your IDE. | ||
- If you are using Android Studio: | ||
* From the Welcome screen, click on "Import project". | ||
* Browse to the AndroidPubSubWebSocket directory and press OK. | ||
* Accept the messages about adding Gradle to the project. | ||
* If the SDK reports some missing Android SDK packages (like Build Tools or the Android API package), follow the instructions to install them. | ||
- If you are using Eclipse: | ||
* Go to File -> Import. Import Wizard will open. | ||
* Select General -> Existing Projects into Workspace. Click Next. | ||
* In Select root directory, browse to the samples directory. | ||
* Select the AndroidPubSubWebSocket project to import. | ||
* Click Finish. | ||
|
||
1. Import the libraries : | ||
- If you use Android Studio, Gradle will take care of downloading these dependencies for you. | ||
- If you use Eclipse, you will need to download the AWS SDK for Android (http://aws.amazon.com/mobile/sdk/) and extract and copy these jars into the 'libs' directory for the project: | ||
* aws-android-sdk-core-X.X.X.jar | ||
* aws-android-sdk-iot-X.X.X.jar | ||
|
||
**Note**: Adding support for MQTT over WebSocket connections is only available in the Paho project nightly builds. The Gradle file is currently configured to add the Paho snapshot repository. This project will be updated to the official build after release as part of [Eclipse Neon](https://projects.eclipse.org/projects/technology.paho/releases/1.2.0). | ||
|
||
1. This sample requires Cognito to authorize to AWS IoT and establish a WebSocket connection. Use Amazon Cognito to create a new identity pool: | ||
1. In the [Amazon Cognito Console](https://console.aws.amazon.com/cognito/), select`Create Identity Pool`. | ||
1. Ensure`Enable access to unauthenticated identities` is checked. This allows the sample application to assume the unauthenticated role associated with this identity pool. | ||
|
||
**Important**: see note below on unauthenticated user access. | ||
|
||
1. Obtain the `PoolID` constant. This will be used in the application. | ||
1. As part of creating the identity pool Cognito will setup two roles in [Identity and Access Management (IAM)](https://console.aws.amazon.com/iam/home#roles). These will be named something similar to:`Cognito_PoolNameAuth_Role` and`Cognito_PoolNameUnauth_Role`. | ||
1. Now we will attach a policy to the unauthenticated role which has permissions to access the required AWS IoT APIs. This is done by first creating an IAM Policy in the [IAM Console](https://console.aws.amazon.com/iam/home#policies) and then attaching it to the unauthenticated role. Below is an example policy which can be used with the sample application. This policy allows any client ID to connect and allows publishing, subscribing and receiving messages on the topic:`mytopic/mysubtopic`. | ||
|
||
``` | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"iot:Connect" | ||
], | ||
"Resource": [ | ||
"*" | ||
] | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"iot:Publish", | ||
"iot:Subscribe", | ||
"iot:Receive" | ||
], | ||
"Resource": [ | ||
"arn:aws:iot:<REGION>:<ACCOUNT ID>:topic/mytopic/mysubtopic" | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
More information on AWS IAM roles and policies can be found [here](http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage.html). More information on AWS IoT policies can be found [here](http://docs.aws.amazon.com/iot/latest/developerguide/authorization.html). | ||
|
||
**Note**: to keep this example simple it makes use of unauthenticated users in the identity pool. This can be used for getting started and prototypes but unauthenticated users should typically only be given read-only permissions if used in production applications. More information on Cognito identity pools including the Cognito developer guide can be found [here](http://aws.amazon.com/cognito/). | ||
|
||
1. Open the AndroidPubSubWebSocket project. | ||
|
||
1. Open `PubSubActivity.java` and update the following constants with the appropriate values: | ||
|
||
``` | ||
CUSTOMER_SPECIFIC_ENDPOINT = "<CHANGE_ME>"; | ||
COGNITO_POOL_ID = "<CHANGE_ME>"; | ||
MY_REGION = Regions.US_EAST_1; | ||
``` | ||
|
||
1. Build and run the sample app. | ||
|
||
1. The sample application will allow you to connect to the AWS IoT platform, and then publish or subscribe to a topic using MQTT. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
repositories { | ||
jcenter() | ||
maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots" } | ||
} | ||
|
||
buildscript { | ||
repositories { | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:1.5.0' | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.application' | ||
|
||
dependencies { | ||
compile 'com.amazonaws:aws-android-sdk-iot:2.2.+' | ||
} | ||
|
||
android { | ||
buildToolsVersion "23.0.2" | ||
compileSdkVersion 23 | ||
|
||
defaultConfig { | ||
versionCode 1 | ||
versionName "1.0.0" | ||
minSdkVersion 10 | ||
targetSdkVersion 23 | ||
} | ||
|
||
lintOptions { | ||
abortOnError false | ||
} | ||
|
||
sourceSets { | ||
main { | ||
manifest.srcFile 'AndroidManifest.xml' | ||
java.srcDirs = ['src'] | ||
resources.srcDirs = ['src'] | ||
aidl.srcDirs = ['src'] | ||
renderscript.srcDirs = ['src'] | ||
res.srcDirs = ['res'] | ||
assets.srcDirs = ['assets'] | ||
} | ||
} | ||
} |
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
AndroidPubSubWebSocket/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#Tue Mar 01 13:56:18 PST 2016 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip |
Oops, something went wrong.