我目前正在使用 Android Studio 开发一个移动应用程序,该应用程序应该通过 AWS Cognito 进行身份验证,我使用 AWS Amplify 进行验证。Amplify 似乎工作正常,但是当我尝试连接到我的 Greengrass 事物的设备影子时,我从这个问题的标题中得到了错误:
AWSIotMqttManager:onFailure:连接失败。MqttException (0) - java.io.IOException: 已经连接
这很奇怪,因为我还没有调用连接命令,所以我不应该连接。
这是我的代码:
// Here I get the credentials which I later use to connect to AWSIoT
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(), // get the context for the current activity
"*****", // your AWS Account id
"us-east-2:****************", // your identity pool id
"arn:aws:iam::*************-unauthRole",// an authenticated role ARN
"arn:aws:iam::*************-authRole", // an unauthenticated role ARN
Regions.US_EAST_2 //Region
);
//Here I create the AWSIotMqttManager
AWSIotMqttManager awsIotMqttManager = new AWSIotMqttManager(uuidAsString,"endpoint");
//then I connect it
try {
awsIotMqttManager.connect(credentialsProvider, new AWSIotMqttClientStatusCallback() {
@Override
public void onStatusChanged(AWSIotMqttClientStatus status, Throwable throwable) {
if (status == AWSIotMqttClientStatus.Connected) {
awsIotMqttManager.subscribeToTopic("$aws/things/thing/shadow/update/documents", AWSIotMqttQos.QOS0, new AWSIotMqttNewMessageCallback() {
@Override
public void onMessageArrived(String topic, byte[] data) {
System.out.println("TOPIC: " + topic);
System.out.println(data);
}
});
String message = "{ \"state\": {\"desired\": {\"message\": \"Hello from ANDROID SDK\"} } }} }";
awsIotMqttManager.publishString(message,"$aws/things/thing/shadow/update",AWSIotMqttQos.QOS0);
}
});
}
catch (Exception e) {
Log.i("CHECK", e.toString());
}