SDK开发
主要使用流程
添加设备

添加设备

在进入添加设备的流程之前您需要了解

  • ImCamWxSDK使用蓝牙的方式来添加设备
  • 若您的小程序同时有其他的使用蓝牙的场景,请确保在添加ImCam设备时,不要去使用小程序的蓝牙功能。

1. 扫描设备屏幕的蓝牙

  app.imcamWx.scanDeviceScreenQRCode()
    .then(res => {
      if (res.errCode === 0) console.log('扫描到设备', res.bluetoothDeviceName)
    })

2. 连接设备

  • 调用connectDevice来连接设备。此时需要用到上一步返回的bluetoothDeviceName
  app.imcamWx.connectDevice({bluetoothDeviceName})
    .then(res => {
      if(res.errCode === 0) console.log('连接设备成功')
    })

您也可以增加监听器,来监听设备和蓝牙的状态

  app.imcamWx.addDeviceStatusListener({
        id: 'deviceStatusListener',
        callback: res => {
            if (!res.connected) console.error('设备断开连接')
        }
    })
  app.imcamWx.addBLEStatusListener({
        id: 'BLEStatusListener',
        callback: res => {
            if (!res.available) console.error('蓝牙断开连接')
        }
    })
  /* 添加后,您也可以移除监听器 */ 
  app.imcamWx.removeBLEStatusListener({
    id: 'BLEStatusListener'
  })
  app.imcamWx.removeDeviceStatusListener({
    id: 'deviceStatusListener'
  })

3. 获取设备的wifi列表

  const onNewWifi = (data) => {
      if (data.ssid) console.log(data.ssid) // ['ChinaNet', 'Intercom']
      if (data.end === 1) console.log('wifi获取完毕')
  }
  app.imcamWx.startGetDeviceWifiList({onNewWifi})
  • 请注意:
    1. 该方法会在调用失败或者获取到第一组wifi信息时,进行返回Promise
    2. wifi信息是2个或者3个一组地返回,返回的方式是通过onNewWifi的传参来获取
    3. 该方法是让设备扫描wifi传给手机,设备只会搜索到2.4g的WiFi
    4. 设备单次扫描wifi,可能出现漏扫描的情况。此时只需要再调用一次getWifiList即可

4. 为设备配网

  • 上一步拿到wifi的ssid后,输入密码。然后调用setDeviceWifi即可为设备配网
  app.imcamWx.setDeviceWifi({ssid,password})
    .then(res => {
      if (res.errCode === 0) console.log('设备已开始配网,设备的sn为:' + res.sn)
    })

5.通知服务器绑定设备

  • 设备收到wifi配置成功后会返回res.sn。此时便可以调用bindDevice来添加设备
  • 请在收到设备开始配网的回复后立即调用此方法去绑定
  app.imcamWx.bindDevice({sn})
    .then(res => {
      if (res.errCode === 0) console.log('设备绑定成功')
    })

绑定成功后,便可去调用getDeviceList来获取设备列表,您将看到刚刚新绑定的设备

© 2024 Cylan