小程序支持蓝牙连接,来讲讲小程序蓝牙连接过程
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) }})
调试工具蓝牙源码免费提供
小程序调试工具
微信搜索【在线二维码】小程序














