订阅Android中的IOT主题

时间:2019-05-09 11:01:21

标签: android amazon-web-services iot

我正在尝试通过我的Android应用程序订阅主题。即使连接成功,订阅也会失败。一旦执行订阅呼叫,IOT连接就会失败,并给出如下所述的错误日志。是否想知道我在哪里编码有误? 物联网策略的所有资源都是出于测试目的而提供的。

{
  "Version": "2012-10-17",
  "Statement": [
   {
      "Effect": "Allow",
      "Action": "iot:*",
      "Resource": "*"
   }
 ]
}

我将AWS-amplify用于我的开发。 https://aws-amplify.github.io/docs/android/pubsub

1。连接到物联网

    private var mAWSMobileClient : AWSMobileClient = AWSMobileClient.getInstance()
    private lateinit var mIotDataManager: AWSIotMqttManager
    private lateinit var mAttachedPolicyReq: AttachPolicyRequest
    private lateinit var mAwsIOTClient: AWSIotClient
    private lateinit var mAwsCredentials: AWSCredentials

 private fun connectToIOT() {
        Thread(Runnable {
            var mDeviceIdentity = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)

            mIotDataManager = AWSIotMqttManager(mDeviceIdentity, <iot endpoint>)
            mIotDataManager.keepAlive = 50
            mIotDataManager.isAutoReconnect = false
            mIotDataManager.connectionStabilityTime = 100

            mAwsCredentials = mAWSMobileClient.awsCredentials
            mAwsIOTClient = AWSIotClient(mAWSMobileClient)
            mAwsIOTClient.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_2))

            mAttachedPolicyReq = AttachPolicyRequest()
            mAttachedPolicyReq.policyName = "test_policy"
            mAttachedPolicyReq.target = mAWSMobileClient.identityId
            mAwsIOTClient.attachPolicy(mAttachedPolicyReq)

            try {
                mIotDataManager.connect(mAWSMobileClient, object : AWSIotMqttClientStatusCallback {
                    override fun onStatusChanged(
                        status: AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus?,
                        throwable: Throwable?
                    ) {
                        when (status) {
                            AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.ConnectionLost -> {}

                            AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected -> {}

                            AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting -> {}

                            AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Reconnecting -> {}

                        else -> {

                        }
                    }
                }
            })
        } catch (e: Exception) {
            Log.d("IOT Data Manager Connection Errror : $e")
        }
    }).start()
}
  1. 订阅方法

    fun subscribeToThing() {
            if(mConnected) {
                Thread(Runnable {
                    mThingsName = "$mThingsName/shadow/get/accepted"
                    var message: String? = null
                    try {
                        mIotDataManager.subscribeToTopic(
                            mThingsName,
                            AWSIotMqttQos.QOS1,
                            object : AWSIotMqttNewMessageCallback {
                                override fun onMessageArrived(topic: String?, data: ByteArray?) {
    
                                    try {
                                        message = String(data!!, Charsets.UTF_8)
                                    } catch (e: UnsupportedEncodingException) {
                                        Log.d("Unsupported Encoding error :$e")
                                    }
                                }
                            })
                    } catch (e: Exception) {
                        Log.d("Subscription error :$e")
                    }
            }).start()
        } else {
            Log.d("IOT Not Connected")
            }
    }
    

结果日志:

  

W / AWSIotMqttManager:连接丢失

     

订阅错误:com.amazonaws.AmazonClientException:客户端错误   订阅时。

1 个答案:

答案 0 :(得分:0)

我看到您正在使用UPDATE `gallery_items` SET position = -222 + (@shuffle_rank := @shuffle_rank + 1) WHERE `gallery_items`.`gallery_id` = 24669422 AND (position >= -222) AND ((SELECT @shuffle_rank := 0) = 0) ORDER BY position ASC LIMIT 3 。也许您没有在使用IOT客户端之前初始化移动客户端实例?以下documentation概述了详细信息。