platform | device | language |
---|---|---|
android |
rp1600 |
java |
About this document
This document describes how to connect Intel Sofia with Azure IoT SDK. This multi-step process includes:
- Configuring Azure IoT Hub
- Registering your IoT device
- Build and deploy Azure IoT SDK on device
You should have the following items ready before beginning the process:
-
Prepare your development environment:
- Download and install latest JDK from here.
- Download Android Studio on your Windows machine and follow the installation instructions.
- Computer with Git client installed and access to the azure-iot-sdks GitHub public repository.
-
Make sure desktop is ready as per instructions given in Prepare your development environment.
-
Plug in your device to your development machine with a USB cable. If you're developing on Windows, you might need to install the appropriate USB driver for your device. For help installing drivers, see the OEM USB Drivers document.
-
Enable USB debugging on your device. On Android 4.0 and newer, go to Settings > Developer options. Kuzo device USB debugging is on by default,so this step can be ignored.
Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.
Please find Azure Android java device sample code here.
The sample application will require user interaction to click a button on screen to receive messages from IoT hub. If the target device is a screenless device, you may modify the sample code to have the app to receive message right after sending messages without user interactions.
Following is the example of modification:
```
public class MainActivity extends AppCompatActivity {
String connString = "[device connection string]";
protected void onCreate(Bundle savedInstanceState) {
. . .
try {
SendMessage();
btnReceiveOnClick(null); // call button click event handler
}
. . .
}
. . .
public void SendMessage() throws URISyntaxException, IOException {
. . .
// Comment/uncomment from lines below to use HTTPS or MQTT protocol
//IotHubClientProtocol protocol = IotHubClientProtocol.HTTPS;
IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
DeviceClient client = new DeviceClient(connString, protocol);
try {
client.open();
}
catch(IOException e1)
{
System.out.println("Exception while opening IoTHub connection: " + e1.toString());
}
catch(Exception e2)
{
System.out.println("Exception while opening IoTHub connection: " + e2.toString());
}
for (int i = 0; i < 5; ++i)
{
String msgStr = "Event Message " + Integer.toString(i);
try
{
Message msg = new Message(msgStr);
msg.setProperty("messageCount", Integer.toString(i));
System.out.println(msgStr);
EventCallback eventCallback = new EventCallback();
client.sendEventAsync(msg, eventCallback, i);
}
catch (Exception e)
{
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
client.close();
}
. . .
public void btnReceiveOnClick(View v) throws URISyntaxException, IOException {
. . .
// Comment/uncomment from lines below to use HTTPS or MQTT protocol
//IotHubClientProtocol protocol = IotHubClientProtocol.HTTPS;
IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
DeviceClient client = new DeviceClient(connString, protocol);
if (protocol == IotHubClientProtocol.MQTT)
{
MessageCallbackMqtt callback = new MessageCallbackMqtt();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
}
else
{
MessageCallback callback = new MessageCallback();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
}
try {
client.open();
}
catch(IOException e1)
{
System.out.println("Exception while opening IoTHub connection: " + e1.toString());
}
catch(Exception e2)
{
System.out.println("Exception while opening IoTHub connection: " + e2.toString());
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
client.close();
}
. . .
}
```
-
Start a new instance of Android Studio and open Android project from here:
azure-iot-sdks/java/device/samples/android-sample/
-
Go to MainActivity.java, replace the [device connection string] placeholder with connection string of the device you have created in Provision your device and get its credentials and save the file. An example of IoT Hub Connection String is as below:
HostName=[YourIoTHubName];SharedAccessKeyName=[YourAccessKeyName];SharedAccessKey=[YourAccessKey]
-
Build your project by going to Build menu > Make Project.
In this section you will run the Azure IoT client SDK samples to validate communication between your device and Azure IoT Hub. You will send messages to the Azure IoT Hub service and validate that IoT Hub has successfully receive the data. You will also monitor any messages sent from the Azure IoT Hub to client.
- Select one of your project's files and click Run from the toolbar.
- In the Choose Device window that appears, select the Choose a running device radio button, select your device, and click OK.
- Android Studio will install the app on your connected device and starts it.
- See Manage IoT Hub to learn how to observe the messages IoT Hub receives from the application.
- As soon as you run the app on your device (or emulator), it will start sending messages to IoTHub.
- Check the Android Monitor window in Android Studio. Verify that the confirmation messages show an OK. If not, then you may have incorrectly copied the device hub connection information.
- See Manage IoT Hub to learn how to send cloud-to-device messages to the application.
- Click the Receive Messages button from the sample App UI loaded on your device or in the emulator. If you modify the application code to receive message right after sending message, you could skip this step.
- Check the Android Monitor window in Android Studio. You should be able to see the command received.