如何使用 android 从 Azure IoT 中心读取设备到云?
How to read device-to-cloud from Azure IoT hub with android?
我有一块板可以将 jsons 和遥测数据发送到 Azure IoT 中心(使用 http)。我想用我的 android 设备读取遥测数据。我查看了一些从 android 的 IoT 中心读取消息的示例,但我只发现了如何从 "Cloud to device feedback" 端点读取它们。所以现在我的应用程序看起来像:
Json from the board ----> "Events" endpoint ---> Function application that resending json to "Cloud to device feedback" endpoint
-----> "Cloud to device feedback" 端点 ----> Android 设备。
我是 Azure 的初学者,所以我确信存在更聪明的方法来做到这一点。 (Json 来自开发板 ----> "Events" 端点 ---> Android 设备)。我是在桌面上完成的,但看起来 android 不适用于桌面项目中的某些库。
有人知道我该怎么做吗? (也许是一些指南或课程)
部分android代码:
public void btnReceiveOnClick(View v) throws URISyntaxException, IOException
{
System.out.println("Receiving:");
Button button = (Button) v;
// 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);
if (protocol == IotHubClientProtocol.MQTT)
{
MessageCallbackMqtt callback = new MessageCallbackMqtt();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
} else
{
MessageCallback callback = new MessageCallback();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
}
try
{
client.open();
} catch (Exception e2)
{
System.out.println("Exception while opening IoTHub connection: " + e2.toString());
}
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
client.closeNow();
try {
....
}catch (JSONException je){
....
}
}
// 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)
{
responce = new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
Counter counter = (Counter) context;
System.out.println(
"[from MessageCallbackMqtt] Received message " + counter.toString()
+ " with content: " + responce);
counter.increment();
return IotHubMessageResult.COMPLETE;
}
}
static class EventCallback implements IotHubEventCallback
{
public void execute(IotHubStatusCode status, Object context)
{
Integer i = (Integer) context;
System.out.println("[from EventCallback] 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;
}
}
您可以参考此 document。它展示了如何使用 Java.In 和 ReadDeviceToCloudMessages.java 示例从您的 IoT 中心读取遥测数据,它连接到服务端事件端点在您的 IoT 中心上并接收设备到云的消息。
顺便说一句,您可以简单地从 Azure 门户获取 eventHubsCompatibleEndpoint、eventHubsCompatiblePath 和 iotHubSasKey。 eventHubsCompatibleEndpoint 格式如下:
sb://xxxxxxxxxxxxxx.servicebus.windows.net/
我有一块板可以将 jsons 和遥测数据发送到 Azure IoT 中心(使用 http)。我想用我的 android 设备读取遥测数据。我查看了一些从 android 的 IoT 中心读取消息的示例,但我只发现了如何从 "Cloud to device feedback" 端点读取它们。所以现在我的应用程序看起来像:
Json from the board ----> "Events" endpoint ---> Function application that resending json to "Cloud to device feedback" endpoint -----> "Cloud to device feedback" 端点 ----> Android 设备。
我是 Azure 的初学者,所以我确信存在更聪明的方法来做到这一点。 (Json 来自开发板 ----> "Events" 端点 ---> Android 设备)。我是在桌面上完成的,但看起来 android 不适用于桌面项目中的某些库。
有人知道我该怎么做吗? (也许是一些指南或课程)
部分android代码:
public void btnReceiveOnClick(View v) throws URISyntaxException, IOException
{
System.out.println("Receiving:");
Button button = (Button) v;
// 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);
if (protocol == IotHubClientProtocol.MQTT)
{
MessageCallbackMqtt callback = new MessageCallbackMqtt();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
} else
{
MessageCallback callback = new MessageCallback();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
}
try
{
client.open();
} catch (Exception e2)
{
System.out.println("Exception while opening IoTHub connection: " + e2.toString());
}
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
client.closeNow();
try {
....
}catch (JSONException je){
....
}
}
// 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)
{
responce = new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
Counter counter = (Counter) context;
System.out.println(
"[from MessageCallbackMqtt] Received message " + counter.toString()
+ " with content: " + responce);
counter.increment();
return IotHubMessageResult.COMPLETE;
}
}
static class EventCallback implements IotHubEventCallback
{
public void execute(IotHubStatusCode status, Object context)
{
Integer i = (Integer) context;
System.out.println("[from EventCallback] 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;
}
}
您可以参考此 document。它展示了如何使用 Java.In 和 ReadDeviceToCloudMessages.java 示例从您的 IoT 中心读取遥测数据,它连接到服务端事件端点在您的 IoT 中心上并接收设备到云的消息。
顺便说一句,您可以简单地从 Azure 门户获取 eventHubsCompatibleEndpoint、eventHubsCompatiblePath 和 iotHubSasKey。 eventHubsCompatibleEndpoint 格式如下:
sb://xxxxxxxxxxxxxx.servicebus.windows.net/