Skip to content

Instantly share code, notes, and snippets.

@paulpv
Created February 23, 2016 09:58
Show Gist options
  • Save paulpv/f66ff6556f23af8dbf50 to your computer and use it in GitHub Desktop.
Save paulpv/f66ff6556f23af8dbf50 to your computer and use it in GitHub Desktop.
Azure iothub Java SDK on Android
public class AzureIoT
{
private static final String sServiceHostName = "...";
private static final String sServiceSharedAccessKey = "...";
private final Activity mActivity;
private final ExecutorService mExecutorService;
public AzureIoT(Activity activity)
{
mActivity = activity;
mExecutorService = Executors.newSingleThreadExecutor();
}
private Future<?> runAsyncTask(Runnable task)
{
return mExecutorService.submit(task);
}
public interface DeviceAddCallback
{
void onSuccess(Device device);
void onException(String deviceId, Exception exception);
}
private static String createServiceConnectionString(String hostName, String sharedAccessKey)
{
return DeviceClient.HOSTNAME_ATTRIBUTE + hostName +
";SharedAccessKeyName=iothubowner" +
';' + DeviceClient.SHARED_ACCESS_KEY_ATTRIBUTE + sharedAccessKey;
}
private static String createDeviceConnectionString(String hostName, Device device)
{
return DeviceClient.HOSTNAME_ATTRIBUTE + hostName +
';' + DeviceClient.DEVICE_ID_ATTRIBUTE + device.getDeviceId() +
';' + DeviceClient.SHARED_ACCESS_KEY_ATTRIBUTE + device.getPrimaryKey();
}
public void deviceAdd(String deviceId, DeviceAddCallback callback)
{
runAsyncTask(() -> {
Device tempDevice;
Exception tempException;
try
{
String serviceConnectionString = createServiceConnectionString(sServiceHostName, sServiceSharedAccessKey);
RegistryManager registryManager = RegistryManager.createFromConnectionString(serviceConnectionString);
tempDevice = Device.createFromId(deviceId, null, null);
try
{
tempDevice = registryManager.addDevice(tempDevice);
tempException = null;
}
catch (IotHubException iote)
{
try
{
tempDevice = registryManager.getDevice(deviceId);
tempException = null;
}
catch (IotHubException iotf)
{
tempException = iotf;
}
}
}
catch (Exception e)
{
tempDevice = null;
tempException = e;
}
final Device device = tempDevice;
final Exception exception = tempException;
mActivity.runOnUiThread(() -> {
if (callback != null)
{
if (device != null)
{
callback.onSuccess(device);
}
else
{
callback.onException(deviceId, exception);
}
}
});
});
}
public interface DeviceSendMessageCallback
{
void onSuccess(Device device, DeviceClient deviceClient);
void onException(Device device, Exception exception);
}
public void deviceSendMessage(Device device, String messageText, DeviceSendMessageCallback callback)
{
runAsyncTask(() -> {
DeviceClient tempDeviceClient;
Exception tempException;
try
{
String connectionString = createIoTDeviceConnectionString(sServiceHostName, device);
tempDeviceClient = new DeviceClient(connectionString, IotHubClientProtocol.MQTT);
tempDeviceClient.open();
Message message = new Message(messageText);
IotHubEventCallback iotHubEventCallback = (responseStatus, callbackContext) -> {
FooLog.i(TAG,
"deviceSendMessage: IoT Hub responded to message with status " +
responseStatus.name());
if (callbackContext != null)
{
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (callbackContext)
{
callbackContext.notify();
}
}
};
Object callbackContext = new Object();
tempDeviceClient.sendEventAsync(message, iotHubEventCallback, callbackContext);
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (callbackContext)
{
try
{
callbackContext.wait();
}
catch (InterruptedException e)
{
// ignore
}
}
tempException = null;
}
catch (Exception e)
{
tempDeviceClient = null;
tempException = e;
}
DeviceClient deviceClient = tempDeviceClient;
Exception exception = tempException;
mActivity.runOnUiThread(() -> {
if (callback != null)
{
if (deviceClient != null)
{
callback.onSuccess(device, deviceClient);
}
else
{
callback.onException(device, exception);
}
}
});
if (deviceClient != null)
{
try
{
deviceClient.close();
}
catch (IOException e)
{
// ignore
}
}
});
}
}
mAzureIoT = new AzureIoT(this);
String deviceId = "qwigybo";
mAzureIoT.deviceAdd(deviceId, new IoTDeviceAddCallback()
{
@Override
public void onSuccess(Device device)
{
FooLog.i(TAG, "deviceAdd onSuccess: device id=" + device.getDeviceId());
FooLog.i(TAG, "deviceAdd onSuccess: device primaryKey=" + device.getPrimaryKey());
String message = "w00t @ " + new Date().toString();
mAzureIoT.deviceSendMessage(device, message, new IoTDeviceSendMessageCallback()
{
@Override
public void onSuccess(Device device, DeviceClient deviceClient)
{
FooLog.i(TAG, "deviceSendMessage onSuccess: device id=" + device.getDeviceId());
}
@Override
public void onException(Device device, Exception exception)
{
FooLog.e(TAG, "deviceSendMessage onException: device id=" + deviceId, exception);
}
});
}
@Override
public void onException(String deviceId, Exception exception)
{
FooLog.e(TAG, "deviceAdd onException: device id=" + deviceId, exception);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment