Android 尝试连接aws iot mqtt代理时连接丢失
这是我的密码Android 尝试连接aws iot mqtt代理时连接丢失,android,amazon-web-services,android-studio,mqtt,aws-iot,Android,Amazon Web Services,Android Studio,Mqtt,Aws Iot,这是我的密码 try { if (AWSIotKeystoreHelper.isKeystorePresent(keystorePath, keystoreName)) { if (AWSIotKeystoreHelper.keystoreContainsAlias(certificateId, keystorePath, keystoreName, keystorePassword)) {
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 broker时,出现如下错误:
E/com.amazonaws.demo.androidsubsubsubsub.pubsub活动:连接错误。
连接丢失(32109)-java.io.EOFException
位于org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
运行(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客户端并不总是完美地传播连接异常,因此可能很难仅从该异常中找出问题的根本原因。看起来您正在使用示例应用程序。我能够让应用程序按照自述说明运行。以下是我怀疑可能导致此问题的一些要点:
希望有帮助 如前所述,问题几乎肯定是由于没有附加适当的策略,但由于AWS示例代码中错误处理不当,这一问题更加复杂。上面的代码首先在设备密钥库中查找已保存的证书,如果找到,则尝试使用它进行连接。如果找不到预期密钥库名称下的证书,则会创建一个新证书,将其保存在密钥库中,然后尝试将原则策略附加到该证书。如果从AWS获取策略时出错,则密钥库中会留下一个配置不正确的条目,AWS提供的代码永远无法从中恢复。我花了很多时间尝试策略,但都没有成功,直到我实际跟踪代码并意识到它与我指定的策略没有任何区别-代码忽略了它,并在密钥库中使用了配置不正确的策略 在上面代码的catch块中,添加以下内容
AWSIotKeystoreHelper.deleteKeystoreAlias(certificateId,
keystorePath, keystoreName, keystorePassword);
这将确保不会有错误配置的证书挂起绊倒您
就初始错误而言,问题可能是您指定的策略的名称和上下文。我在IAM中创建了策略,但这些策略未被识别-实际上,您需要按照Roshan在IoT核心安全上下文中所述创建IoT策略,并在PubSub示例代码中指定您给它的名称