文档

初始化

更新时间:
一键部署

使用设备端Android版本LinkVisual SDK,您需要先初始化SDK。本文为您介绍初始化SDK的方法。

下文简称设备端Android版本LinkVisual SDK为LinkVisual SDK。

前提条件

  • 已创建产品和设备,具体操作,请参见设备接入

  • 已获取LinkVisual SDK,具体操作,请参见获取SDK

背景信息

LinkVisual SDK依赖于设备接入Link SDK提供的,设备端与云端的双向数据通道能力,进行消息监听和处理。初始化LinkVisual SDK前,需要先完成Link SDK的初始化。

操作步骤

步骤一:传入设备证书信息。

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) + "]");
                // 初始化SDK
                IPCDev.getInstance().init(context,${YourProductKey}, ${YourDeviceName}, ${YourDeviceSecret});
            }

其中,变量参数说明如下:

变量

参数

示例

说明

${YourProductKey}

ProductKey

g18l****

您添加设备后,保存的设备证书信息,详细信息,请参见执行结果。您也可在物联网平台控制台中设备的设备详情页面查看。

${YourDeviceName}

DeviceName

Device1

${YourDeviceSecret}

DeviceSecret

b2e6e4f1058d8****

步骤二:在物模型的设备服务中,注册异步服务调用的监听器。

设备服务的详细信息,请参考设备服务调用

//注册异步服务调用的监听器
LinkKit.getInstance().getDeviceThing().setServiceHandler(service.getIdentifier(),
                itResRequestHandler);
//异步服务调用监听器
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 + "]");
            /**
             * 添加SDK对异步服务调用的监听
             */
            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) + "]");
        }
    };

步骤三:注册同步服务调用的监听器。

/**
 * 注册同步服务调用的监听器
 */
LinkKit.getInstance().registerOnPushListener(connectNotifyListener);
//同步服务调用监听器
private IConnectNotifyListener connectNotifyListener = new IConnectNotifyListener() {
        @Override
        public void onNotify(String connectId, String topic, AMessage aMessage) {
            /**
             * 添加SDK对同步服务调用的监听
             */
            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){

        }
    };
  • 本页导读 (1)
文档反馈