我有一个电路板,可以将具有遥测功能的jsons发送到Azure IoT中心(使用http)。我想用我的Android设备读取遥测数据。我查看了一些从IoT hub for android读取消息的示例,但我发现只能从“云到设备反馈”端点读取它们。所以现在我的应用程序看起来像:
董事会的Json ----> “事件”端点--->将json重新发送到“云到设备反馈”端点的功能应用程序 -----> “云到设备反馈”端点----> Android设备。
我是Azure的初学者,所以我确信存在更聪明的方法。 (董事会的Json ---->“事件”端点---> 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;
}
}
答案 0 :(得分:0)
您可以参考此document。它显示了如何使用Java从IoT Hub读取遥测。在ReadDeviceToCloudMessages.java示例中,它连接到IoT Hub上的服务端Events端点并接收设备到云的消息。
顺便说一下,您可以从Azure门户获取 eventHubsCompatibleEndpoint ,eventHubsCompatiblePath和 iotHubSasKey 。 eventHubsCompatibleEndpoint 采用以下格式:
SB://xxxxxxxxxxxxxx.servicebus.windows.net/