BluetoothDemo/pages/PCL11/index.js

165 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-10-13 17:15:36 +08:00
const util = require("../../utils/util");
const {
inArray,
ab2hex
} = util
Page({
data: {
connected: false,
name: '',
weight: "",
imp: "",
devices: [],
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: 500, //上报设备的间隔
success: (res) => {
this.onBluetoothDeviceFound()
},
})
},
// 停止搜寻附近的蓝牙外围设备
stopBluetoothDevicesDiscovery() {
wx.stopBluetoothDevicesDiscovery()
},
// 找到新设备的事件
onBluetoothDeviceFound() {
let that = this
wx.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => {
device.advertisData = device.advertisData ? device.advertisData : ''
if (!device.name && !device.localName) {
let value = ab2hex(device.advertisData, "")
let id = value.substring(12, 16)
if (value.indexOf('c0') !== -1 && id == '0002') {
device.name = "PCL-体脂称"
let buff = device.advertisData.slice(-6)
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
let tempMac = Array.from(device.mac)
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
//
const foundDevices = this.data.devices
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
const dataT = {}
if (idx === -1) {
dataT[`devices[${foundDevices.length}]`] = device
} else {
dataT[`devices[${idx}]`] = device
}
this.setData(dataT)
let msg = parseInt(value.substring(16, 18), 16).toString(2)
let weight = parseInt(value.substring(4, 8), 16)
let type = msg.substring(5, 6) //0实时,1稳定
let num = msg.substring(3, 5) //小数点
let unit = msg.substring(1, 3) //单位
let dw1 = "kg"
if (unit == "10") {
dw1 = "lb"
}
if (num == "00") {
weight = parseInt(value.substring(4, 8), 16) / 10
}
if (num == "10") {
if (unit == "10") {
dw1 = "lb"
weight = parseInt(value.substring(4, 8), 16) / 10
} else {
weight = parseInt(value.substring(4, 8), 16) / 100
}
}
that.setData({
weight: "您的实时体重是:" + weight + dw1
})
if (type == '1') {
that.setData({
weight: "您的稳定体重是:" + weight + dw1
})
that.setData({
imp:"阻抗是:" + parseInt(value.substring(8, 12), 16) / 10
})
}
}
return
}
})
})
},
//监听蓝牙连接状态
onBLEConnectionStateChange() {
wx.onBLEConnectionStateChange((res) => {
if (!res.connected) {
wx.stopBluetoothDevicesDiscovery();
setTimeout(() => {
wx.showToast({
title: '连接已断开',
icon: 'none'
})
}, 500)
this.setData({
connected: false,
devices: [],
weight: "",
imp: ""
})
}
})
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
wx.stopBluetoothDevicesDiscovery();
wx.closeBluetoothAdapter()
this._discoveryStarted = false
wx.showToast({
title: '结束流程',
icon: 'none'
})
this.setData({
devices: [],
weight: "",
imp: ""
})
},
});