const plugin = requirePlugin("sdkPlugin").AiLink; function inArray(arr, key, val) { for (let i = 0; i < arr.length; i++) { if (arr[i][key] === val) { return i; } } return -1; } // ArrayBuffer转16进度字符串示例 function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(','); } Page({ data: { showWriteInput:false, devices: [ // { // deviceId: "02:03:04:05:06:07", // name: "elink", // localName: "elink", // mac: "02:03:04:05:06:07", // RSSI: -69, // advertisServiceUUIDs:[ // "FFE0","FFC0" // ], // analyzeDataText:"0102003241123413" // } ], connected: false, chs: [], }, onLoad: function () { }, openBluetoothAdapter() { wx.openBluetoothAdapter({ success: (res) => { console.log('openBluetoothAdapter success', res) this.startBluetoothDevicesDiscovery() }, fail: (res) => { if (res.errCode === 10001) { wx.showToast({ title: '请打开蓝牙', icon:"none" }) wx.onBluetoothAdapterStateChange(function (res) { console.log('onBluetoothAdapterStateChange', res) if (res.available) { this.startBluetoothDevicesDiscovery() } }) } } }) }, getBluetoothAdapterState() { wx.getBluetoothAdapterState({ success: (res) => { console.log('getBluetoothAdapterState', res) if (res.discovering) { this.onBluetoothDeviceFound() } else if (res.available) { this.startBluetoothDevicesDiscovery() } } }) }, startBluetoothDevicesDiscovery() { if (this._discoveryStarted) { return } this._discoveryStarted = true wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: true, services: [ "FFE0", "F0A0", ], success: (res) => { console.log('startBluetoothDevicesDiscovery success', res) this.onBluetoothDeviceFound() }, }) }, stopBluetoothDevicesDiscovery() { wx.stopBluetoothDevicesDiscovery() }, onBluetoothDeviceFound() { wx.onBluetoothDeviceFound((res) => { res.devices.forEach(device => { if (!device.name && !device.localName) { return } const foundDevices = this.data.devices const idx = inArray(foundDevices, 'deviceId', device.deviceId) const data = {} console.log(device) // console.log(ab2hex(device.advertisData)) if(device.advertisServiceUUIDs[0].indexOf("F0A0") !== -1){ let parseDataRes = plugin.parseBroadcastData(device.advertisData) console.log(parseDataRes) if(parseDataRes.status == 1) { let analyzeData = plugin.analyzeBroadcastScaleData(parseDataRes) console.log(analyzeData) device.analyzeDataText = analyzeData.text } }else{ let buff = device.advertisData.slice(-6) device.mac = new Uint8Array(buff) } if (idx === -1) { data[`devices[${foundDevices.length}]`] = device } else { data[`devices[${idx}]`] = device } this.setData(data) }) }) }, createBLEConnection(e) { const ds = e.currentTarget.dataset const index = ds.index this._device = this.data.devices[index] console.log(this._device) const deviceId = ds.deviceId const name = ds.name this.mac = ds.mac wx.createBLEConnection({ deviceId, success: (res) => { this.setData({ connected: true, name, deviceId, }) console.log("createBLEConnection:success") wx.stopBluetoothDevicesDiscovery() this.getBLEDeviceServices(deviceId) } }) this.stopBluetoothDevicesDiscovery() }, closeBLEConnection() { wx.closeBLEConnection({ deviceId: this._deviceId }) this.setData({ connected: false, chs: [], }) }, getBLEDeviceServices(deviceId) { wx.getBLEDeviceServices({ deviceId, success: (res) => { for (let i = 0; i < res.services.length; i++) { if (res.services[i].isPrimary) { this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid) return } } } }) }, getBLEDeviceCharacteristics(deviceId, serviceId) { const _this = this this._deviceId = deviceId this._serviceId = serviceId this._device.serviceId = serviceId wx.getBLEDeviceCharacteristics({ deviceId, serviceId, success: (res) => { console.log('getBLEDeviceCharacteristics success', res.characteristics) // let uuid1, uuid2, uuid3; for (let i = 0; i < res.characteristics.length; i++) { let item = res.characteristics[i] if (item.uuid.indexOf('0000FFE1') != -1) { this.uuid1 = item.uuid //下发数据 } else if (item.uuid.indexOf('0000FFE2') != -1) { this.uuid2 = item.uuid //监听数据 } else if (item.uuid.indexOf('0000FFE3') != -1) { this.uuid3 = item.uuid //写入设置 } } // 打开监听 wx.notifyBLECharacteristicValueChange({ deviceId, serviceId, characteristicId: this.uuid2, state: true, }) wx.notifyBLECharacteristicValueChange({ deviceId, serviceId, characteristicId: this.uuid3, state: true, }) plugin.initPlugin(res.characteristics, this._device) wx.onBLECharacteristicValueChange((characteristic) => { let bleData = plugin.parseBleData(characteristic.value) if (bleData.status == 0) { console.log("握手成功") } else if (bleData.status == 1) { console.log(bleData) let parseData = bleData.data //对应协议中的payload数据,可以自行解析该数据 console.log(ab2hex(parseData)) console.log(ab2hex(bleData.completeData)) const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId) const data = {} if (idx === -1) { data[`chs[${this.data.chs.length}]`] = { uuid: characteristic.characteristicId, value: ab2hex(parseData) } } else { data[`chs[${idx}]`] = { uuid: characteristic.characteristicId, value: ab2hex(parseData) } } this.setData(data) } else { console.log(bleData) } }) }, fail(res) { console.error('getBLEDeviceCharacteristics', res) } }) }, writeBLECharacteristicValue(buffer, uuid, deviceId, serviceId) { // 向蓝牙设备发送一个二进制流数据 wx.writeBLECharacteristicValue({ deviceId, serviceId, characteristicId: uuid, value: buffer, success(res) { console.log('writeBLECharacteristicValue success', res) console.log(ab2hex(buffer)) } }) }, closeBluetoothAdapter() { wx.closeBluetoothAdapter() this._discoveryStarted = false }, showWriteInputView(){ this.setData({ showWriteInput: true }) }, hideWriteInputView(){ this.setData({ showWriteInput: false }) }, writingCmd(e){ this._cmd = e.detail.value }, submitCmd(){ if(!this._cmd){ return } let arr = [] if(this._cmd.indexOf(",") == -1){ arr = this._cmd.split(" ") } else { arr = this._cmd.split(",") } for(let i in arr){ arr[i] = parseInt(arr[i],16) } // let arr = [ // 0xA6, // 0x01, // 0x28, // 0x29, // 0x6A, // //A6 01 28 29 6A // ] if(arr[0] == 0xA6){ let payload = arr.slice(2,-2) plugin.sendDataOfA6(payload) } else if(arr[0] == 0xA7) { let cid = [arr[1], arr[2]] // 001E let payload = arr.slice(4,-2) plugin.sendDataOfA7(payload) // this.writeBLECharacteristicValue(buff, this.uuid1, this._deviceId, this._serviceId) } return }, });