You must initialize LinkVisual SDK for Android before you use the SDK. This topic describes how to initialize the SDK.
For convenience purposes, all mentions of LinkVisual SDK in this topic refer to LinkVisual SDK for Android.
Prerequisites
A product and device are created in the IoT Platform console. For more information, see Device Connection.
LinkVisual SDK is obtained. For more information, see Obtain the SDK.
Background
You must first use Link SDK to connect your device to IoT Platform, and establish a channel for data exchange. You can then use LinkVisual SDK to listen to and process messages. You must initialize Link SDK before you initialize LinkVisual SDK.
For more information about the features of LinkVisual SDK and Link SDK and the relationship between the two SDKs, see Overview.
For more information about Link SDK, see Link SDK for Android.
Procedure
Step 1: Specify the device certificate information.
LinkKit.getInstance().init(this, params, new ILinkKitConnectListener() {
@Override
public void onError(AError error){
Log.d(TAG,
"onError() called with: error = [" + (error == null ? "null" : (error.getCode() + error.getMsg()))
+ "]");
}
@Override
public void onInitDone(Object data){
Log.d(TAG, "onInitDone() called with: data = [" + JSON.toJSONString(data) + "]");
// Initialize the SDK.
IPCDev.getInstance().init(context,${YourProductKey}, ${YourDeviceName}, ${YourDeviceSecret});
}
The following table describes the parameters:
${YourProductKey} | ProductKey | g18l**** | When the device is connected to LinkVisual, the device certificate information is saved. For more information, see Result. You can also view the information on the Device Details page in the IoT Platform console. |
${YourDeviceName} | DeviceName | Device1 | |
${YourDeviceSecret} | DeviceSecret | b2e6e4f1058d8**** |
Step 2: Register a listener for asynchronous service invocations in the Thing Specification Language (TSL) model.
For more information about device services, see Device services.
// Register the listener for asynchronous service invocations.
LinkKit.getInstance().getDeviceThing().setServiceHandler(service.getIdentifier(),
itResRequestHandler);
// The listener for asynchronous service invocations
private ITResRequestHandler itResRequestHandler = new ITResRequestHandler() {
@Override
public void onProcess(String identify, Object result, ITResResponseCallback
itResResponseCallback) {
Log.d(TAG,
"ITResRequestHandler onProcess() called with: identify = [" + identify + "], result = ["
+ JSON.toJSONString(result) + "], itResResponseCallback = ["
+ itResResponseCallback + "]");
/**
* Use the SDK to listen to asynchronous service invocations.
*/
IPCDev.getInstance().notifyAsyncTopicReceived(identify, result, itResResponseCallback);
}
@Override
public void onSuccess(Object o, OutputParams outputParams) {
Log.d(TAG,
"onSuccess() called with: o = [" + JSON.toJSONString(o) + "], outputParams = [" + JSON
.toJSONString(outputParams) + "]");
}
@Override
public void onFail(Object o, ErrorInfo errorInfo) {
Log.d(TAG, "onFail() called with: o = [" + JSON.toJSONString(o) + "], errorInfo = [" + JSON
.toJSONString(errorInfo) + "]");
}
};
Step 3: Register a listener for synchronous service invocations.
/**
* Register a listener for synchronous service invocations.
*/
LinkKit.getInstance().registerOnPushListener(connectNotifyListener);
// The listener for synchronous service invocations
private IConnectNotifyListener connectNotifyListener = new IConnectNotifyListener() {
@Override
public void onNotify(String connectId, String topic, AMessage aMessage) {
/**
* Use the SDK to listen to synchronous service invocations.
*/
IPCDev.getInstance().notifySyncTopicReceived(connectId, topic, aMessage);
if (CONNECT_ID.equals(connectId) && !TextUtils.isEmpty(topic) &&
topic.startsWith("/sys/" + productKey + "/" + deviceName + "/rrpc/request")) {
Log.d(TAG, "IConnectNotifyListener onNotify() called with: connectId = [" + connectId + "], topic = ["
+ topic + "], aMessage = ["
+ new String((byte[])aMessage.data) + "]");
}
}
@Override
public boolean shouldHandle(String connectId, String topic){
return true;
}
@Override
public void onConnectStateChange(String connectId, ConnectState connectState){
}
};