185 lines
4.3 KiB
JavaScript
185 lines
4.3 KiB
JavaScript
const util = require("../../utils/util");
|
||
const {
|
||
inArray,
|
||
ab2hex
|
||
} = util
|
||
const plugin = requirePlugin("sdkPlugin").AiLink;
|
||
Page({
|
||
data: {
|
||
connected: false,
|
||
name: '',
|
||
weight: "",
|
||
height: "",
|
||
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: 1000, //上报设备的间隔
|
||
services: [
|
||
"F0A0",
|
||
],
|
||
success: (res) => {
|
||
this.onBluetoothDeviceFound()
|
||
},
|
||
})
|
||
},
|
||
// 停止搜寻附近的蓝牙外围设备
|
||
stopBluetoothDevicesDiscovery() {
|
||
wx.stopBluetoothDevicesDiscovery()
|
||
},
|
||
|
||
// 找到新设备的事件
|
||
onBluetoothDeviceFound() {
|
||
let that = this
|
||
wx.onBluetoothDeviceFound((res) => {
|
||
res.devices.forEach(device => {
|
||
if (!device.name && !device.localName) {
|
||
return
|
||
}
|
||
if (device.advertisServiceUUIDs[0].indexOf("F0A0") !== -1) {
|
||
let parseDataRes = plugin.parseBroadcastData(device.advertisData)
|
||
let analyzeData = plugin.analyzeBroadcastScaleData(parseDataRes)
|
||
let analyzeDataText = analyzeData.text
|
||
let data = analyzeData.data
|
||
const foundDevices = this.data.devices
|
||
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
|
||
const dataT = {}
|
||
let buffer = device.advertisData.slice(0, 8)
|
||
device.mac = new Uint8Array(buffer) // 保存广播数据中的mac地址,这是由于iOS不直接返回mac地址
|
||
let tempMac = Array.from(device.mac)
|
||
tempMac.reverse()
|
||
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
|
||
that.imp = dataT.adc
|
||
if (idx === -1) {
|
||
dataT[`devices[${foundDevices.length}]`] = device
|
||
} else {
|
||
dataT[`devices[${idx}]`] = device
|
||
}
|
||
this.setData(dataT)
|
||
console.log("analyzeData", analyzeData)
|
||
if (parseDataRes.status == 1) {
|
||
let dw1 = "kg"
|
||
let dwH = "CM"
|
||
// 体重单位
|
||
if (data.weightUnit == "1") {
|
||
dw1 = "斤"
|
||
}
|
||
if (data.weightUnit == "4") {
|
||
dw1 = "st:lb"
|
||
data.weight = 1 * data.weight + 5
|
||
}
|
||
if (data.weightUnit == "6") {
|
||
dw1 = "lb"
|
||
}
|
||
if (data.weightDecimal == "1") {
|
||
data.weight = data.weight / 10
|
||
}
|
||
if (data.weightDecimal == "2") {
|
||
data.weight = data.weight / 100
|
||
}
|
||
if (data.weightDecimal == "3") {
|
||
data.weight = data.weight / 1000
|
||
}
|
||
that.setData({
|
||
weight: analyzeData.text
|
||
})
|
||
// that.setData({
|
||
// weight: "您的体重是:" + data.weight + dw1
|
||
// })
|
||
//身高单位
|
||
// if (data.tempUnit == "1") {
|
||
// dwH = "FT"
|
||
// data.tempNegative = data.tempNegative * 2.54
|
||
// }
|
||
// that.setData({
|
||
// height: "您的身高是:" + data.tempNegative / 10 + dwH
|
||
// })
|
||
return
|
||
}
|
||
}
|
||
|
||
})
|
||
})
|
||
},
|
||
//监听蓝牙连接状态
|
||
onBLEConnectionStateChange() {
|
||
wx.onBLEConnectionStateChange((res) => {
|
||
if (!res.connected) {
|
||
wx.stopBluetoothDevicesDiscovery();
|
||
setTimeout(() => {
|
||
wx.showToast({
|
||
title: '连接已断开',
|
||
icon: 'none'
|
||
})
|
||
}, 500)
|
||
this.setData({
|
||
connected: false,
|
||
devices: [],
|
||
weight: "",
|
||
height: "",
|
||
text: "",
|
||
imp: ""
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 断开蓝牙模块
|
||
*/
|
||
closeBluetoothAdapter() {
|
||
wx.stopBluetoothDevicesDiscovery();
|
||
wx.closeBluetoothAdapter()
|
||
this._discoveryStarted = false
|
||
wx.showToast({
|
||
title: '结束流程',
|
||
icon: 'none'
|
||
})
|
||
this.setData({
|
||
devices: [],
|
||
weight: "",
|
||
height: "",
|
||
text: "",
|
||
imp: ""
|
||
})
|
||
},
|
||
});
|