|
|
@@ -259,9 +259,20 @@ export const useBleStore = defineStore('ble', {
|
|
|
})
|
|
|
// 永久注册设备发现监听器(不可反复 on/off,否则多次后系统丢失监听)
|
|
|
uni.onBluetoothDeviceFound((res) => {
|
|
|
- console.log("扫描结果",res)
|
|
|
const deviceCount = res && res.devices ? res.devices.length : 0
|
|
|
console.log(`[BLE Global] onBluetoothDeviceFound 触发, 设备数=${deviceCount}, s.onDeviceFound=${!!s.onDeviceFound}`)
|
|
|
+ // 立即在系统回调内转换 advertisData,避免原生 ArrayBuffer 被平台回收
|
|
|
+ if (res && res.devices) {
|
|
|
+ res.devices.forEach(device => {
|
|
|
+ if (device.advertisData && device.advertisData.byteLength > 0) {
|
|
|
+ const bytes = new Uint8Array(device.advertisData)
|
|
|
+ device._advertisDataHex = Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('')
|
|
|
+ console.log(`[BLE Scan] 广播数据: ${device._advertisDataHex}`)
|
|
|
+ } else {
|
|
|
+ device._advertisDataHex = ''
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
if (s.onDeviceFound) s.onDeviceFound(res)
|
|
|
})
|
|
|
s.bound = true
|
|
|
@@ -408,10 +419,23 @@ export const useBleStore = defineStore('ble', {
|
|
|
const onFound = (res) => {
|
|
|
if (finished) return
|
|
|
for (const d of res.devices) {
|
|
|
- if (devices.has(d.deviceId)) continue
|
|
|
+ const advertisDataHex = d._advertisDataHex || ''
|
|
|
+ const name = d.name || d.localName || ''
|
|
|
+
|
|
|
+ // 重复设备:如果本次带有 advertisData 但之前没有,补充更新
|
|
|
+ if (devices.has(d.deviceId)) {
|
|
|
+ if (advertisDataHex) {
|
|
|
+ const existing = this.scannedDevices.find(item => item.deviceId === d.deviceId)
|
|
|
+ if (existing && !existing.advertisData) {
|
|
|
+ existing.advertisData = advertisDataHex
|
|
|
+ console.log(`[BLE Scan] 补充广播数据: ${name || d.deviceId} -> ${advertisDataHex}`)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
devices.set(d.deviceId, d)
|
|
|
foundCount++
|
|
|
- const name = d.name || d.localName || ''
|
|
|
console.log(`[BLE Scan] 发现设备 #${foundCount}: name=${name}, deviceId=${d.deviceId}, RSSI=${d.RSSI}`)
|
|
|
const hit =
|
|
|
(deviceName && name === deviceName) ||
|
|
|
@@ -421,11 +445,12 @@ export const useBleStore = defineStore('ble', {
|
|
|
matched.push(d)
|
|
|
const exists = this.scannedDevices.find(item => item.deviceId === d.deviceId)
|
|
|
if (!exists) {
|
|
|
- console.log(`[BLE Scan] ✅ 设备匹配并加入列表: ${name || d.deviceId}`)
|
|
|
+ console.log(`[BLE Scan] ✅ 设备匹配并加入列表: ${name || d.deviceId}, 广播数据: ${advertisDataHex}`)
|
|
|
this.scannedDevices.push({
|
|
|
deviceId: d.deviceId,
|
|
|
name: name,
|
|
|
- RSSI: d.RSSI || ''
|
|
|
+ RSSI: d.RSSI || '',
|
|
|
+ advertisData: advertisDataHex
|
|
|
})
|
|
|
}
|
|
|
if (!returnAll) { finish(null, d); return }
|
|
|
@@ -549,7 +574,7 @@ export const useBleStore = defineStore('ble', {
|
|
|
|
|
|
/** 扫描 + 连接一步到位(支持 deviceId 直连优先,跳过扫描) */
|
|
|
async scanAndConnect(opt = {}) {
|
|
|
- const { deviceId: directId, deviceName, ...restOpt } = opt
|
|
|
+ const { deviceId: directId, deviceName, advertisData: passedAdvertisData, ...restOpt } = opt
|
|
|
|
|
|
// 策略:如果已有 deviceId,先尝试直连(不扫描),失败后回退扫描
|
|
|
if (directId) {
|
|
|
@@ -557,6 +582,10 @@ export const useBleStore = defineStore('ble', {
|
|
|
logger.info(`directConnect attempt, deviceId=${directId}`)
|
|
|
await this.connectDevice(directId)
|
|
|
this.device.name = deviceName || this.device.name || ''
|
|
|
+ // 存储 advertisData 作为跨平台设备标识
|
|
|
+ if (passedAdvertisData) {
|
|
|
+ this.device.advertisData = passedAdvertisData
|
|
|
+ }
|
|
|
logger.info('directConnect success, scan skipped')
|
|
|
return this.device
|
|
|
} catch (e) {
|
|
|
@@ -571,6 +600,8 @@ export const useBleStore = defineStore('ble', {
|
|
|
const device = await this.startScan(scanOpt)
|
|
|
await this.connectDevice(device.deviceId)
|
|
|
this.device.name = device.name || device.localName || ''
|
|
|
+ // 存储 advertisData:优先用扫描结果中的,其次用传入的
|
|
|
+ this.device.advertisData = device.advertisData || passedAdvertisData || ''
|
|
|
return this.device
|
|
|
},
|
|
|
|