我已经从
https://github.com/Azure/azure-iot-sdk-java/tree/master/device/iot-device-samples/android-sample
并按照此处的指示创建了一个 IOT 集线器
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)