1
votes

I have cloned android application from

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

and created a IOT Hub as directed Here

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

and I got Connection string as mentioned there

My code is as bellow

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());
    }

but getting this error while running app, please correct me

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)
2
Add the exception stack traceMrityunjay

2 Answers

3
votes

This issue caused by using the wrong connection string.

Instead of using IoT Hub connection string you need use device connection string that has the following format:

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

Simply, you can get this connection string from Device Explorer.

0
votes

https://stackoverflow.com/a/44688691/15057788 Confirm this solution it also work fine for me, you could see my details issue below.

**1.**First time once the applicatin lunch, in the logcat shown me to "com.iothub.azure.microsoft.com.androidsample E/ion: ioctl c0044901 failed with code -1: Invalid argument"

**2.**Second time once click starting to connect the IoThub from the application on the application display shown me to "Exception while opening IoTHub connection:java.lang.lllegalArgumentException:IoT Hub hostName cannot be null"

Hopefully, my details issue it's gonna be an benefit for anyone, and "Rita Han - MSFT" thank you so much.