// const plugin = requirePlugin("myPlugin").AiFresh; const {AiFresh: plugin} = requirePlugin("sdkPlugin"); 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: { 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" // } ], mac: "", connected: false, text: "", unitList_all: ['g', 'ml', 'lb:oz', 'oz', 'kg', '斤', '牛奶 ml', '水 ml', '牛奶 floz', '水 floz', 'lb'], unitList: ['g', 'ml', 'lb:oz', 'oz'], weight: 0, unit: 'g', unitIndex: 0, min: 0, second: 0, }, onLoad: function () { }, clearDevices() { this.setData({ devices: [] }) this.closeBluetoothAdapter() wx.nextTick(()=>{ this.openBluetoothAdapter() }) }, tare() { console.log("去皮") plugin.cmdTare() }, getUnits() { console.log("获取设备支持的单位") plugin.cmdGetAllUnits() }, countUp() { console.log("正计时开始") plugin.cmdTimeCountUp() }, countUpPause() { console.log("正计时暂停") plugin.cmdTimeCountUpPause(this.data.min, this.data.second) }, countDown() { console.log("倒计时开始") if (this.data.min || this.data.second) { plugin.cmdTimeCountDown(this.data.min, this.data.second) } else { plugin.cmdTimeCountDown(5, 30) } }, countDownPause() { console.log("倒计时暂停") plugin.cmdTimeCountDownPause(this.data.min, this.data.second) }, resetTime() { console.log("重置时间") plugin.cmdTimeReset() this.setData({ min: 0, second: 0, }) }, selectUnit(e) { let unit = e.currentTarget.dataset.unit let index = e.currentTarget.dataset.index let idx = this.data.unitList_all.indexOf(unit) plugin.cmdSwitchUnit(idx) }, 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: [ "FFB0", ], success: (res) => { console.log('startBluetoothDevicesDiscovery success', res) this.onBluetoothDeviceFound() }, }) }, stopBluetoothDevicesDiscovery() { wx.stopBluetoothDevicesDiscovery() }, onBluetoothDeviceFound() { wx.onBluetoothDeviceFound((res) => { res.devices.forEach(device => { const foundDevices = this.data.devices const idx = inArray(foundDevices, 'deviceId', device.deviceId) const data = {} console.log(device) // console.log(ab2hex(device.advertisData)) if (idx === -1) { data[`devices[${foundDevices.length}]`] = device } else { data[`devices[${idx}]`] = device } this.setData(data) }) }) }, createBLEConnection(e) { const ds = e.currentTarget.dataset const deviceId = ds.deviceId const name = ds.name const index = ds.index this._device = this.data.devices[index] console.log(this._device) wx.createBLEConnection({ deviceId, success: (res) => { let buff = this._device.advertisData.slice(-6) let arr = new Uint8Array(buff).reverse() let mac = ab2hex(arr).replace(/,/g,":") this.setData({ connected: true, name, deviceId, mac: mac, }) console.log("createBLEConnection:success") wx.stopBluetoothDevicesDiscovery() this.getBLEDeviceServices(deviceId) } }) this.stopBluetoothDevicesDiscovery() }, closeBLEConnection() { wx.closeBLEConnection({ deviceId: this._deviceId }) this.setData({ connected: false, min: 0, second: 0, weight: 0, unitList: ['g', 'ml', 'lb:oz', 'oz'], }) }, getBLEDeviceServices(deviceId) { wx.getBLEDeviceServices({ deviceId, success: (res) => { console.log('getBLEDeviceServices:', res) for (let i = 0; i < res.services.length; i++) { if (res.services[i].isPrimary && res.services[i].uuid.indexOf('0000FFB0') != -1) { this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid) return } } } }) }, getBLEDeviceCharacteristics(deviceId, serviceId) { this._deviceId = deviceId this._serviceId = serviceId this._device.serviceId = serviceId wx.getBLEDeviceCharacteristics({ deviceId, serviceId, success: (res) => { console.log('getBLEDeviceCharacteristics success', res.characteristics) for (let i = 0; i < res.characteristics.length; i++) { let item = res.characteristics[i] if (item.uuid.indexOf('0000FFB1') != -1) { this.uuid1 = item.uuid //下发数据 } else if (item.uuid.indexOf('0000FFB2') != -1) { this.uuid2 = item.uuid //监听数据 } } // 打开监听 wx.notifyBLECharacteristicValueChange({ deviceId, serviceId, characteristicId: this.uuid2, state: true, }) plugin.initPlugin(res.characteristics, this._device) plugin.cmdGetAllUnits() wx.onBLECharacteristicValueChange((characteristic) => { let bleData = plugin.parseBleData(characteristic.value) if (bleData.status == 1) { this.setData({ text: bleData.data.text, weight: bleData.data.weight, unit: bleData.data.unit, unitIndex: bleData.data.unitIndex }) } else if (bleData.status == 2) { this.setData({ min: bleData.data.min, second: bleData.data.second, }) } else if (bleData.status == 3) {//设备支持的单位 // 使用返回的单位索引,从本地的所有单位列表中取出支持的单位名 let unitList = bleData.data.unitIndexs.map(i=>{ return this.data.unitList_all[i] }) this.setData({ // unitList: bleData.data.unitList, unitList, }) } 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(plugin.ab2hex(buffer)) } }) }, closeBluetoothAdapter() { wx.closeBluetoothAdapter() this._discoveryStarted = false }, });