适用于Android的MQTT AWS通过异常``已连接''

时间:2019-04-02 09:53:05

标签: android exception aws-sdk mqtt iot

我正在尝试在我的android应用程序中实现mqtt aws iot。我正在尝试一个示例应用程序,在其中放置了诸如 CognitoPoolID Endpoint Region 之类的必需值。这是我的示例代码

初始化

 clientId = UUID.randomUUID().toString();
    tvClientId.setText(clientId);

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


**//MQTTManager instance creation**
mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
    mqttManager.setAutoReconnect(true);
    mqttManager.setKeepAlive(10);

建立连接

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

我得到这样的状态值

LogMainActivity: Status = Connecting
LogMainActivity: Status = Reconnecting
LogMainActivity: Connection error.
    MqttException (0) - java.io.IOException: Already connected
        at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
        at java.lang.Thread.run(Thread.java:784)
     Caused by: java.io.IOException: Already connected
        at java.io.PipedOutputStream.connect(PipedOutputStream.java:100)
        at java.io.PipedInputStream.connect(PipedInputStream.java:191)
        at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketReceiver.<init>(WebSocketReceiver.java:42)
        at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:78)
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
        at java.lang.Thread.run(Thread.java:784) 

我对使用aws服务非常陌生,因此只需对此进行一点故障排除,如果有人遇到此类问题,请提供解决方案的指南。谢谢

0 个答案:

没有答案