建立设备与平台的连接

获取设备证书(ProductKey、DeviceName和DeviceSecret)后,设备可通过MQTT与IFLYIoT物联网平台通信

前置条件: 已创建产品和设备(如何创建产品和设备

操作步骤:

1.初始化SDK:
//配置设备凭证以及ApiSecret,ApiKey
HashParam param = new HashParam();
param.putParam(XIotMqttConstants.PRODUCT_KEY, 创建的ProductKey);
param.putParam(XIotMqttConstants.DEVICE_NAME, 创建的DeviceName);
param.putParam(XIotMqttConstants.DEVICE_SECRET, 创建的DeviceSecret);
param.putParam(XIotMqttConstants.API_SECRET, getString(R.string.apiSecret));
param.putParam(XIotMqttConstants.API_KEY, getString(R.string.apiKey));

if (XIotMqttClient.getInstance() != null) {
    XIotMqttClient.getInstance().destory();
}
try {
    //初始化SDK
    XIotMqttClient.createInstance(this.getApplicationContext(), param);
    //打开SDK日志
    XIotMqttClient.getInstance().setDebugOn(true);
} catch (XIotException e) {
   XiotLog.e("", "create client failed:" + e);
}
2.注册消息监听:
public class XiotTopicListener implements XIotTopicCallback {
    final String TAG = "XiotTopicListener";

    //事件消息
    @Override
    public void onEventReply(XIotMessage message) {
        XiotLog.d(TAG, "[onEventReply] " + message.getStringPayload());
    }
    //属性消息
    @Override
    public void onPropertyReply(XIotMessage message) {
        XiotLog.d(TAG, "[onPropertyReply] " + message.getStringPayload());
    }
    //获取属性响应
    @Override
    public void onPropertyRequest(XIotMessage message) {
        XiotLog.d(TAG, "[onPropertyRequest] " + message.getStringPayload());
    }
    //设置属性响应
    @Override
    public void onPropertySet(XIotMessage message) {
        XiotLog.d(TAG, "[onPropertySet] " + message.getStringPayload());
    }
    //服务消息下发
    @Override
    public void onServiceSet(XIotMessage message) {
        XiotLog.d(TAG, "[onServiceSet] " + message.getStringPayload());
    }
    /**
     * 主动下载模式下的下载提示接口
     * @param firmwareName
     * @param version    固件版本
     * @param firmwareMD5   固件md5
     * @param size
     * @param firmwareInfo  固件信息
     */
    @Override
    public void onFirmWareReadyToDownload(String firmwareName, String version, String firmwareMD5, long size, String firmwareInfo) {
        XiotLog.d(TAG, "onFirmWareReadyToDownload:name=" + firmwareName + " version=" + version + " firmwareMD5=" + firmwareMD5 + " firmwareInfo=" + " length=" + size);
    }
    /**
     * 固件版本下载监听
     *
     * @param errCode      错误码,取值范围{0,-1},0表示成功,-1表示失败
     * @param firmwareInfo 在错误码为0时,下载固件的本机位置;错误码为-1 时,具体错误信息
     */
    @Override
    public void onFirmWareDownloaded(int errCode, String firmwareInfo) {
        XiotLog.d(TAG, "onFireWareDownloaded :" + errCode + " info=" + firmwareInfo);
    }
    /**
     * 下载固件进度
     * @param progress  下载进度
     * @param total 文件总大小
     */
    @Override
    public void onFirmWareDownloading(double progress, long total) {
        XiotLog.d(TAG, "total:" + total + ", onFireWareDownloaded :" + progress);
    }
    //应用级远程配置反馈
    @Override
    public void onRemoteConfig(int errCode, HashParam param, String err) {

    }
    //下发标签上传结果
    @Override
    public void onDeviceNotice(XIotMessage message) {
        XiotLog.d(TAG, "[onDeviceNotice] " + message.getStringPayload());
    }
}
XIotMqttClient.getInstance().registerMsgListener(new XiotTopicListener());
3.建立连接:
//设置连接的超时时间
XIotMqttClient.getInstance().setConnectionTimeout(60000);
//设置最大重连次数
XIotMqttClient.getInstance().setMaxConnectionRetries(100);
//建立客户端sdk与服务端的连接,这是非阻塞式的连接
XIotMqttClient.getInstance().connect(new IMqttConnectionListener() {
    @Override
    public void onConnectionStatus(int code, final String status) {
        //连接结果回调 0代表成功
        XiotLog.v(TAG, code + ":-----:" + status);
    }
});

连接成功后,在产品详情页面,点击设备信息框中更多选项按钮,进入设备列表页面。

连接1

在设备列表页面,可实时看到设备的连接状态,设备激活时间以及最后上线时间。

连接2

下一步:设备上报数据

results matching ""

    No results matching ""