小程序蓝牙模块教程--小程序走过的坑(12)(最新版)

小程序支持蓝牙连接,来讲讲小程序蓝牙连接过程

demo

1、初始化蓝牙设备

  • 其他蓝牙相关 API 必须在 wx.openBluetoothAdapter 调用之后使用。否则 API 会返回错误(errCode=10000)。
  • 关闭蓝牙模块。调用该方法将断开所有已建立的连接并释放系统资源。建议在使用蓝牙流程后,与 wx.openBluetoothAdapter 成对调用。
  • 官方建议与关闭蓝牙模块一起使用
wx.openBluetoothAdapter({
  success (res) {
    console.log(res)
  }
})

 

2、打开蓝牙模块之后就可以监听蓝牙

wx.onBluetoothAdapterStateChange(function (res) {
  console.log('adapterState changed, now is', res)
})

通过该接口监听蓝牙设备状态。蓝牙适配器是否可用,蓝牙适配器是否处于搜索状态将会在该接口通知

3、获得蓝牙适配器状态

wx.getBluetoothAdapterState({
  success (res) {
    console.log(res)
  }
})

判断蓝牙是否可用

4、开始搜索蓝牙

成功后打开寻找到新设备的事件的回调函数;

新设备回调将会在该回调函数被调用

wx.startBluetoothDevicesDiscovery({
  services: ['FEE7'],
  success (res) {
    console.log(res)
    //成功后
wx.onBluetoothDeviceFound(function(devices) {
  console.log('new device list has founded')
  console.dir(devices)
  console.log(ab2hex(devices[0].advertisData))
})
  }
})

5、在回调中匹配参数确定要查找的设备,先关闭搜索。再连接该设备

停止查找

wx.stopBluetoothDevicesDiscovery({
  success (res) {
    console.log(res)
  }
})

连接设备 

wx.createBLEConnection({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 
  deviceId,
  success (res) {
    console.log(res)
  }
}) 

6、连接成功获取蓝牙设备所有服务(service)。通过isPrimary字段判断

wx.getBLEDeviceServices({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId,
  success (res) {
    console.log('device services:', res.services)
  }
})

7、获取蓝牙设备该服务中所有特征值(characteristic)。

wx.getBLEDeviceCharacteristics({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  success (res) {
    console.log('device getBLEDeviceCharacteristics:', res.characteristics)
  }
})

8、启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。

wx.notifyBLECharacteristicValueChange({
  state: true, // 启用 notify 功能
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接  
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  characteristicId,
  success (res) {
    console.log('notifyBLECharacteristicValueChange success', res.errMsg)
  }
})

9、监听低功耗蓝牙设备的特征值变化事件。必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。(设备推送的消息将会再该事件被回调)

// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
  let hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}
wx.onBLECharacteristicValueChange(function(res) {
  console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
  console.log(ab2hex(res.value))
})

10-1、向特征值写入数据(向蓝牙写入设备)必须设备的特征值支持 write 才可以成功调用。

注意,安卓只支持20个字节,超过需要多次写入,ios支持40字节。

// 向蓝牙设备发送一个0x00的16进制数据
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)

wx.writeBLECharacteristicValue({
  // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  characteristicId,
  // 这里的value是ArrayBuffer类型
  value: buffer,
  success (res) {
    console.log('writeBLECharacteristicValue success', res.errMsg)
  }
})

10-2、读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用。

// 必须在这里的回调才能获取
wx.onBLECharacteristicValueChange(function(characteristic) {
  console.log('characteristic value comed:', characteristic)
})

wx.readBLECharacteristicValue({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  characteristicId,
  success (res) {
    console.log('readBLECharacteristicValue:', res.errCode)
  }
})

 

11、写入完事件将会在第9点被推送。进行接下来的业务逻辑。

12、接下来就是关闭连接

wx.closeBLEConnection({
  deviceId,
  success (res) {
    console.log(res)
  }
})

13、关闭蓝牙模块。调用该方法将断开所有已建立的连接并释放系统资源。

wx.closeBluetoothAdapter({
  success (res) {
    console.log(res)
  }
})

其余接口

14、根据 uuid 获取处于已连接状态的设备。注意,已连接设备的获取是在蓝牙适配器初始化之后的。并不是手机所有的连接设备。(巨坑)

wx.getConnectedBluetoothDevices({
  success (res) {
    console.log(res)
  }
})

 

调试工具蓝牙源码免费提供

 

小程序调试工具

微信搜索【在线二维码】小程序

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值