gpt4 book ai didi

android - 尝试连接 aws iot mqtt 代理时连接丢失

转载 作者:行者123 更新时间:2023-12-05 06:30:34 24 4
gpt4 key购买 nike

这是我的代码

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

当我尝试使用 Android 手机连接 aws iot mqtt 代理时,出现以下错误:

E/com.amazonaws.demo.androidpubsub.PubSubActivity:连接错误。 连接丢失 (32109) - java.io.EOFException 在 org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146) 在 java.lang.Thread.run(Thread.java:818) 引起原因:java.io.EOFException 在 java.io.DataInputStream.readByte(DataInputStream.java:77) 在 org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65) 在 org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107) 在 java.lang.Thread.run(Thread.java:818)

最佳答案

上述异常可能是由于多种原因造成的,例如网络连接丢失、连接或订阅的策略限制等。不幸的是,Mqtt paho 客户端并不总是完美地传播连接异常,因此可能很难找到根本原因这个问题只是来自这个异常(exception)。看来您正在关注 this示例应用程序。我能够按照 README 说明使用该应用程序。以下是我怀疑可能导致此问题的一些要点:

  1. 确保将以下 IAM 策略附加到作为身份池创建的一部分创建的未经身份验证的角色

    { "版本": "2012-10-17", “陈述”: [ { "效果": "允许", “行动”: [ “物联网:AttachPrincipalPolicy”, “物联网:创建 key 和证书” ], “资源”:[ “*” ] } ]

  2. 确保以下物联网政策附加到设备证书

    { "版本": "2012-10-17", “陈述”: [ { "效果": "允许", “行动”:“物联网:连接”, “资源”:“” }, { "效果": "允许", “行动”: [ “物联网:发布”, “物联网:订阅”, “物联网:接收” ], “资源”:“” } ]

希望对您有所帮助!

关于android - 尝试连接 aws iot mqtt 代理时连接丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52270222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com