Working with the API

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

Establish Connection

Certificate based mutual authentication

To connect with the AWS IoT Core service on the standard MQTT port 8883, you can use the connect API as shown below.

1mqttManager.connect(<YOUR_KEYSTORE>, new AWSIotMqttClientStatusCallback() {
2 @Override
3 public void onStatusChanged(final AWSIotMqttClientStatus status,
4 final Throwable throwable) {
5 Log.d(LOG_TAG, "Status = " + String.valueOf(status));
6 }
7});

The AWS IoT Core service also allows you to connect devices using MQTT with certificate based mutual authentication on port 443. You can do this using the connectUsingALPN API as shown below. See MQTT with TLS client authentication on port 443 for more information.

1mqttManager.connectUsingALPN(<YOUR_KEYSTORE>, new AWSIotMqttClientStatusCallback() {
2 @Override
3 public void onStatusChanged(final AWSIotMqttClientStatus status,
4 final Throwable throwable) {
5 Log.d(LOG_TAG, "Status = " + String.valueOf(status));
6 }
7});

You can take a look at the API Reference to get more information.

AWS Credentials based Authentication

This method uses AWS Signature Version 4 Credentials to sign the request to connect to the AWS IoT endpoint.

1try {
2 mqttManager.connect(AWSMobileClient.getInstance(), new AWSIotMqttClientStatusCallback() {
3 @Override
4 public void onStatusChanged(final AWSIotMqttClientStatus status, final Throwable throwable) {
5 Log.d(LOG_TAG, "Connection Status: " + String.valueOf(status));
6 }
7 });
8} catch (final Exception e) {
9 Log.e(LOG_TAG, "Connection error: ", e);
10}

Custom Authentication

AWS IoT allows you to define custom authorizers that allow you to manage your own authentication and authorization strategy using a custom authentication service and a Lambda function. Custom authorizers allow AWS IoT to authenticate your devices and authorize operations using bearer token authentication and authorization strategies. See AWS IoT Custom Authentication for more details.

Please follow the steps outlined in Setting up Custom Authentication to create the custom authorizer and configure the workflow with AWS IoT.

Once the custom authorizer workflow is configured, you can establish a connection as follows:

1try {
2 String tokenKeyName = <TOKEN_KEY_NAME>;
3 String token = <TOKEN>;
4 String tokenSignature = <TOKEN_SIGNATURE>;
5 String customAuthorizerName = <AUTHORIZER_NAME>;
6 mqttManager.connect(
7 tokenKeyName,
8 token,
9 tokenSignature,
10 customAuthorizerName,
11 new AWSIotMqttClientStatusCallback() {
12 @Override
13 public void onStatusChanged(final AWSIotMqttClientStatus status,
14 final Throwable throwable) {
15 Log.d(LOG_TAG, "Status = " + String.valueOf(status));
16 runOnUiThread(new Runnable() {
17 @Override
18 public void run() {
19 if (throwable != null) {
20 Log.e(LOG_TAG, "Connection error.", throwable);
21 }
22 }
23 });
24 }
25 });
26} catch (final Exception e) {
27 Log.e(LOG_TAG, "Connection error.", e);
28}

This feature is available in the AWS SDK for Android starting from version 2.16.8.

Subscribe to a topic

In order to start receiving messages from your provider, you need to subscribe to a topic as follows;

1try {
2 mqttManager.subscribeToTopic("myTopic", AWSIotMqttQos.QOS0 /* Quality of Service */,
3 new AWSIotMqttNewMessageCallback() {
4 @Override
5 public void onMessageArrived(final String topic, final byte[] data) {
6 try {
7 String message = new String(data, "UTF-8");
8 Log.d(LOG_TAG, "Message received: " + message);
9 } catch (UnsupportedEncodingException e) {
10 Log.e(LOG_TAG, "Message encoding error: ", e);
11 }
12 }
13 });
14} catch (Exception e) {
15 Log.e(LOG_TAG, "Subscription error: ", e);
16}

Subscribe to multiple topics

To subscribe for multiple topics, just call subscribeToTopic() for each topic you wish to subscribe.

Publish to a topic

To send a message to a topic, use publishString() method with your topic name and the message:

1try {
2 mqttManager.publishString("Hello to all subscribers!", "myTopic", AWSIotMqttQos.QOS0);
3} catch (Exception e) {
4 Log.e(LOG_TAG, "Publish error: ", e);
5}

Unsubscribe from a topic

To stop receiving messages from a topic, you can use unsubscribeTopic() method:

1try {
2 mqttManager.unsubscribeTopic("myTopic");
3} catch (Exception e) {
4 Log.e(LOG_TAG, "Unsubscribe error: ", e);
5}
6
7// You will no longer get messages for "myTopic"

Close Connection

In order to disconnect, you need to close the connection as follows:

1try {
2 mqttManager.disconnect();
3} catch (Exception e) {
4 Log.e(LOG_TAG, "Disconnect error: ", e);
5}

API Reference

For the complete API documentation for AWS IoT, visit our API reference.