Microsoft Azure IoTHub Serials 2 - 如何为android应用添加IoTHub支持
2023-09-11 14:17:02 时间
1. 在build.gradle(app)文件的dependencies中添加对以下项的依赖:
'com.microsoft.azure.sdk.iot:iot-device-client:1.5.37'
其中后面的1.5.37是版本号,最新的版本可以到这里查:
http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.microsoft.azure.sdk.iot%22
2. 在build.gradle(app)文件的packagingOptions中添加如下内容
packagingOptions { exclude "META-INF/MSFTSIG.SF" exclude "META-INF/MSFTSIG.RSA" exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'thirdpartynotice.txt' }
如下图所示。
3. 在需要与IoTHub交互的java文件中,引入如下package
//Azure IoTHub packages import com.microsoft.azure.sdk.iot.device.DeviceClient; import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol; import com.microsoft.azure.sdk.iot.device.IotHubEventCallback; import com.microsoft.azure.sdk.iot.device.IotHubMessageResult; import com.microsoft.azure.sdk.iot.device.IotHubStatusCode; import com.microsoft.azure.sdk.iot.device.Message;
4. 添加如下全局的连接字符串
//Azure IoTHub private final String connString = "HostName=************.azure-devices.net;DeviceId=***********;SharedAccessKey=*********************"; private final String deviceId = "*****************";
5. 在java文件中添加如下3个类(MessageCallbackMqtt、EventCallback、MessageCallback和Counter)的声明
// Our MQTT doesn't support abandon/reject, so we will only display the messaged received // from IoTHub and return COMPLETE static class MessageCallbackMqtt implements com.microsoft.azure.sdk.iot.device.MessageCallback { public IotHubMessageResult execute(Message msg, Object context) { Counter counter = (Counter) context; System.out.println( "Received message " + counter.toString() + " with content: " + new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET)); counter.increment(); return IotHubMessageResult.COMPLETE; } } static class EventCallback implements IotHubEventCallback { public void execute(IotHubStatusCode status, Object context) { Integer i = (Integer) context; System.out.println("IoT Hub responded to message " + i.toString() + " with status " + status.name()); } } static class MessageCallback implements com.microsoft.azure.sdk.iot.device.MessageCallback { public IotHubMessageResult execute(Message msg, Object context) { Counter counter = (Counter) context; System.out.println( "Received message " + counter.toString() + " with content: " + new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET)); int switchVal = counter.get() % 3; IotHubMessageResult res; switch (switchVal) { case 0: res = IotHubMessageResult.COMPLETE; break; case 1: res = IotHubMessageResult.ABANDON; break; case 2: res = IotHubMessageResult.REJECT; break; default: // should never happen. throw new IllegalStateException("Invalid message result specified."); } System.out.println("Responding to message " + counter.toString() + " with " + res.name()); counter.increment(); return res; } } /** * Used as a counter in the message callback. */ static class Counter { int num; Counter(int num) { this.num = num; } int get() { return this.num; } void increment() { this.num++; } @Override public String toString() { return Integer.toString(this.num); } }
6.需要发送数据时,调用以下代码段
private void SendMessage() throws URISyntaxException, IOException { // Comment/uncomment from lines below to use HTTPS or MQTT protocol // IotHubClientProtocol protocol = IotHubClientProtocol.HTTPS; IotHubClientProtocol protocol = IotHubClientProtocol.MQTT; DeviceClient client = new DeviceClient(connString, protocol); try { client.open(); } catch (Exception e2) { System.err.println("Exception while opening IoTHub connection: " + e2.toString()); } //发送message填充 String msgStr = "{\"deviceId\":\"" + deviceId + ",\"PM25\":" + PM25 + ",\"PM10\":" + PM10 + "}"; try { Message msg = new Message(msgStr); msg.setProperty("PMAlert", PM25 > 100 ? "true" : "false"); msg.setMessageId(java.util.UUID.randomUUID().toString()); System.out.println(msgStr); EventCallback eventCallback = new EventCallback(); client.sendEventAsync(msg, eventCallback, null); } catch (Exception e) { System.err.println("Exception while sending event: " + e.getMessage()); } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } client.closeNow(); }
相关文章
- 如何将为Android开发的AIR应用转移到SD卡
- 安卓 android studio 报错 The specified Android SDK Build Tools version (27.0.3) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle
- android Notification定义与应用
- Android应用开发(三):如何完成Activity之间的传递参数
- 手机Android音视频采集与直播推送,实现单兵、移动监控类应用
- 50. 如何使用 Cordova 将 SAP UI5 应用生成一个能在 Android 手机上安装的混合应用
- Android 7.1有些应用无法使用以太网下载视频问题
- android系统预装第三方应用并启动应用
- Android 12.0设置默认浏览器安装一款浏览器默认浏览器无效的解决方案
- Android FFmpegMediaMetadataRetriever获取歌曲的作者
- Android 10.0在电话拨号盘(Dialer app中)通过暗码进入工厂测试模式
- android q(10.0)app应用卸载白名单
- 【Android 逆向】Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | Visual Studio 中 SDK 和 NDK 安装位置 )
- 【Android 插件化】现有的针对插件化恶意应用的解决方案 | 插件化应用开发推荐方案
- 【Android 插件化】Hook 插件化框架 ( 通过反射获取 “宿主“ 应用中的 Element[] dexElements )
- 【Android 进程保活】应用进程拉活 ( 双进程守护保活 )
- 【Android 性能优化】应用启动优化 ( 安卓应用启动分析 | Launcher 应用启用普通安卓应用 | 应用进程分析 )
- android app应用名字(多语言,本地化,国际化,全球化)
- android 输入法如何启动流程_Android输入法显示流程
- 实验四 Android项目CriminalIntent应用开发一