BluetoothDemo/bluetooth_demo_L01/pages/index/index.js

165 lines
5.5 KiB
JavaScript

const util = require("../../utils/util");
const {
inArray,
ab2hex
} = util
const plugin = requirePlugin("sdkPlugin").AiLink;
Page({
data: {
devices: [],
connected: false,
weight: "",
imp: "",
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, //是否允许重复上报同一设备
services: [ //要搜索蓝牙设备主 service 的 uuid 列表
"F0A0",
],
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) {
return
}
if (device.advertisServiceUUIDs[0].indexOf("F0A0") !== -1) {
let value = ab2hex(device.advertisData)
let parseDataRes = plugin.parseBroadcastData(device.advertisData)
let analyzeData = plugin.analyzeBroadcastScaleData(parseDataRes)
let analyzeDataText = analyzeData.text
let data = analyzeData.data
const dataT = {}
const foundDevices = this.data.devices
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
let buffer = device.advertisData.slice(0, 8)
device.mac = new Uint8Array(buffer)
let tempMac = Array.from(device.mac)
tempMac.reverse()
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
if (idx === -1) {
dataT[`devices[${foundDevices.length}]`] = device
} else {
dataT[`devices[${idx}]`] = device
}
this.setData(dataT)
if (parseDataRes.status == 1) {
let dw1 = "kg"
if (data.weightUnit == "1") {
dw1 = "斤"
}
if (data.weightUnit == "4") {
dw1 = "st:lb"
data = 1 * data + 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
})
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: ""
})
},
});