2017-03-10 58 views
0

我試圖使用發佈AWSIotMqttManager與IOT上的Android應用Amazon Web Services的一個MQTT消息,我都跟着用this例如爲基地,以我的代碼。該應用程序表示,它可以成功連接到設備,但無法真正發佈消息,這裏有什麼問題?使用的Android要發佈的消息MQTT AWS上的物聯網服務

// Initialize the AWS Cognito credentials provider 
     credentialsProvider = new CognitoCachingCredentialsProvider(
       getApplicationContext(), // context 
       COGNITO_POOL_ID, // Identity Pool ID 
       MY_REGION // Region 
     ); 
     Region region = Region.getRegion(MY_REGION); 

     //intialize unnqique clientid as client to iot aws 
     Long tsLong = System.currentTimeMillis()/1000; 
     clientId = tsLong.toString(); 
// MQTT Client 
     mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT); 
// The following block uses a Cognito credentials provider for authentication with AWS IoT. 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       awsCredentials = credentialsProvider.getCredentials(); 

       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         bttnConnect.setEnabled(true); 
         Toast.makeText(WelcomePageActivity.this, "credentials ok?", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 
     }).start(); 
//connection button onclick lisetner will connect to the mqtt protocol 
     bttnConnect.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("LOG_TAG", "clientId = " + clientId); 
       try { 
        mqttManager.connect(credentialsProvider, new AWSIotMqttClientStatusCallback() { 
         @Override 
         public void onStatusChanged(final AWSIotMqttClientStatus status, 
                final Throwable throwable) { 
          Log.d("LOG_TAG", "Status = " + String.valueOf(status)); 

          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            if (status == AWSIotMqttClientStatus.Connecting) { 
             tvStatus.setText("Connecting..."); 

            } else if (status == AWSIotMqttClientStatus.Connected) { 
             tvStatus.setText("Connected"); 

            } else if (status == AWSIotMqttClientStatus.Reconnecting) { 
             if (throwable != null) { 
              Log.e("LOG_TAG", "Connection error.", throwable); 
             } 
             tvStatus.setText("Reconnecting"); 
            } else if (status == AWSIotMqttClientStatus.ConnectionLost) { 
             if (throwable != null) { 
              Log.e("LOG_TAG", "Connection error.", throwable); 
              throwable.printStackTrace(); 
             } 
             tvStatus.setText("Disconnected"); 
            } else { 
             tvStatus.setText("Disconnected"); 

            } 
           } 
          }); 
         } 
        }); 

       } catch (final Exception e) { 
        Log.e("LOG_TAG", "Connection error.", e); 
        tvStatus.setText("Error! " + e.getMessage()); 
       } 
      } 
     }); 
     //publisj button 
     ledbutton.setOnClickListener(new View.OnClickListener() { 
      final String topic = "$aws/things/industech/shadow/update"; 
      final String msg = "{\"state\": {\"desired\": {\"ledBarStatus\": 1},\"reported\": {\"temperature\": 25,\"ledBarStatus\":1}}}"; 

      @Override 
      public void onClick(View v) { 
       try { 
        mqttManager.publishString(msg, topic, AWSIotMqttQos.QOS1); 
       } catch (Exception e) { 
        Log.e("LOG_TAG", "Publish error.", e); 
       } 
      } 
     }); 

日誌:

/CognitoCachingCredentialsProvider: Loading credentials from SharedPreferences 

D/CognitoCachingCredentialsProvider: Saving credentials to SharedPreferences 

D/LOG_TAG: clientId = 1489081527 

D/LOG_TAG: Status = Connecting 

D/LOG_TAG: Status = Connected 

D/LOG_TAG: Status = Reconnecting 

D/LOG_TAG: Status = Connected 
+0

您能解決這個問題嗎?我昨天自己偶然發現了這個問題。它有時會說「已連接」,不會繼續前進,有時會繼續記錄「重新連接」。 – RamithDR

回答

0

感謝您使用AWS物聯網設備SDK。您是否可以使用您的憑據來運行AndroidPubSubSample程序,並且能夠成功訂閱和發佈消息?這是爲了確保您擁有正確的憑據設置,例如正確的策略和權限。

此外,您可以訂閱生命週期事件「$ aws/events /#」以查看訂閱和發佈的日誌。我可以將您的JSON數據成功發佈到$ aws/things /../更新主題,並在$ aws.things /.../ update /被硬編碼主題和消息接受。

如果您對使用AWS IoT Device SDK有更多疑問或問題,請隨時在github資源庫中打開一個問題。對於Android SDK專門,你可以去https://github.com/aws/aws-sdk-android

0

我相信這個錯誤是因爲你無法訂閱一個主題。訂閱在IAM策略聲明中使用「topicfilter」,而「發佈和接收」使用「topic」。示例中的策略不正確。工作IAM政策如下:

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
     { 
      "Effect": "Allow", 
      "Action": [ 
       "iot:Connect" 
      ], 
      "Resource": [ 
       "*" 
      ] 
     }, 
     { 
      "Effect": "Allow", 
      "Action": [ 
       "iot:Publish", 
       "iot:Receive" 
      ], 
      "Resource": [ 
       "arn:aws:iot:<your region>:<youracc>:topic/whatevertopic/*" 
      ] 
     }, 
     { 
      "Effect": "Allow", 
      "Action": [ 
       "iot:Subscribe" 
      ], 
      "Resource": [ 
       "arn:aws:iot:<your region>:<youracc>:topicfilter/whatevertopic/*" 
      ] 
     } 
    ] 
} 
相關問題