I'm trying to create a device on Azure IoT Hub using the Azure IoT Hub Java SDK (version 1.6.23), but I keep getting the following error when calling registryManager.addDevice(device):
com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException: Bad message format! ErrorCode:ArgumentInvalid;DeviceAuthenticationNoneInvalid ...
My expectation was that passing null for the symmetric key parameter in the Device.createFromId() method would trigger auto-generation of the key (as mentioned in the documentation). However, when I pass null, it seems that the device is created with an invalid authentication type ("None"), which IoT Hub rejects.
Here's the code snippet I'm using:
package ch.rfag.Azure;
import com.microsoft.azure.sdk.iot.service.RegistryManager;
import com.microsoft.azure.sdk.iot.service.Device;
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
public class CreateDevice {
// Replace with your IoT Hub connection string.
private static final String IOT_HUB_CONNECTION_STRING =
"HostName=your-iothub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=yourKey";
// Replace with your desired device ID.
private static final String DEVICE_ID = "JAVA-CREATED-DEVICE-1";
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
RegistryManager registryManager = RegistryManager.createFromConnectionString(IOT_HUB_CONNECTION_STRING);
// Expecting auto-generation of keys when passing null
Device device = Device.createFromId(DEVICE_ID, null, null);
try {
device = registryManager.addDevice(device);
} catch (IotHubException e) {
// If the device already exists, retrieve it.
System.err.println("Error creating device (it might already exist): " + e.getMessage());
device = registryManager.getDevice(DEVICE_ID);
}
System.out.println("Device created successfully!");
System.out.println("Device id: " + device.getDeviceId());
System.out.println("Primary Key: " + device.getPrimaryKey());
System.out.println("Secondary Key: " + device.getSecondaryKey());
}
}
I tried the solutions from here:
create azure IOT hub device using .Net which uses the async version in c# which also exists in java but that just runs forever and never returns.
What am i missing?
I solved the problem by using the newest version of
implementation 'com.microsoft.azure.sdk.iot:iot-service-client:2.1.9'
A device can be created like this:
public class CreateDevice {
// Replace with your IoT Hub connection string.
private static final String IOT_HUB_CONNECTION_STRING =
"";
// Replace with your desired device ID.
private static final String DEVICE_ID = "JAVA-CREATED-DEVICE-1";
public static void main(String[] args) throws IOException, IotHubException {
RegistryClient registryClient = new RegistryClient(IOT_HUB_CONNECTION_STRING);
Device device = new Device(DEVICE_ID);
try {
device = registryClient.addDevice(device);
} catch (IotHubException e) {
// If the device already exists, retrieve it.
System.err.println("Error creating device (it might already exist): " + e.getMessage());
}
System.out.println("Device created successfully!");
System.out.println("Device id: " + device.getDeviceId());
System.out.println("Primary Key: " + device.getPrimaryKey());
System.out.println("Secondary Key: " + device.getSecondaryKey());
}
}
Edit: Be aware that acording to my Intellij IDEA the newest version contains a vulnerability.