【问题标题】:Android app is not connecting Azure-iot HubAndroid 应用未连接 Azure-iot Hub
【发布时间】:2017-06-21 10:04:27
【问题描述】:

我已经从

克隆了 android 应用程序

https://github.com/Azure/azure-iot-sdk-java/tree/master/device/iot-device-samples/android-sample

并按照此处的指示创建了一个物联网中心

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-getstarted

我得到了那里提到的连接字符串

我的代码如下

String connString = "HostName=********.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=FYhyjH***************c1rQ22g=";

IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;

    DeviceClient client = new DeviceClient(connString, protocol); // error at this line 

    try {
        client.open();
    }
    catch(IOException e1)
    {
        System.out.println("Exception while opening IoTHub connection3: " + e1.toString());
    }
    catch(Exception e2)
    {
        System.out.println("Exception while opening IoTHub connection4: " + e2.toString());
    }

但在运行应用程序时出现此错误,请纠正我

06-21 17:29:13.865 12640-12640/com.ccc.iot_demo I/System.out: Exception while opening IoTHub connection2: java.lang.IllegalArgumentException: Device ID cannot be null.
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err: java.lang.IllegalArgumentException: Device ID cannot be null.
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at com.microsoft.azure.sdk.iot.device.IotHubConnectionString.validateTerms(IotHubConnectionString.java:206)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at com.microsoft.azure.sdk.iot.device.IotHubConnectionString.<init>(IotHubConnectionString.java:90)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at com.microsoft.azure.sdk.iot.device.DeviceClient.<init>(DeviceClient.java:141)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at com.ccc.iot_demo.MainActivity.SendMessage(MainActivity.java:52)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at com.ccc.iot_demo.MainActivity.onCreate(MainActivity.java:33)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at android.app.Activity.performCreate(Activity.java:6259)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:     at android.os.Looper.loop(Looper.java:148)
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5443)
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

【问题讨论】:

  • 添加异常堆栈跟踪

标签: iot azure-iot-hub azure-iot-sdk


【解决方案1】:

这个问题是由于使用了错误的连接字符串造成的。

您需要使用具有以下格式的设备连接字符串,而不是使用 IoT 中心连接字符串:

HostName=[YOUR HUB NAME].azure-devices.net;DeviceId=[YOUR DEVICE ID];SharedAccessKey=rr****************************************4w=

简单地说,你可以从Device Explorer获取这个连接字符串。

【讨论】:

    【解决方案2】:

    https://stackoverflow.com/a/44688691/15057788 确认此解决方案对我来说也可以正常工作,您可以在下面查看我的详细信息问题。

    **1.**第一次应用午餐时,在 logcat 中显示“com.iothub.azure.microsoft.com.androidsample E/ion: ioctl c0044901 failed with code -1: Invalid argument”

    **2.**第二次单击开始从应用程序连接 IoThub 时,应用程序显示显示“打开 IoTHub 连接时出现异常:java.lang.lllegalArgumentException:IoT Hub hostName 不能为空”

    希望我的详细信息问题对任何人都有好处,“Rita Han - MSFT”非常感谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多