1.开启蓝牙

//初始化蓝牙适配器 openBluetoothAdapter: function () { let that = this; let timer = null; wx.openBluetoothAdapter({ success: function (res) { console.log('开启蓝牙---', res) return that.getBluetoothAdapterState(); }, fail: function (err) { console.log('初始失败---', err); clearTimeout(timer) timer = setTimeout(function () { return that.openBluetoothAdapter(); }, 1000) } }); }, 

2.获取本机蓝牙适配器状态

getBluetoothAdapterState: function () { let that = this; wx.getBluetoothAdapterState({ success: function (res) { console.log('蓝牙状态---', res) let available = res.available let discovering = res.discovering if (!available) { return that.startConnect(); } else if (!discovering) { return that.startBluetoothDevicesDiscovery(); } } }) }, 

3.开始搜寻蓝牙设备

startBluetoothDevicesDiscovery: function () { let that = this; let timer = null; wx.startBluetoothDevicesDiscovery({ //services: ['FEE7'],//添加此参数后,在魅族设备上找不到设备 allowDuplicatesKey: false,//是否重复上报同一设备 success: function (res) { console.log('蓝牙搜索---', res) let isDiscovering = res.isDiscovering if (!isDiscovering) { return that.startBluetoothDevicesDiscovery() } else { return that.getBluetoothDevices(); } }, fail: function (err) { console.log('搜索失败---', err); wx.closeBluetoothAdapter({ success: function (res) { console.log(res) clearTimeout(timer) timer = setTimeout(function () { return that.startConnect(); }, 3000) } }) } }); }, 

4.获取已发现的设备

getBluetoothDevices: function () { let that = this wx.getBluetoothDevices({ //services: ['FEE7'],//不知是否需要(官方文档没有提及) success: function (res) { console.log('获取所有已发现的蓝牙设备', res) let devices = res.devices let RSSI = '' let deviceId = '' let deviceName = '' for (let i = 0; i < devices.length; i++) { console.log(i + '设备信息---', devices[i]) deviceId = devices[i].deviceId deviceName = devices[i].name RSSI = devices[i].RSSI } if (deviceName == '设备名称') { console.log('命中目标开启连接--', deviceName, deviceId, RSSI) return that.createBLEConnection(deviceId); } else { return that.onBluetoothDeviceFound() } } }) }, 

5.监听寻找到新设备的事件

onBluetoothDeviceFound: function () { let that = this let timer = null console.log('监听新设备方法---') wx.onBluetoothDeviceFound(function (devices) { console.log('找到的新设备', devices) let name = devices.devices[0]['name'] if (name != '' && name.indexOf('设备名称') != -1) { let deviceId = devices.devices[0]['deviceId']; return that.createBLEConnection(deviceId); } }) }, 

6.连接低功耗蓝牙设备

createBLEConnection: function (deviceId) { let that = this; let timer = null; wx.createBLEConnection({ // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId: deviceId, success: function (res) { console.log('连接设备---', res) //连接成功则停止搜索蓝牙 wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log('停止搜索---', res) return that.getBLEDeviceServices(deviceId) } }) }, fail: function (res) { console.log('连接失败---', res); clearTimeout(timer) timer = setTimeout(function () { wx.closeBLEConnection({ deviceId: deviceId, success: function (res) { console.log(res) return that.createBLEConnection(deviceId); } }) }, 3000) } }) }, 

7.获取蓝牙设备所有 service(服务)

getBLEDeviceServices: function (deviceId) { let that = this; wx.getBLEDeviceServices({ // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId: deviceId, success: function (res) { console.log('设备服务信息---', res.services) return that.getBLEDeviceCharacteristics(deviceId) } }) }, 

8.获取蓝牙特征值

getBLEDeviceCharacteristics: function (deviceId) { let that = this; wx.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取,或者根据蓝牙协议写死(此demo写死) serviceId: serviceId, success: function (res) { console.log('设备特征值---', res.characteristics) // 这里的回调可以获取到 write 导致的特征值改变 that.onBLECharacteristicValueChange() //开启notify that.notifyBLECharacteristicValueChange(deviceId) } }) }, 

9.监听设备特征值变化

onBLECharacteristicValueChange: function () { let that = this; wx.onBLECharacteristicValueChange(function (res) { console.log(`特征值 ${res.characteristicId} 已改变, 现在是 ${res.value}`) //在此获取到设备token }) }, 

10.启用notify功能

notifyBLECharacteristicValueChange: function (deviceId) { let that = this; let timer = null; wx.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 serviceId: serviceId,//写死 // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 characteristicId: this_chara_read,//写死 success: function (res) { console.log('notify功能---', res) console.log('准备写入数据,需要延迟---') clearTimeout(timer) timer = setTimeout(function () { return that.writeBLECharacteristicValue(deviceId) }, 2000) } }) }, 

11.向蓝牙设备写入数据

writeBLECharacteristicValue: function (deviceId) { let that = this; let timer = null; // 向蓝牙设备发送数据 let hex = that.aesEncrypt(getToken) let buffer = new ArrayBuffer(16) let dataView = new DataView(buffer) for (let i = 0; i < dataView.byteLength; i++) { dataView.setUint8(i, hex[i]) } buffer = dataView.buffer wx.writeBLECharacteristicValue({ // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 serviceId: serviceId,//写死 // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 characteristicId: this_chara_write,//写死 // 这里的value是ArrayBuffer类型 value: buffer, success: function (res) { console.log('首次写入特征数据---', res) return that.openLock(deviceId) }, fail: function (res) { console.log('首次写入数据失败---', res) wx.closeBLEConnection({ deviceId: deviceId, success: function (res) { console.log('重新开启连接', res) return that.createBLEConnection(deviceId); } }) } }) }, 

12.开锁

openLock: function (deviceId) { let that = this let timer = null //写入二进制数据 let k = 0; for (let i = 9; i < 13; i++) { openLock[i] = token[k]; k++; } // 向蓝牙设备发送数据 let hex = that.aesEncrypt(openLock) console.log('开锁', hex) let buffer = new ArrayBuffer(16) let dataView = new DataView(buffer) for (let i = 0; i < dataView.byteLength; i++) { dataView.setUint8(i, hex[i]) } buffer = dataView.buffer wx.writeBLECharacteristicValue({ // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取(此处写死) serviceId: serviceId, // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 characteristicId: this_chara_write,//写死 // 这里的value是ArrayBuffer类型 value: buffer, success: function (res) { console.log('开锁写入数据---', res) wx.closeBluetoothAdapter({ success: function (res) { console.log('关闭蓝牙模块---',res) } }) }, fail: function (res) { console.log('开锁失败数据--', res) wx.closeBLEConnection({ deviceId: deviceId, success: function (res) { console.log(res) return that.createBLEConnection(deviceId); } }) } }) },