|
|
@@ -55,7 +55,7 @@ Page({ |
|
|
|
title: '请打开蓝牙', |
|
|
|
icon: "none" |
|
|
|
}) |
|
|
|
wx.onBluetoothAdapterStateChange(function (res) { |
|
|
|
wx.onBluetoothAdapterStateChange( (res)=> { |
|
|
|
console.log('onBluetoothAdapterStateChange', res) |
|
|
|
if (res.available) { |
|
|
|
this.startBluetoothDevicesDiscovery() |
|
|
@@ -88,7 +88,6 @@ Page({ |
|
|
|
allowDuplicatesKey: true, |
|
|
|
services: [ |
|
|
|
"FFE0", |
|
|
|
// "F0A0", // BM30广播模块需加上,如使用连接模块可忽略 |
|
|
|
], |
|
|
|
success: (res) => { |
|
|
|
console.log('startBluetoothDevicesDiscovery success', res) |
|
|
@@ -113,22 +112,13 @@ Page({ |
|
|
|
const data = {} |
|
|
|
// console.log(device) |
|
|
|
// console.log(ab2hex(device.advertisData)) |
|
|
|
// 此处判断是否BM30广播模块,如使用连接模块请删除此 if ,只保留 else 内容 |
|
|
|
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) // 保存广播数据中的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 { |
|
|
@@ -244,31 +234,6 @@ Page({ |
|
|
|
success: (res) => { |
|
|
|
console.log('getBLEDeviceCharacteristics success', res.characteristics) |
|
|
|
|
|
|
|
// 这部分功能在插件的 initPlugin 中已实现,如需用到其中的 uuid 也可取消注释 |
|
|
|
// 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) |
|
|
|
|
|
|
@@ -287,63 +252,7 @@ Page({ |
|
|
|
this.toConnectWifi() |
|
|
|
},500) |
|
|
|
} else if (bleData.status == 1) { |
|
|
|
let payload = bleData.data //对应协议中的payload数据,可以自行解析该数据 |
|
|
|
// console.log(ab2hex(payload, ' ')) |
|
|
|
// console.log(ab2hex(bleData.completeData, ' ')) |
|
|
|
|
|
|
|
// 以体脂秤数据解析为例 |
|
|
|
switch (payload[0]) { |
|
|
|
/* |
|
|
|
* 例如: A7 00 0E 05 01 00 01 F4 10 19 7A---------50.0kg |
|
|
|
* 其中 01 00 01 F4 10 为 payload |
|
|
|
* 具体指令请根据协议解析 |
|
|
|
*/ |
|
|
|
case 0x01: |
|
|
|
case 0x02: |
|
|
|
let weightValue = (payload[1] << 16) | (payload[2] << 8) | payload[3] |
|
|
|
let decPoint = (payload[4] & 0xf0) >> 4 |
|
|
|
let unit = payload[4] & 0x0f |
|
|
|
// console.log("体重数值:" + weightValue) |
|
|
|
// console.log("小数点:" + decPoint) |
|
|
|
// console.log("单位:" + unit) |
|
|
|
if (unit == 1) { // 单位为斤 |
|
|
|
this._weight = weightValue / 2 |
|
|
|
} else { |
|
|
|
// ... 其他单位 |
|
|
|
} |
|
|
|
this._weight = weightValue / (decPoint * 10) // 除去小数点位数 |
|
|
|
break; |
|
|
|
// ... |
|
|
|
/* |
|
|
|
* 例如: A7 00 0E 03 07 02 30 4A 7A---------阻抗测量成功,阻抗 560Ω |
|
|
|
* 其中 07 02 30 为 payload |
|
|
|
* 具体指令请根据协议解析 |
|
|
|
*/ |
|
|
|
case 0x07: |
|
|
|
this._adc = (payload[1] << 8) | payload[2] |
|
|
|
break; |
|
|
|
case 0x0A: |
|
|
|
//测量完成 |
|
|
|
let bodyData = plugin.getBodyData(1, 20, 170, this._weight, this._adc) // 体脂秤数据解析 |
|
|
|
console.log("解析后的体脂数据: ", bodyData) |
|
|
|
console.log(util.getWeightDisplay(170, this._weight)) |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
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(bleData.completeData, ' ') |
|
|
|
} |
|
|
|
} else { |
|
|
|
data[`chs[${idx}]`] = { |
|
|
|
uuid: characteristic.characteristicId, |
|
|
|
value: ab2hex(bleData.completeData, ' ') |
|
|
|
} |
|
|
|
} |
|
|
|
this.setData(data) |
|
|
|
console.log('A7-----^') |
|
|
|
} else if (bleData.status == 2) { |
|
|
|
console.log('A6-----^') |
|
|
|
} |