Sample Code for lettest version of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 03/19/2021 | Standard authorized firmware sample v2.0.6 | Download |
How to run an Android sample code?
Prerequisite tools:
- Java 8 (or above)
- Android SDK
- Android Studio
Installation:
1. You can direclty import the sdk package from gradle URL.
2. Please verify jcenter in your root build.gradle at the end of repositories.
allprojects { repositories { ... jcenter() } }
2. Add the dependency package.
dependencies { implementation 'com.iotconnectsdk:iotconnectpoc:2.0.6' }
Usage:
To initialize the SDK object need to import above dependency package.
Prerequisite input data
uniqueId = <<uniqueId>>; cpId = <<CPID>>; env = <<env>>;
– To Initialize the SDK object and get the device information to connect to the device with IoTConnect cloud.
IoTConnectSDK ioTConnectSDK = new IoTConnectSDK(Activity.this, cpId, uniqueId, IotSDKCallback, environment);
Implement methods to receive the command from Cloud to Device(C2D)
IotSDKCallback // To receive the command from Cloud to Device(C2D). //Command receive from Cloud to Device(C2D). @Override public void onReceiveMsg(String message) { if (!message.isEmpty()) { JSONObject mainObject = new JSONObject(message); String cmdType = mainObject.getString("cmdType"); JSONObject dataObj = mainObject.getJSONObject("data"); switch (cmdType) { case "0x01": // Device Command break; case "0x02": // Firmware OTA Command break; } } }
Get attributes List
@Override public void attributeData(ListattributesBeanList) { Log.d(TAG,data); }
Get connection status
@Override public void onConnectionStateChange(boolean isConnected) { if (isConnected) { //device connected } else { //device not connected } }
To send the data from Device To Cloud(D2C)
data = [{ "uniqueId": "<< Device UniqueId >>", "time" : "<< date >>", //Date format should be as defined "data": {} // example : {"temperature": 15.55, "gyroscope" : { 'x' : -1.2 }} }]; iotConnectSDK.sendData(data);
To send the command acknowledgment
JSONObject obj = new JSONObject("{ "ackId": "", "st": "", "msg": "", "childId": "" }"); String messageType = ""; ioTConnectSDK.sendAck(JSONObject obj, String messageType)
- ackId(*) : Command Acknowledgment GUID which will receive from command payload (data.ackId)
- st(*) : Acknowledgment status sent to cloud
4 = Fail
6 = Device command[0x01]
7 = Firmware OTA command[0x02]) - msg : It is used to send your custom message
- childId : Leave it blank(“”)
- msgType : Message type
5 = Device command[0x01]
11 = Firmware OTA command[0x02]
Disconnect iotConnect on Activity onDestroy
@Override protected void onDestroy() { super.onDestroy(); if (ioTConnectSDK != null) { ioTConnectSDK.disconnectSDK(); } }