Sample Code for lettest version of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 03/22/2021 | Standard authorized firmware sample v3.0 | Download |
How to run an Android sample code?
Prerequisite tools:
- Java 8 (or above)
- Android SDK
- Android Studio
Installation:
1. You can directly 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:3.0' }
Usage:
To initialize the SDK object need to import the above dependency package.
Prerequisite input data
String uniqueId = <<uniqueId>>; String cpId = <<CPID>>; String env = <<env>>;
- uniqueId : Its device ID which register on IotConnect platform and also its status has Active and Acquired.
- cpId : It is the company code. It gets from the IoTConnect UI portal “Settings->Key Vault”.
- env : It is the UI platform environment. It gets from the IoTConnect UI portal “Settings->Key Vault”.
SdkOptions is for the SDK configuration and needs to parse in SDK object initialize call. You need to manage the below configuration as per your device authentication type.
- If your device has an authentication type “Key” then no need any configuration. SDK will manage itself from device information.
- To configure the secure SSL/x509 connection follow below step for CA or SelfSiged certificate
- “sdkOptions” is optional. Mandatory for SSL/x509 device authentication type only.
- Standard file format for the “sdkOptions” is as below:
String sdkOptions = { "certificate" : { "SSLKeyPath" : "<< SystemPath >>/device.key", "SSLCertPath" : "<< SystemPath >>/device.pem", "SSLCaPath" : "<< SystemPath >>/rootCA.pem" }, "offlineStorage": { "disabled": false, "availSpaceInMb": 1, "fileCount": 5 } }
- certificate : It is indicated to define the path of the certificate file. Mandatory for X.509/SSL device CA signed or self-signed authentication type only.
- SSLKeyPath : your device key
- SSLCertPath: your device certificate
- SSLCaPath : Root CA certificate
- offlineStorage” : Define the configuration related to the offline data storage.
- disabled : false = offline data storing, true = not storing offline data
- availSpaceInMb: Define the file size of offline data which should be in (MB)
- fileCount : Number of files need to create for offline data
Note: sdkOptions is optional but mandatory for SSL/x509 device authentication type only. Define proper setting or leave it NULL. If you do not provide offline storage, it will set the default settings as per defined above. It may harm your device by storing the large data. Once memory gets full may chance to stop the execution.
To Initialize the SDK object and connect to the cloud
SDKClient sdkClient = SDKClient.getInstance(Activity.this,cpId,uniqueId,DeviceCallback,TwinUpdateCallback, sdkOptions,environment);
To receive the command 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 Command break; case "0x16": // Device Connection status (command : true [connected] // and command : false [disconnected]) break; default: break; } } }
To receive the twin from Cloud to Device(C2D)
@Override public void twinUpdateCallback(JSONObject data) { Log.d(TAG, data); }
To get the list of attributes with respective device.
String data = sdkClient.getAttributes(); Log.d("Attribute list device wise :", data);
\
Note: you can checkout device attribute format with all device type click here
This is the standard data input format for Gateway and non Gateway device to send the data on IoTConnect cloud(D2C)
// For Non Gateway Device String data = [{ "uniqueId": "<< Device UniqueId >>", "time" : "<< date >>", "data": {} }]; // For Gateway and multiple child device String data = [{ "uniqueId": "<< Gateway Device UniqueId >>", // It should be must first object of the array "time": "<< date >>", "data": {} }, { "uniqueId":"<< Child DeviceId >>", //Child device "time": "<< date >>", "data": {} }] sdkClient.sendData(String data);
- “time” : Date format should be as defined //”2021-01-24T10:06:17.857Z”
- “data” : JSON data type format // {“temperature”: 15.55, “gyroscope” : { ‘x’ : -1.2 }}
To update the Twin Property
String key = "<< Desired property key >>"; String value = "<< Desired Property value >>"; sdkClient.updateTwin(key,value)
- key : Desired property key received from Twin callback message
- value : Value of the respective desired property
To send the command acknowledgment from device to cloud.
JSONObject obj = new JSONObject("{ "ackId": "", "st": "", "msg": "", "childId": "" }"); String messageType = ""; sdkClient.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 : It is used for Gateway’s child device OTA update only
0x01 : null or “” for Device command
0x02 : null or “” for Gateway device and mandatory for Gateway child device’s OTA update.
How to get the “childId”?
– You will get child uniqueId for child device OTA command from payload “data.urls[~].uniqueId” - msgType : Message type
5 = Device command[0x01]
11 = Firmware OTA command[0x02]
To disconnect the device from the cloud
sdkClient.dispose()
To get the all twin property Desired and Reported
sdkClient.getAllTwins();
Disconnect iotConnect on Activity onDestroy
@Override protected void onDestroy() { super.onDestroy(); if (sdkClient != null) { sdkClient.dispose(); } }