Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-appdev-plugin-wificonf-blecombo

Package Overview
Dependencies
18
Maintainers
6
Versions
172
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    qcloud-iotexplorer-appdev-plugin-wificonf-blecombo

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


Version published
Weekly downloads
18
decreased by-91.13%
Maintainers
6
Install size
8.32 MB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 15 Apr 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