2017-03-10 2 views
0

Ich versuche, eine MQTT Nachricht mit AWSIotMqttManager mit Amazon Web Services zu veröffentlichen IOT auf einem Android App zu veröffentlichen, ich habe zu meinem Code this Beispiel als Basis verwendet folgen. Die App sagt, dass sie sich erfolgreich mit dem Gerät verbinden kann, aber keine Nachricht veröffentlicht. Was ist hier falsch?Android verwenden Um MQTT Nachrichten auf AWS IoT Dienstleistungen

// 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); 
       } 
      } 
     }); 

Die Log:

/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

Konnten Sie das beheben? Ich bin gestern selbst über dieses Problem gestolpert. Es wird manchmal "connected" angezeigt und bewegt sich nicht weiter oder protokolliert manchmal "Reconnecting". – RamithDR

Antwort

0

Dank für die Verwendung von AWS IoT Device SDK. Können Sie Ihre Anmeldeinformationen verwenden, um das AndroidPubSubSample-Programm auszuführen, und können Sie Nachrichten erfolgreich abonnieren und veröffentlichen? So stellen Sie sicher, dass Sie die richtigen Anmeldeinformationen eingerichtet haben, z. Korrekte Richtlinien und Berechtigungen angehängt.

Zusätzlich können Sie Lifecycle-Ereignisse "$ aws/events/#" abonnieren, um das Protokoll Ihres Abonnements zu sehen und zu veröffentlichen. Ich konnte Ihre JSON-Daten erfolgreich in $ aws/sachen/../ update-Themen veröffentlichen und wurde von $ aws.things /.../ update/accepted durch hardcoding topic und message akzeptiert.

Wenn Sie weitere Fragen und Probleme bei der Verwendung des AWS IoT Device SDK haben, können Sie ein Problem im Github-Repository lösen. Für Android SDK speziell können Sie gehen zu https://github.com/aws/aws-sdk-android

0

Ich glaube, der Fehler liegt daran, dass Sie ein Thema nicht abonnieren konnten. Subscribe verwendet "topicfilter" in der IAM-Richtlinienanweisung, während "Publish and Receive" "topic" verwendet. Die Richtlinie im Beispiel ist falsch. Die funktionierende IAM-Richtlinie lautet wie folgt:

{ 
    "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/*" 
      ] 
     } 
    ] 
} 
Verwandte Themen