const util = require("../../utils/util"); const { inArray, ab2hex } = util Page({ data: { devices: [], connected: false, cmd: '', name: '', weight: "", height: "", text: "", imp0: '', imp1: '', imp2: '', imp3: '', imp4: '', imp5: '', imp7: '', uuid1: null, uuid2: null, uuid3: null, deviceId: null, }, onLoad: function() {}, // 初始化蓝牙模块 openBluetoothAdapter() { wx.openBluetoothAdapter({ success: (res) => { console.log('openBluetoothAdapter success', res) wx.showToast({ title: '蓝牙连接中', icon: "none" }) this.startBluetoothDevicesDiscovery() }, fail: (res) => { if (res.errCode === 10001) { wx.showToast({ title: '请打开蓝牙', icon: "none" }) // 监听本机蓝牙状态变化的事件 wx.onBluetoothAdapterStateChange((res) => { console.log('onBluetoothAdapterStateChange', res) if (res.available) { this.startBluetoothDevicesDiscovery() } }) } } }) }, // 开始搜寻附近的蓝牙外围设备 startBluetoothDevicesDiscovery() { if (this._discoveryStarted) { return } this._discoveryStarted = true wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: true, interval: 1000, //上报设备的间隔 services: [ "FFE0", ], 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 } if (device.name.indexOf('AiLink_') != -1) { wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后,停止搜寻附近的蓝牙 const foundDevices = this.data.devices const idx = inArray(foundDevices, 'deviceId', device.deviceId) const data = {} let buff = device.advertisData.slice(-6) device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址,这是由于iOS不直接返回mac地址 let tempMac = Array.from(device.mac) tempMac.reverse() device.macAddr = ab2hex(tempMac, ':').toUpperCase() if (idx === -1) { data[`devices[${foundDevices.length}]`] = device } else { data[`devices[${idx}]`] = device } this.setData(data) } }) }) }, // 连接低功耗蓝牙设备 createBLEConnection(e) { wx.showLoading({ title: '连接中', }) const ds = e.currentTarget.dataset const index = ds.index this._device = this.data.devices[index] 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") this.onBLEConnectionStateChange() this.getBLEDeviceServices(deviceId) }, fail: res => { wx.hideLoading() wx.showToast({ title: '连接失败', icon: 'none' }) } }) }, //监听蓝牙连接状态 onBLEConnectionStateChange() { wx.onBLEConnectionStateChange((res) => { if (!res.connected) { setTimeout(() => { wx.showToast({ title: '连接已断开', icon: 'none' }) }, 500) this.setData({ connected: false, devices: [], weight: "", height: "", text: "", imp: "", imp0: '', imp1: '', imp2: '', imp3: '', imp4: '', imp5: '', imp7: '', }) } }) }, // 获取蓝牙设备的 serviceId getBLEDeviceServices(deviceId) { wx.getBLEDeviceServices({ deviceId, success: (res) => { for (let i = 0; i < res.services.length; i++) { if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) { wx.showLoading({ title: '获取设备的UUID成功', }) this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid) return } } } }) }, // 获取蓝牙设备某个服务中所有特征值(characteristic) /** * read: true读,write: true写,notify: true广播 */ getBLEDeviceCharacteristics(deviceId, serviceId) { let that = this that._deviceId = deviceId that._serviceId = serviceId that._device.serviceId = serviceId wx.hideLoading() wx.getBLEDeviceCharacteristics({ deviceId, serviceId, success: (res) => { console.log('getBLEDeviceCharacteristics success', res.characteristics) wx.showLoading({ title: '获取特征值成功', }) for (let i = 0; i < res.characteristics.length; i++) { let item = res.characteristics[i]; if (item.uuid.indexOf('FFE1') != -1) { that.data.uuid1 = item.uuid //主服务 UUID } else if (item.uuid.indexOf('FFE2') != -1) { that.data.uuid2 = item.uuid //写入设置 that.notifyBLECharacteristicValue(deviceId, serviceId, item.uuid) } else if (item.uuid.indexOf('FFE3') != -1) { that.data.uuid3 = item.uuid //监听数据 } } wx.hideLoading() }, fail(res) { console.error('getBLEDeviceCharacteristics', res) } }) }, //解析蓝牙返回数据 notifyBLECharacteristicValue(deviceId, serviceId, notifyId) { let that = this wx.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 deviceId: deviceId, serviceId: serviceId, characteristicId: notifyId, success(res) { wx.onBLECharacteristicValueChange((characteristic) => { let value = ab2hex(characteristic.value, ""); let num = value.substring(18, 19) let dw = value.substring(19, 20) let type = value.substring(8, 10) let typeInfo = value.substring(10, 12) console.log("type", type) if (type == "01") { // 称重模式 let data = parseInt(value.substring(13, 18), 16) let dw1 = "kg" if (dw == "1") { dw1 = "斤" } if (dw == "4") { dw1 = "st:lb" } if (dw == "6") { dw1 = "lb" } if (num == "1") { data = parseInt(value.substring(13, 18), 16) / 10 } if (num == "2") { data = parseInt(value.substring(13, 18), 16) / 100 } if (num == "3") { data = parseInt(value.substring(13, 18), 16) / 1000 } if (typeInfo == "01") { that.setData({ weight: "实时体重是:" + data + dw1 }) } if (typeInfo == "02") { that.setData({ weight: "稳定体重是:" + data + dw1 }) } } if (type == "02") { //阻抗 let mcu = value.substring(12, 14) console.log("typeInfo", typeInfo) if (typeInfo == "02") { that.setData({ imp0: "阻抗值测量失败" }) } else if (typeInfo == "03") { if (mcu == "00") { let imp0 = parseInt(value.substring(14, 22), 16) that.setData({ imp0: "双脚阻抗:" + imp0 }) } if (mcu == "01") { let imp1 = parseInt(value.substring(14, 22), 16) that.setData({ imp1: "双手阻抗:" + imp1 }) console.log("双手", imp1) } if (mcu == "02") { let imp2 = parseInt(value.substring(14, 22), 16) that.setData({ imp2: "左手阻抗:" + imp2 }) console.log("左手阻抗", imp2) } if (mcu == "03") { let imp3 = parseInt(value.substring(14, 22), 16) that.setData({ imp3: "右手阻抗:" + imp3 }) console.log("右手阻抗", imp3) } if (mcu == "04") { let imp4 = parseInt(value.substring(14, 22), 16) that.setData({ imp4: "左脚阻抗:" + imp4 }) console.log("左脚阻抗", imp4) } if (mcu == "05") { let imp5 = parseInt(value.substring(14, 22), 16) that.setData({ imp5: "右脚阻抗:" + imp5 }) console.log("右脚阻抗", imp5) } if (mcu == "07") { let imp7 = parseInt(value.substring(14, 22), 16) that.setData({ imp7: "右全身阻抗:" + imp7 }) } } } if (type == "05") { //身高 let height = parseInt(value.substring(14, 18), 16) / 10 let dw1 = "cm" if (dw == "01") { dw1 = "inch" } if (dw == "02") { dw1 = "ft-in" } if (typeInfo == "02") { that.setData({ height: "身高:" + height + "单位:" + dw1 }) } else if (typeInfo == "03" || typeInfo == "04") { that.setData({ height: "身高测量失败" }) } } }) }, fail(res) { console.error('getBLEDeviceCharacteristics', res) } }) }, /** * 断开蓝牙模块 */ closeBluetoothAdapter() { wx.closeBluetoothAdapter() this._discoveryStarted = false wx.showToast({ title: '断开蓝牙模块', icon: 'none' }) this.setData({ devices: [], weight: "", height: "", text: "", imp: "", imp0: '', imp1: '', imp2: '', imp3: '', imp4: '', imp5: '', imp7: '', }) }, // 断开与低功耗蓝牙设备的连接 closeBLEConnection() { wx.closeBLEConnection({ deviceId: this._deviceId }) wx.showToast({ title: '断开蓝牙连接', icon: 'none' }) this.setData({ connected: false, devices: [], text: "", height: "", weight: "", imp: "", imp0: '', imp1: '', imp2: '', imp3: '', imp4: '', imp5: '', imp7: '', }) }, });