Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cognito connect to android sample #432

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,11 @@ dependencies {
implementation 'software.amazon.awssdk.crt:android:0.22.1'
}
```
[Android IoT Samples README](./android/app/src/main/assets/README.md)

Replace `0.22.1` in `software.amazon.awssdk.crt:android:0.22.1` with the latest version of the CRT.
Look up the latest CRT version here: https://github.com/awslabs/aws-crt-java/releases

#### Caution
You will need to override and provide a ROOT_CERTIFICATE manually from one of the following [certificates](https://www.amazontrust.com/repository/). For overriding default trust store you can use following [method](https://github.com/aws/aws-iot-device-sdk-java-v2/blob/ed802dce740895bcd3b0b91de30ec49407e34a87/sdk/src/main/java/software/amazon/awssdk/iot/AwsIotMqttConnectionBuilder.java#L151-L160). It's a [known problem](https://github.com/aws/aws-iot-device-sdk-java-v2/issues/157).

## Samples

[Samples README](samples)
Expand Down
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {
java.srcDir '../../samples/BasicPubSub/src/main/java'
java.srcDir '../../samples/Jobs/src/main/java'
java.srcDir '../../samples/Shadow/src/main/java'
java.srcDir '../../samples/CognitoConnect/src/main/java'
java.srcDir 'src/main/java'
}
}
Expand Down
23 changes: 20 additions & 3 deletions android/app/src/main/assets/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
# Android App
Files must be placed in the assets directory
# Required files to run all samples:
[**Return to aws-iot-device-sdk-java-v2 README**](../../../../../../aws-iot-device-sdk-java-v2/README.md)

# Files used by IoT Samples App
Files must be placed in the assets directory for the sample app to connect to IoT Core.
## Required by all samples:
sbSteveK marked this conversation as resolved.
Show resolved Hide resolved
* endpoint.txt - IoT ATS Endpoint
* certificate.pem - IoT Thing Certificate
* privatekey.pem - IoT Thing Private Key

# Optional files:
## Required to run Cognito Client sample:
* cognitoIdentity.txt - Cognito identity ID
* signingRegion.txt - Signing region

## Optional files:
* rootca.pem - override the default system trust store
* clientId.txt - specifies --clientId CLI argument
* topic.txt - specifies --topic CLI argument
* message.txt - specifies --message CLI argument
* port.txt - specifies --port CLI argument
* thingName.txt - specifies --thingName CLI argument

# Links to samples
[**BasicPubSub**](../../../../../samples/BasicPubSub/README.md)

[**Jobs**](../../../../../samples/Jobs/README.md)

[**Shadow**](../../../../../samples/Shadow/README.md)

[**CognitoConnect**](../../../../../samples/CognitoConnect/README.md)
##### NOTE: The shadow sample does not currently complete on android due to its dependence on stdin keyboard input.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import kotlin.concurrent.thread
val SAMPLES = mapOf(
"Publish/Subscribe Sample" to "pubsub.PubSub",
"Jobs Client Sample" to "jobs.JobsSample",
"Shadow Client Sample" to "shadow.ShadowSample"
"Shadow Client Sample" to "shadow.ShadowSample",
"Cognito Client Sample" to "cognitoconnect.CognitoConnect"
)

class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
Expand Down Expand Up @@ -126,9 +127,16 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
resourceNames.add("endpoint.txt")

// Add required files for Samples here
if (name == "pubsub.PubSub" || name == "jobs.JobsSample" || name == "shadow.ShadowSample"){
resourceNames.add("certificate.pem")
resourceNames.add("privatekey.pem")
when(name) {
"pubsub.PubSub", "jobs.JobsSample", "shadow.ShadowSample" -> {
resourceNames.add("certificate.pem")
resourceNames.add("privatekey.pem")
}

"cognitoconnect.CognitoConnect" -> {
resourceNames.add("cognitoIdentity.txt")
resourceNames.add("signingRegion.txt")
}
}

// Copy to cache and get file locations for required files
Expand All @@ -150,12 +158,34 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
}

if(isResourcesFound) {
args.addAll(arrayOf(
"--endpoint", assetContents("endpoint.txt"),
"--cert", resourceMap["certificate.pem"],
"--key", resourceMap["privatekey.pem"],
"--port", assetContentsOr("port.txt", "8883"),
"--client_id", assetContentsOr("clientId.txt", "android-java-crt-test")))
args.addAll(arrayOf("--endpoint", assetContents("endpoint.txt")))

when(name) {
"pubsub.PubSub" -> {
args.addAll(arrayOf(
"--cert", resourceMap["certificate.pem"],
"--key", resourceMap["privatekey.pem"],
"--port", assetContentsOr("port.txt", "8883"),
"--client_id", assetContentsOr("clientId.txt", "android-java-crt-test"),
"--topic", assetContentsOr("topic.txt", "test/topic"),
"--message", assetContentsOr("message.txt", "Hello World From Android")))
}

"jobs.JobsSample", "shadow.ShadowSample" -> {
args.addAll(arrayOf(
"--cert", resourceMap["certificate.pem"],
"--key", resourceMap["privatekey.pem"],
"--port", assetContentsOr("port.txt", "8883"),
"--client_id", assetContentsOr("clientId.txt", "android-java-crt-test"),
"--thing_name", assetContentsOr("thingName.txt", "aws-iot-unit-test")))
}

"cognitoconnect.CognitoConnect" -> {
args.addAll(arrayOf(
"--signing_region", assetContents("signingRegion.txt"),
"--cognito_identity", assetContents("cognitoIdentity.txt")))
}
}

// Check for optional root CA file
try {
Expand All @@ -168,16 +198,6 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
}
} catch (e: Exception) {}

if (name == "pubsub.PubSub") {
args.addAll(arrayOf(
"--topic", assetContentsOr("topic.txt", "test/topic"),
"--message", assetContentsOr("message.txt", "Hello World From Android")))
} else if (name in arrayOf("jobs.JobsSample", "shadow.ShadowSample")) {
args.addAll(arrayOf(
"--thing_name", assetContentsOr("thingName.txt", "aws-iot-unit-test")
))
}

val main = sampleClass?.getMethod("main", Array<String>::class.java)

try {
Expand Down