qcloud-iotexplorer-appdev-plugin-wificonf-blecombo
TypeScript icon, indicating that this package has built-in type declarations

3.0.112 • Public • Published

qcloud-iotexplorer-appdev-plugin-wificonf-blecombo

腾讯云物联网开发平台应用开发小程序端WIFI配网方式之蓝牙辅助配网(BLE-combo) SDK

安装依赖

npm install qcloud-iotexplorer-appdev-sdk
npm install qcloud-iotexplorer-appdev-plugin-wificonf-core

安装SDK

npm install qcloud-iotexplorer-appdev-plugin-wificonf-blecombo

配网流程

一、注册插件

就像其他配网方式那样,我们首先要向 appdev-sdk 里面注册 blecombo 插件,appdev-sdk 的初始化方式,详见qcloud-iotexplorer-appdev-sdk,这里不再赘述

import BleComboPlug from 'qcloud-iotexplorer-appdev-plugin-wificonf-blecombo';

BleComboPlug.install(sdk);

二、获取Wi-Fi信息

在这一步,需要使用表单来让用户填入WI-FI的 SSID 和 password 信息,以供后续配网使用。

三、连接设备

在配网正式开始前,我们还需要先完成手机和设备的蓝牙连接,并获得一个 deviceAdapter 来向设备传递WI-FI和token数据。主要分为以下几步:

1. 创建一个 bluetoothAdapter

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

import { BleComboEspDeviceAdapter, BleComboLLSyncDeviceAdapter } from 'qcloud-iotexplorer-appdev-plugin-wificonf-blecombo';
import { BlueToothAdapter } from 'qcloud-iotexplorer-bluetooth-adapter';

BleComboLLSyncDeviceAdapter.injectOptions({
  appDevSdk, // appDevSdk是 qcloud-iotexplorer-appdev-sdk 的实例
})
export const bluetoothAdapter = new BlueToothAdapter({
  deviceAdapters: [
    BleComboEspDeviceAdapter,
    BleComboLLSyncDeviceAdapter,
  ],
});

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 回调函数中,我们可以获得搜寻到的设备列表,这时可以将设备列表展示到页面上,供用户选择要连接哪个设备。

3. 连接设备

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

try {
  // device参数是上一步获取的devices中的某一个item
  const deviceAdapter = await bluetoothAdapter.connectDevice(device);

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

在上面三步完成之后,我们已经通过蓝牙连接到了设备,并获得了可以更设备通信的 deviceAdapter,接下来就可以正式进行配网了。

四、开始配网

接下来我们开始蓝牙辅助配网

  // 这里可以进行一些UI进度更新操作
  const onStepChange = (progress) => {
    console.log(progress);
  }

  // 这里是配网进行过程中的回调函数
  const onProgress = (data) => {
    console.info(data.code, data.detail);
    switch (data.code) {
      case WifiConfStepCode.PROTOCOL_START: // 开始配网
        onStepChange(1);
        break;
      case WifiConfStepCode.PROTOCOL_SUCCESS: // 设备联网成功,设备可以访问互联网
        onStepChange(2);
        break;
      case WifiConfStepCode.BUSINESS_QUERY_TOKEN_STATE_SUCCESS: // 发送token到设备成功
        break;
      case WifiConfStepCode.WIFI_CONF_SUCCESS: // 配网成功
        onStepChange(4);
        break;
    }
  };

  const onComplete = ({ productId, deviceName }) => {
    // 配网成功后,可以拿到设备的 productId 和 设备名称
    console.log('配网成功', productId, deviceName);
  };

  const onError = async ({ code, detail }) => {
    console.error('配网出错', code, detail);
  };

  const config = {
    wifiConfToken, // 用于设备连接云端的token
    targetWifiInfo: { // 用于设备联网的wifi信息,由用户填入
      SSID: '你的Wi-Fi名称',
      password: '你的Wi-Fi密码',
      BSSID: '',
    },
    wifiConfType: 'ble', // 'ble' | 'llsyncble'
    deviceAdapter, // 由连接设备之后获得

    familyId: 'default',
    roomId,

    onProgress, // 用来更新页面的进度条
    onError,
    OnComplete,
  }

  // 开始执行配网逻辑 go!
  sdk.plugins['wifiConfBleCombo'].start(config);

关于 bluetoothAdapter 的 api 更多介绍,请参考 https://github.com/tencentyun/qcloud-iotexplorer-bluetooth-adapter

Readme

Keywords

Package Sidebar

Install

npm i qcloud-iotexplorer-appdev-plugin-wificonf-blecombo

Weekly Downloads

9

Version

3.0.112

License

MIT

Unpacked Size

231 kB

Total Files

46

Last publish

Collaborators

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