【发布时间】:2020-03-28 19:43:40
【问题描述】:
我试图从https://github.com/awslabs/aws-sdk-android-samples/tree/master/AndroidPubSub 测试aws-sdk-android-samples/AndroidPubSub/,但在我点击连接后,总是收到错误消息
a2k94wsqkar4rm-ats.iot.us-west-2.amazonaws.com/52.13.183.162(端口 8883)30000 毫秒后:isConnected 失败:ECONNREFUSED(连接 拒绝)
我在 AWS IoT 控制台上创建了我的设备证书并激活了它,还附加了如下所述的策略,
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Connect",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Subscribe",
"iot:Receive"
],
"Resource": "*"
}
]
}
我尝试了两种方法将证书和密钥安装到本地密钥存储,并且它们都有效。
方法一,通过源码
AssetManager assetManager = getAssets();
// read cert and private key from pem file
InputStream input = assetManager.open("4ed2c76117-private.pem");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
String privateKeyPem = new String(buffer);
System.out.println(privateKeyPem);
input = assetManager.open("4ed2c76117-certificate.pem");
size = input.available();
byte[] buffer2 = new byte[size];
input.read(buffer2);
input.close();
String certPem = new String(buffer2);
System.out.println(certPem);
// 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,
certPem,
privateKeyPem,
keystorePath, keystoreName, keystorePassword);
// load keystore from file into memory to pass on
// connection
clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,
keystorePath, keystoreName, keystorePassword);
方法二,通过终端命令,
openssl pkcs12 -export -out iot_keystore.p12 -inkey 4ed2c76117-private.pem -in 4ed2c76117-certificate.pem -name default
keytool -importkeystore -srckeystore iot_keystore.p12 -srcstoretype pkcs12 -destkeystore iot_keystore.bks -deststoretype bks --provider org.bouncycastle.jce.provider.BouncyCastleProvider -–providerpath bcprov-jdk15on-164.jar
adb push iot_keystore.bks /data/user/0/com.amazonaws.demo.androidpubsub/files/iot_keystore
谁能帮忙解决这个问题?
【问题讨论】:
标签: android aws-sdk mqtt aws-iot