qcloud-iotexplorer-bluetooth-adapter-llsync
TypeScript icon, indicating that this package has built-in type declarations

3.0.113 • Public • Published

qcloud-iotexplorer-bluetooth-adapter-llsync

腾讯连连 llsync sdk 开发文档,小程序可以通过本 sdk 完成和标准蓝牙设备进行连接,绑定,控制等流程。通过下面的图片可以直观地了解整个流程:

1. 创建一个 bluetoothAdapter

bluetoothAdapter 可以用来搜索设备,连接到设备。代码如下:

import { BlueToothAdapter } from 'qcloud-iotexplorer-bluetooth-adapter';
import { LLSyncDeviceAdapter } from 'qcloud-iotexplorer-bluetooth-adapter-llsync';

// 关于appDevSdk文档,详见https://www.npmjs.com/package/qcloud-iotexplorer-appdev-sdk
const options = {
  appDevSdk, // 通过qcloud-iotexplorer-appdev-sdk得到的实例
}
LLSyncDeviceAdapter.injectOptions(options);
export const bluetoothAdapter = new BlueToothAdapter({
  deviceAdapters: [
    LLSyncDeviceAdapter,
  ],
});

2. 获取蓝牙设备列表

通过 bluetoothAdapter.startSearch方法,我们可以发现设备,获得设备列表。

  const serviceIds = [BleComboLLSyncDeviceAdapter.serviceId];
  await bluetoothAdapter.startSearch({
    ignoreDeviceIds,
    serviceIds,
    ignoreServiceIds,
    onError: (error) => {
      console.log('----error', error);
      // 搜索设备出错
      bluetoothAdapter.stopSearch();
    },
    onSearch: (devices) => {
      console.log('searched devices', devices);
      if (devices.length > 0) {
        console.log('找到设备', devices); // 此时可以在页面上展示
      }
    },
    timeout: 1.4 * 15 * 1000,
  });

在上面的 onSearch 回调函数中,我们可以获得搜寻到的设备列表,这时可以将设备列表展示到页面上,供用户选择要连接哪个设备。

tip: 如果设备无法搜索到,请确认设备没有被绑定

3. 连接设备

用户从上面获取到的设备中选择一个,并发起连接操作时,可以调用 bluetoothAdapter.connectDevice 方法进行连接。连接成功后会返回一个 deviceAdapter,可以用来向连接的设备发送Wi-Fi,token等数据。

tip: 如果在连接时提示没有权限操作该产品,请到控制台/应用开发对应用和产品进行关联

try {
  // device 参数是上一步 onSearch 回调中获取 devices 数组的某一项
  const deviceAdapter = await bluetoothAdapter.connectDevice(device);

  if (!deviceAdapter) {
    throw {
      code: 'CONNECT_ERROR',
    }
  }
} catch (err) {
  console.error('连接到设备出错');
}

在这一步中,可以通过连接设备获得 deviceAdapter ,通过 deviceAdapter 可以完成后续设备的绑定和解绑操作

4. 绑定设备

绑定设备时,可以传入familyId, roomId, 从而将设备绑定到特定的家庭和房间

try {
  const deviceId = await deviceAdapter.bindDevice({ familyId, roomId });
} catch (err) {
  console.log(err);
}

5. 解绑设备

设备绑定后,也可以通过小程序发起删除设备的操作,这时需要调用解绑api,解绑完成后设备会恢复未绑定的状态。

try{
  await deviceAdapter.unbindDevice({ familyId, deviceId });
} catch (err) {
  console.log(err);
}

6. deviceAdapter事件

事件 描述 参数
connect 蓝牙设备连接时触发 DeviceInfo
disconnect 蓝牙设备连接断开时触发 DeviceInfo
authorized 蓝牙设备授权完成时触发 { version, mtu, otaVersion, }

Readme

Keywords

none

Package Sidebar

Install

npm i qcloud-iotexplorer-bluetooth-adapter-llsync

Weekly Downloads

16

Version

3.0.113

License

MIT

Unpacked Size

685 kB

Total Files

84

Last publish

Collaborators

  • xiaoyuze88
  • imgss
  • hazelchen
  • vivialex
  • elizahong
  • tencentiot