156 lines
4.1 KiB
JavaScript
156 lines
4.1 KiB
JavaScript
|
|
const util = require("../../utils/util");
|
|||
|
|
const {
|
|||
|
|
inArray,
|
|||
|
|
ab2hex
|
|||
|
|
} = util
|
|||
|
|
|
|||
|
|
Page({
|
|||
|
|
data: {
|
|||
|
|
devices: [],
|
|||
|
|
deviceId: null,
|
|||
|
|
connected: false,
|
|||
|
|
weight: "",
|
|||
|
|
imp: "",
|
|||
|
|
tetx: ''
|
|||
|
|
},
|
|||
|
|
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, //是否允许重复上报同一设备
|
|||
|
|
success: (res) => {
|
|||
|
|
console.log('startBluetoothDevicesDiscovery success', res)
|
|||
|
|
this.onBluetoothDeviceFound()
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
// 停止搜寻附近的蓝牙外围设备
|
|||
|
|
stopBluetoothDevicesDiscovery() {
|
|||
|
|
wx.stopBluetoothDevicesDiscovery()
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 找到新设备的事件
|
|||
|
|
onBluetoothDeviceFound() {
|
|||
|
|
let that = this
|
|||
|
|
wx.onBluetoothDeviceFound((res) => {
|
|||
|
|
res.devices.forEach(device => {
|
|||
|
|
if (!device.name && !device.localName) {
|
|||
|
|
let value = ab2hex(device.advertisData, "")
|
|||
|
|
let id = value.substring(12, 16)
|
|||
|
|
if (value.indexOf('c0') !== -1 && id == '0002') {
|
|||
|
|
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()
|
|||
|
|
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,
|
|||
|
|
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: "",
|
|||
|
|
text: ''
|
|||
|
|
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 断开蓝牙模块
|
|||
|
|
*/
|
|||
|
|
closeBluetoothAdapter() {
|
|||
|
|
wx.stopBluetoothDevicesDiscovery();
|
|||
|
|
wx.closeBluetoothAdapter()
|
|||
|
|
this._discoveryStarted = false
|
|||
|
|
wx.showToast({
|
|||
|
|
title: '结束流程',
|
|||
|
|
icon: 'none'
|
|||
|
|
})
|
|||
|
|
this.setData({
|
|||
|
|
devices: [],
|
|||
|
|
weight: "",
|
|||
|
|
imp: "",
|
|||
|
|
text: ''
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
});
|