简体   繁体   English

如何使用Android从Azure IoT中心读取设备到云的信息?

[英]How to read device-to-cloud from Azure IoT hub with android?

I have a board that sends jsons with telemetry to Azure IoT hub (using http). 我有一块板,可通过遥测将json发送到Azure IoT中心(使用http)。 I want to read telemetry data with my android device. 我想用我的Android设备读取遥测数据。 I looked some examples of reading messages from IoT hub for android, but I found only how to read them from "Cloud to device feedback" endpoint. 我看了一些从IoT中心的Android读取消息的示例,但仅发现了如何从“云到设备反馈”端点读取消息的示例。 So now my application looks like: 所以现在我的应用程序看起来像:

Json from the board ----> "Events" endpoint ---> Function application that resending json to "Cloud to device feedback" endpoint -----> "Cloud to device feedback" endpoint ----> Android device. 董事会中的Json ---->“事件”端点--->将json重新发送到“ Cloud to device feedback”端点的功能应用程序----->“ Cloud to device feedback”端点----> Android设备。

I'm a beginner in Azure, so I'm sure that exists smarter way to do that. 我是Azure的初学者,因此我确信这是一种更聪明的方法。 (Json from the board ----> "Events" endpoint ---> Android device) . (来自董事会的Json ---->“ Events”端点---> Android设备) I did it on my desktop, but looks like android doesn't work with some libraries from desktop project. 我是在桌面上完成的,但看起来android无法与桌面项目中的某些库一起使用。

Does anybody know how can I do it? 有人知道我该怎么做吗? (maybe some guides or lessons) (也许是一些指南或课程)

Desktop version 桌面版

Part of android code: 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;
    }
}

You can refer to this document .It shows how to read the telemetry from you IoT Hub with Java.In the ReadDeviceToCloudMessages.java sample, it connects to the service-side Events endpoint on your IoT Hub and receives the device-to-cloud messages. 您可以参考本文档。文档显示了如何使用Java从IoT中心读取遥测信息。在ReadDeviceToCloudMessages.java示例中,它连接到IoT中心上的服务端事件端点并接收设备到云消息。

BTW, you can get the eventHubsCompatibleEndpoint , eventHubsCompatiblePath and iotHubSasKey from Azure Portal simply. 顺便说一句,您可以简单地从Azure门户获取eventHubsCompatibleEndpoint ,eventHubsCompatiblePath和iotHubSasKey The eventHubsCompatibleEndpoint is in this format: eventHubsCompatibleEndpoint的格式如下:

sb://xxxxxxxxxxxxxx.servicebus.windows.net/ sb://xxxxxxxxxxxxxx.servicebus.windows.net/

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM