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); } }) } }) },