修复八电极蓝牙对接流程

This commit is contained in:
qcl_123 2026-03-17 09:51:59 +08:00
parent a161319690
commit 4ff1bb8e67
6 changed files with 318 additions and 314 deletions

View File

@ -1,321 +1,325 @@
const util = require("../../utils/util"); const util = require("../../utils/util");
const { const {
inArray, inArray,
ab2hex ab2hex
} = util } = util
const plugin = requirePlugin("sdkPlugin").AiLink; const plugin = requirePlugin("sdkPlugin").AiLink;
Page({ Page({
data: { data: {
devices: [], devices: [],
connected: false, connected: false,
cmd: '', cmd: '',
name: '', name: '',
weight: "", weight: "",
height: "", height: "",
text: "", text: "",
imp: "", imp: "",
uuid1: "", uuid1: "",
uuid2: "", uuid2: "",
uuid3: "", uuid3: "",
deviceId: null, deviceId: null,
}, },
onLoad: function() {}, onLoad: function() {},
// 初始化蓝牙模块 // 初始化蓝牙模块
openBluetoothAdapter() { openBluetoothAdapter() {
wx.openBluetoothAdapter({ wx.openBluetoothAdapter({
success: (res) => { success: (res) => {
console.log('openBluetoothAdapter success', res) console.log('openBluetoothAdapter success', res)
wx.showToast({ wx.showToast({
title: '蓝牙连接中', title: '蓝牙连接中',
icon: "none" icon: "none"
}) })
this.startBluetoothDevicesDiscovery() this.startBluetoothDevicesDiscovery()
}, },
fail: (res) => { fail: (res) => {
if (res.errCode === 10001) { if (res.errCode === 10001) {
wx.showToast({ wx.showToast({
title: '请打开蓝牙', title: '请打开蓝牙',
icon: "none" icon: "none"
}) })
// 监听本机蓝牙状态变化的事件 // 监听本机蓝牙状态变化的事件
wx.onBluetoothAdapterStateChange((res) => { wx.onBluetoothAdapterStateChange((res) => {
console.log('onBluetoothAdapterStateChange', res) console.log('onBluetoothAdapterStateChange', res)
if (res.available) { if (res.available) {
this.startBluetoothDevicesDiscovery() this.startBluetoothDevicesDiscovery()
} }
}) })
} }
} }
}) })
}, },
// 开始搜寻附近的蓝牙外围设备 // 开始搜寻附近的蓝牙外围设备
startBluetoothDevicesDiscovery() { startBluetoothDevicesDiscovery() {
if (this._discoveryStarted) { if (this._discoveryStarted) {
return return
} }
this._discoveryStarted = true this._discoveryStarted = true
wx.startBluetoothDevicesDiscovery({ wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true, allowDuplicatesKey: true,
interval: 1000, //上报设备的间隔 interval: 1000, //上报设备的间隔
services: [ services: [
"FFE0", "FFE0",
], ],
success: (res) => { success: (res) => {
console.log('startBluetoothDevicesDiscovery success', res) console.log('startBluetoothDevicesDiscovery success', res)
this.onBluetoothDeviceFound() this.onBluetoothDeviceFound()
}, },
}) })
}, },
// 停止搜寻附近的蓝牙外围设备 // 停止搜寻附近的蓝牙外围设备
stopBluetoothDevicesDiscovery() { stopBluetoothDevicesDiscovery() {
wx.stopBluetoothDevicesDiscovery() wx.stopBluetoothDevicesDiscovery()
}, },
// 找到新设备的事件 // 找到新设备的事件
onBluetoothDeviceFound() { onBluetoothDeviceFound() {
wx.onBluetoothDeviceFound((res) => { wx.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => { res.devices.forEach(device => {
if (!device.name && !device.localName) { if (!device.name && !device.localName) {
return return
} }
if (device.name.indexOf('AiLink_') != -1) { console.log("name", device.name)
wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后停止搜寻附近的蓝牙 if (device.name.indexOf('AiLink_') != -1 || device.name.toLowerCase().indexOf(
const foundDevices = this.data.devices 'pcf01') != -1) {
const idx = inArray(foundDevices, 'deviceId', device.deviceId) wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后停止搜寻附近的蓝牙
const data = {} const foundDevices = this.data.devices
let buff = device.advertisData.slice(-6) const idx = inArray(foundDevices, 'deviceId', device.deviceId)
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址 const data = {}
let tempMac = Array.from(device.mac) let buff = device.advertisData.slice(-6)
tempMac.reverse() device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
device.macAddr = ab2hex(tempMac, ':').toUpperCase() let tempMac = Array.from(device.mac)
if (idx === -1) { tempMac.reverse()
data[`devices[${foundDevices.length}]`] = device device.macAddr = ab2hex(tempMac, ':').toUpperCase()
} else { if (idx === -1) {
data[`devices[${idx}]`] = device data[`devices[${foundDevices.length}]`] = device
} } else {
this.setData(data) data[`devices[${idx}]`] = device
} }
}) this.setData(data)
}) }
}, })
// 连接低功耗蓝牙设备 })
createBLEConnection(e) { },
wx.showLoading({ // 连接低功耗蓝牙设备
title: '连接中', createBLEConnection(e) {
}) wx.showLoading({
const ds = e.currentTarget.dataset title: '连接中',
const index = ds.index })
this._device = this.data.devices[index] const ds = e.currentTarget.dataset
const deviceId = ds.deviceId const index = ds.index
const name = ds.name this._device = this.data.devices[index]
this.mac = ds.mac const deviceId = ds.deviceId
wx.createBLEConnection({ const name = ds.name
deviceId, this.mac = ds.mac
success: (res) => { wx.createBLEConnection({
this.setData({ deviceId,
connected: true, success: (res) => {
name, this.setData({
deviceId, connected: true,
}) name,
console.log("createBLEConnection:success") deviceId,
this.onBLEConnectionStateChange() })
this.getBLEDeviceServices(deviceId) console.log("createBLEConnection:success")
}, this.onBLEConnectionStateChange()
fail: res => { this.getBLEDeviceServices(deviceId)
wx.hideLoading() },
wx.showToast({ fail: res => {
title: '连接失败', wx.hideLoading()
icon: 'none' wx.showToast({
}) title: '连接失败',
} icon: 'none'
}) })
}, }
//监听蓝牙连接状态 })
onBLEConnectionStateChange() { },
wx.onBLEConnectionStateChange((res) => { //监听蓝牙连接状态
if (!res.connected) { onBLEConnectionStateChange() {
setTimeout(() => { wx.onBLEConnectionStateChange((res) => {
wx.showToast({ if (!res.connected) {
title: '连接已断开', setTimeout(() => {
icon: 'none' wx.showToast({
}) title: '连接已断开',
}, 500) icon: 'none'
this.setData({ })
connected: false, }, 500)
devices: [], this.setData({
weight: "", connected: false,
height: "", devices: [],
text: "", weight: "",
imp: "" height: "",
}) text: "",
} imp: ""
}) })
}, }
})
},
// 获取蓝牙设备的 serviceId // 获取蓝牙设备的 serviceId
getBLEDeviceServices(deviceId) { getBLEDeviceServices(deviceId) {
wx.getBLEDeviceServices({ wx.getBLEDeviceServices({
deviceId, deviceId,
success: (res) => { success: (res) => {
for (let i = 0; i < res.services.length; i++) { for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) { if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) {
wx.showLoading({ wx.showLoading({
title: '获取设备的UUID成功', title: '获取设备的UUID成功',
}) })
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid) this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
return return
} }
} }
} }
}) })
}, },
// 获取蓝牙设备某个服务中所有特征值(characteristic) // 获取蓝牙设备某个服务中所有特征值(characteristic)
/** /**
* read: true读,write: true写,notify: true广播 * read: true读,write: true写,notify: true广播
*/ */
getBLEDeviceCharacteristics(deviceId, serviceId) { getBLEDeviceCharacteristics(deviceId, serviceId) {
let that = this let that = this
that._deviceId = deviceId that._deviceId = deviceId
that._serviceId = serviceId that._serviceId = serviceId
that._device.serviceId = serviceId that._device.serviceId = serviceId
wx.hideLoading() wx.hideLoading()
wx.getBLEDeviceCharacteristics({ wx.getBLEDeviceCharacteristics({
deviceId, deviceId,
serviceId, serviceId,
success: (res) => { success: (res) => {
console.log('getBLEDeviceCharacteristics success', res.characteristics) console.log('getBLEDeviceCharacteristics success', res.characteristics)
for (let i = 0; i < res.characteristics.length; i++) { for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i]; let item = res.characteristics[i];
if (item.uuid.indexOf('0000FFE1') != -1) { if (item.uuid.indexOf('0000FFE1') != -1) {
that.data.uuid1 = item.uuid //下发数据 that.data.uuid1 = item.uuid //下发数据
} else if (item.uuid.indexOf('0000FFE2') != -1) { } else if (item.uuid.indexOf('0000FFE2') != -1) {
that.data.uuid2 = item.uuid //监听数据 that.data.uuid2 = item.uuid //监听数据
} else if (item.uuid.indexOf('0000FFE3') != -1) { } else if (item.uuid.indexOf('0000FFE3') != -1) {
that.data.uuid3 = item.uuid //写入设置 that.data.uuid3 = item.uuid //写入设置
} }
} }
// 打开监听 // 打开监听
wx.notifyBLECharacteristicValueChange({ wx.notifyBLECharacteristicValueChange({
deviceId, deviceId,
serviceId, serviceId,
characteristicId: that.data.uuid2, characteristicId: that.data.uuid2,
state: true, state: true,
}) })
wx.notifyBLECharacteristicValueChange({ wx.notifyBLECharacteristicValueChange({
deviceId, deviceId,
serviceId, serviceId,
characteristicId: that.data.uuid3, characteristicId: that.data.uuid3,
state: true, state: true,
}) })
// 初始化插件 // 初始化插件
plugin.initPlugin(res.characteristics, that._device) plugin.initPlugin(res.characteristics, that._device)
wx.onBLECharacteristicValueChange((characteristic) => { wx.onBLECharacteristicValueChange((characteristic) => {
let bleData = plugin.parseBleData(characteristic.value) let bleData = plugin.parseBleData(characteristic.value)
let dw1 = "kg" let dw1 = "kg"
if (bleData.status == 0) { if (bleData.status == 0) {
// 发送 男22岁185 // 发送 男22岁185
let A = 22 let A = 22
let H = 185 let H = 185
let sex = "0x01" let sex = "0x01"
let age = "0x" + A.toString(16) let age = "0x" + A.toString(16)
let height = "0x" + H.toString(16) let height = "0x" + H.toString(16)
let arr = [0x01, parseInt(sex), parseInt(age), parseInt(height),0x00] let arr = [0x01, parseInt(sex), parseInt(age), parseInt(height),
plugin.sendDataOfA7(arr) 0x00
console.log("握手成功", arr) ]
} else if (bleData.status == 1) { plugin.sendDataOfA7(arr)
let payload = ab2hex(bleData.data, '') console.log("握手成功", arr)
let type = payload.substring(0, 2) } else if (bleData.status == 1) {
let typeInfo = payload.substring(4, 6) let payload = ab2hex(bleData.data, '')
console.log("payload", payload) let type = payload.substring(0, 2)
if (type == "10" || type == "40") { //体脂模式 let typeInfo = payload.substring(4, 6)
let data = parseInt(payload.substring(6, 12), 16) console.log("payload", payload)
let num = payload.substring(12, 13) if (type == "10" || type == "40") { //体脂模式
let dw = payload.substring(13, 14) let data = parseInt(payload.substring(6, 12), 16)
if (dw == "1") { let num = payload.substring(12, 13)
dw1 = "斤" let dw = payload.substring(13, 14)
} if (dw == "1") {
if (num == "1") { dw1 = "斤"
data = data / 10 }
} if (num == "1") {
if (num == "2") { data = data / 10
data = data / 100 }
} if (num == "2") {
if (num == "3") { data = data / 100
data = data / 1000 }
} if (num == "3") {
if (typeInfo == "01") { data = data / 1000
that.setData({ }
weight: "实时体重是:" + data + dw1 if (typeInfo == "01") {
}) that.setData({
} weight: "实时体重是:" + data + dw1
if (typeInfo == "02") { })
that.setData({ }
weight: "稳定体重是:" + data + dw1 if (typeInfo == "02") {
}) that.setData({
} weight: "稳定体重是:" + data + dw1
} })
if (type == "14" || type == "41") { //身高模式 }
let height = parseInt(payload.substring(4, 8), 16) }
that.setData({ if (type == "14" || type == "41") { //身高模式
height: "身高是:" + height let height = parseInt(payload.substring(4, 8), 16)
}) that.setData({
} height: "身高是:" + height
if (type == "11") { //阻抗模式 })
if (typeInfo == "03" || typeInfo == "04") { }
let imp = parseInt(payload.substring(8, 12), 16) if (type == "11") { //阻抗模式
console.log("imp", payload, imp) if (typeInfo == "03" || typeInfo == "04") {
that.setData({ let imp = parseInt(payload.substring(8, 12), 16)
imp: "阻抗值:" + imp console.log("imp", payload, imp)
}) that.setData({
} imp: "阻抗值:" + imp
} })
} }
}) }
}, }
fail(res) { })
console.error('getBLEDeviceCharacteristics', res) },
} fail(res) {
}) console.error('getBLEDeviceCharacteristics', res)
}, }
})
},
/** /**
* 断开蓝牙模块 * 断开蓝牙模块
*/ */
closeBluetoothAdapter() { closeBluetoothAdapter() {
wx.closeBluetoothAdapter() wx.closeBluetoothAdapter()
this._discoveryStarted = false this._discoveryStarted = false
wx.showToast({ wx.showToast({
title: '断开蓝牙模块', title: '断开蓝牙模块',
icon: 'none' icon: 'none'
}) })
this.setData({ this.setData({
devices: [], devices: [],
weight: "", weight: "",
height: "", height: "",
text: "", text: "",
imp: "" imp: ""
}) })
}, },
// 断开与低功耗蓝牙设备的连接 // 断开与低功耗蓝牙设备的连接
closeBLEConnection() { closeBLEConnection() {
wx.closeBLEConnection({ wx.closeBLEConnection({
deviceId: this._deviceId deviceId: this._deviceId
}) })
wx.showToast({ wx.showToast({
title: '断开蓝牙连接', title: '断开蓝牙连接',
icon: 'none' icon: 'none'
}) })
this.setData({ this.setData({
connected: false, connected: false,
devices: [], devices: [],
text: "", text: "",
height: "", height: "",
weight: "", weight: "",
imp: "" imp: ""
}) })
}, },
}); });

BIN
bluetooth_demo_J01.zip Normal file

Binary file not shown.

BIN
bluetooth_demo_L01.zip Normal file

Binary file not shown.

BIN
bluetooth_demo_L08.zip Normal file

Binary file not shown.

View File

@ -291,11 +291,11 @@ Page({
}) })
} }
} }
if (typeInfo == "05") { // if (typeInfo == "05") {
let mcu = payload.substring(4, 6) // let mcu = payload.substring(4, 6)
let imp = parseInt(payload.substring(6, 14), 16) // let imp = parseInt(payload.substring(6, 14), 16)
console.log("阻抗类型:", payload, type, typeInfo, mcu,imp) //阻抗模式 // console.log("阻抗类型:", payload, type, typeInfo, mcu,imp) //阻抗模式
} // }
} }
if (type == "0f") { if (type == "0f") {
that.setData({ that.setData({

Binary file not shown.