gpt4 book ai didi

java - 使用 Android 在 AWS IoT 服务上发布 MQTT 消息

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:47 29 4
gpt4 key购买 nike

我正在尝试在 Android 应用程序上使用 AWSIotMqttManager 与亚马逊 Web 服务 IoT 发布 mqtt 消息,我已使用 this示例作为我的代码的基础。应用程序显示可以成功连接到设备,但无法实际发布消息,这里出了什么问题?

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

//intialize unnqique clientid as client to iot aws
Long tsLong = System.currentTimeMillis()/1000;
clientId = tsLong.toString();
// MQTT Client
mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
// The following block uses a Cognito credentials provider for authentication with AWS IoT.
new Thread(new Runnable() {
@Override
public void run() {
awsCredentials = credentialsProvider.getCredentials();

runOnUiThread(new Runnable() {
@Override
public void run() {
bttnConnect.setEnabled(true);
Toast.makeText(WelcomePageActivity.this, "credentials ok?", Toast.LENGTH_SHORT).show();
}
});
}
}).start();
//connection button onclick lisetner will connect to the mqtt protocol
bttnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG_TAG", "clientId = " + clientId);
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());
}
}
});
//publisj button
ledbutton.setOnClickListener(new View.OnClickListener() {
final String topic = "$aws/things/industech/shadow/update";
final String msg = "{\"state\": {\"desired\": {\"ledBarStatus\": 1},\"reported\": {\"temperature\": 25,\"ledBarStatus\":1}}}";

@Override
public void onClick(View v) {
try {
mqttManager.publishString(msg, topic, AWSIotMqttQos.QOS1);
} catch (Exception e) {
Log.e("LOG_TAG", "Publish error.", e);
}
}
});

日志:

/CognitoCachingCredentialsProvider: Loading credentials from SharedPreferences

D/CognitoCachingCredentialsProvider: Saving credentials to SharedPreferences

D/LOG_TAG: clientId = 1489081527

D/LOG_TAG: Status = Connecting

D/LOG_TAG: Status = Connected

D/LOG_TAG: Status = Reconnecting

D/LOG_TAG: Status = Connected

最佳答案

感谢您使用 AWS IoT 设备开发工具包。您是否能够使用您的凭据来运行 AndroidPubSubSample 程序,并且您是否能够成功订阅和发布消息?这是为了确保您拥有正确的凭据设置,例如附加正确的政策和权限。

此外,您还可以订阅生命周期事件“$aws/events/#”来查看您的订阅和发布日志。我可以成功将您的 JSON 数据发布到 $aws/things/../update 主题,并通过硬编码主题和消息在 $aws.things/.../update/accepted 上被接受。

如果您对使用 AWS IoT 设备 SDK 有更多疑问和问题,请随时在 github 存储库上提出问题。具体针对Android SDK,您可以访问https://github.com/aws/aws-sdk-android

关于java - 使用 Android 在 AWS IoT 服务上发布 MQTT 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42722167/

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