尝试连接AWS IoT MQTT代理时连接丢失。

3

这是我的代码

try {
            if (AWSIotKeystoreHelper.isKeystorePresent(keystorePath, keystoreName)) {
                if (AWSIotKeystoreHelper.keystoreContainsAlias(certificateId, keystorePath,
                        keystoreName, keystorePassword)) {
                    Log.i(LOG_TAG, "Certificate " + certificateId
                            + " found in keystore - using for MQTT.");
                    // load keystore from file into memory to pass on connection
                    clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,
                            keystorePath, keystoreName, keystorePassword);
                    btnConnect.setEnabled(true);
                    mqttManager.setAutoReconnect(false);
                } else {
                    Log.i(LOG_TAG, "Key/cert " + certificateId + " not found in keystore.");
                }
            } else {
                Log.i(LOG_TAG, "Keystore " + keystorePath + "/" + keystoreName + " not found.");
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "An error occurred retrieving cert/key from keystore.", e);
        }

        if (clientKeyStore == null) {
            Log.i(LOG_TAG, "Cert/key was not found in keystore - creating new key and certificate.");

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // Create a new private key and certificate. This call
                        // creates both on the server and returns them to the
                        // device.
                        CreateKeysAndCertificateRequest createKeysAndCertificateRequest =
                                new CreateKeysAndCertificateRequest();
                        createKeysAndCertificateRequest.setSetAsActive(true);
                        final CreateKeysAndCertificateResult createKeysAndCertificateResult;
                        createKeysAndCertificateResult =
                                mIotAndroidClient.createKeysAndCertificate(createKeysAndCertificateRequest);
                        Log.i(LOG_TAG,
                                "Cert ID: " +
                                        createKeysAndCertificateResult.getCertificateId() +
                                        " created.");

                        // store in keystore for use in MQTT client
                        // saved as alias "default" so a new certificate isn't
                        // generated each run of this application
                        AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certificateId,
                                createKeysAndCertificateResult.getCertificatePem(),
                                createKeysAndCertificateResult.getKeyPair().getPrivateKey(),
                                keystorePath, keystoreName, keystorePassword);


                        // load keystore from file into memory to pass on
                        // connection
                        clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,
                                keystorePath, keystoreName, keystorePassword);


                        // Attach a policy to the newly created certificate.
                        // This flow assumes the policy was already created in
                        // AWS IoT and we are now just attaching it to the
                        // certificate.
                        AttachPrincipalPolicyRequest policyAttachRequest =
                                new AttachPrincipalPolicyRequest();
                        policyAttachRequest.setPolicyName(AWS_IOT_POLICY_NAME);
                        policyAttachRequest.setPrincipal(createKeysAndCertificateResult
                                .getCertificateArn());
                        mIotAndroidClient.attachPrincipalPolicy(policyAttachRequest);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                btnConnect.setEnabled(true);
                            }
                        });
                    } catch (Exception e) {
                        Log.e(LOG_TAG,
                                "Exception occurred when generating new private key and certificate.",
                                e);
                    }
                }
            }).start();
        }
    }

    View.OnClickListener connectClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d(LOG_TAG, "clientId = " + clientId);

            try {
                mqttManager.connect(clientKeyStore, 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);
                                    }
                                    tvStatus.setText("Disconnected");
                                } else {
                                    tvStatus.setText("Disconnected");

                                }
                            }
                        });
                    }
                });
            } catch (final Exception e) {
                Log.e(LOG_TAG, "Connection error.", e);
                tvStatus.setText("Error! " + e.getMessage());
            }
        }
    };

当我尝试使用安卓手机连接到AWS IoT MQTT代理时,我遇到了以下错误: E/com.amazonaws.demo.androidpubsub.PubSubActivity: 连接错误。 连接丢失(32109)- java.io.EOFException at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146) at java.lang.Thread.run(Thread.java:818) 导致原因:java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:77) at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65) at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107) at java.lang.Thread.run(Thread.java:818)
请注意,本文中的HTML标签已被保留。
2个回答

1
上述异常可能是由多种原因引起的,例如网络连接丢失、连接或订阅的策略限制等。不幸的是,Mqtt paho客户端并不总是完美地传播连接异常,因此仅从此异常很难找到问题的根本原因。看起来您正在遵循this示例应用程序。我能够按照README说明使应用程序正常工作。以下是我怀疑可能导致此问题的一些要点:
  1. 确保以下 IAM 策略已附加到作为身份池创建的未经身份验证的角色上:

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

  2. 确保以下 IoT 策略已附加到设备证书上:

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:Connect", "Resource": "" }, { "Effect": "Allow", "Action": [ "iot:Publish", "iot:Subscribe", "iot:Receive" ], "Resource": "" } ] }

希望对您有所帮助!

0
正如已经建议的那样,问题几乎肯定是由于没有附加适当策略所致,但这进一步复杂化了 AWS 示例代码中的错误处理。上面的代码首先在设备密钥库中查找保存的证书,如果找到,则尝试使用它进行连接。如果在预期的密钥库名称下找不到证书,则创建一个新证书,将其保存在密钥库中,然后尝试将原则策略附加到它上面。如果从 AWS 获取策略时出现任何问题,则会在密钥库中留下配置不正确的条目,而 AWS 提供的代码永远无法从中恢复。我花了很多时间尝试不成功的策略实验,直到我实际跟踪代码并意识到无论我指定什么策略都没有关系 - 代码都会忽略它并使用密钥库中配置不正确的策略!
在上面的代码中的 catch 块中添加以下内容
AWSIotKeystoreHelper.deleteKeystoreAlias(certificateId,
                            keystorePath, keystoreName, keystorePassword);

这将确保没有配置不正确的证书挂在那里绊倒您。

就初始错误而言,问题可能是您指定的策略名称和上下文。我在IAM中创建了策略,但它们没有被识别 - 您实际上需要按照Roshan上面所述的方式在IoT Core Security的上下文中创建IoT策略,并在PubSub示例代码中指定其名称。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接