Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-bluetooth-adapter-llsync

Package Overview
Dependencies
9
Maintainers
6
Versions
124
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

qcloud-iotexplorer-bluetooth-adapter-llsync

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


Version published
Maintainers
6
Weekly downloads
12
increased by50%

Weekly downloads

Readme

Source

qcloud-iotexplorer-bluetooth-adapter-llsync

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

<img src=https://iot-public-1256872341.cos.ap-guangzhou.myqcloud.com/shuaisguo/1629101191691.gif style="width: 200px">

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, }

FAQs

Last updated on 27 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc