繁体   English   中英

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

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

我有一块板,可通过遥测将json发送到Azure IoT中心(使用http)。 我想用我的Android设备读取遥测数据。 我看了一些从IoT中心的Android读取消息的示例,但仅发现了如何从“云到设备反馈”端点读取消息的示例。 所以现在我的应用程序看起来像:

董事会中的Json ---->“事件”端点--->将json重新发送到“ Cloud to device feedback”端点的功能应用程序----->“ 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;
    }
}

您可以参考本文档。文档显示了如何使用Java从IoT中心读取遥测信息。在ReadDeviceToCloudMessages.java示例中,它连接到IoT中心上的服务端事件端点并接收设备到云消息。

顺便说一句,您可以简单地从Azure门户获取eventHubsCompatibleEndpoint ,eventHubsCompatiblePath和iotHubSasKey eventHubsCompatibleEndpoint的格式如下:

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

在此处输入图片说明

暂无
暂无

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

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