fix:蓝牙bug解决
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { buildBleDeviceName } from '@/utils/ble-device-name'
|
||||
import {
|
||||
findDeviceInBleScan,
|
||||
getConnectedBleDeviceByMac,
|
||||
isBluetoothApiSupported,
|
||||
scanBluetoothDevices,
|
||||
} from '@/utils/bluetooth'
|
||||
@@ -17,7 +18,8 @@ import type { DeviceListItem } from './device.types'
|
||||
|
||||
let syncing = false
|
||||
|
||||
const PRESENCE_SCAN_MS = 8000
|
||||
/** 关机后广播马上消失,缩短扫描窗口以便更快判定离线 */
|
||||
const PRESENCE_SCAN_MS = 4500
|
||||
|
||||
const PRESENCE_DEBUG = import.meta.env.DEV
|
||||
|| import.meta.env.VITE_BLE_DEBUG === 'true'
|
||||
@@ -35,6 +37,27 @@ function wasDeviceOnlineInCloud(device: DeviceListItem): boolean {
|
||||
return device.status === '1' && device.bluetoothConnected === '1'
|
||||
}
|
||||
|
||||
function resolveBleName(mac?: string): string | undefined {
|
||||
if (!mac) return undefined
|
||||
try {
|
||||
return buildBleDeviceName(mac)
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
function findDeviceInPresenceScan(
|
||||
device: DeviceListItem,
|
||||
scanned: UniApp.BluetoothDeviceInfo[],
|
||||
): UniApp.BluetoothDeviceInfo | null {
|
||||
if (!device.mac) return null
|
||||
return findDeviceInBleScan(
|
||||
{ mac: device.mac, name: resolveBleName(device.mac) },
|
||||
scanned,
|
||||
{ strictName: true },
|
||||
)
|
||||
}
|
||||
|
||||
function applyPresence(
|
||||
device: DeviceListItem,
|
||||
blePresent: boolean,
|
||||
@@ -103,7 +126,7 @@ function shouldSyncOfflineToCloud(before: DeviceListItem, after: DeviceListItem)
|
||||
return false
|
||||
}
|
||||
|
||||
/** 通过 BLE 扫描刷新设备在线状态(仅 MAC 精确匹配,不用缓存广播) */
|
||||
/** 通过 BLE 扫描刷新设备在线状态;扫描未命中即刻离线 */
|
||||
export async function refreshDevicePresence(
|
||||
devices: DeviceListItem[],
|
||||
): Promise<DeviceListItem[]> {
|
||||
@@ -119,6 +142,17 @@ export async function refreshDevicePresence(
|
||||
syncing = true
|
||||
|
||||
try {
|
||||
// 先看 GATT 是否仍连接:已断开的在线设备可立刻标离线,不必等完整扫描
|
||||
const connectedFlags = await Promise.all(
|
||||
targets.map(async (device) => {
|
||||
if (!device.mac || !device.isOnline) return false
|
||||
const connected = await getConnectedBleDeviceByMac(device.mac, {
|
||||
name: resolveBleName(device.mac),
|
||||
})
|
||||
return !!connected
|
||||
}),
|
||||
)
|
||||
|
||||
const scanned = await scanBluetoothDevices({ timeoutMs: PRESENCE_SCAN_MS })
|
||||
logPresence('scan result', {
|
||||
count: scanned.length,
|
||||
@@ -129,19 +163,19 @@ export async function refreshDevicePresence(
|
||||
|
||||
const nextDevices = devices.map((device) => {
|
||||
if (!device.deviceId) return device
|
||||
const isTarget = targets.some((item) => sameId(item.deviceId, device.deviceId))
|
||||
if (!isTarget) return device
|
||||
const targetIndex = targets.findIndex((item) => sameId(item.deviceId, device.deviceId))
|
||||
if (targetIndex < 0) return device
|
||||
|
||||
const found = device.mac
|
||||
? findDeviceInBleScan({ mac: device.mac }, scanned, { strictName: true })
|
||||
: null
|
||||
const present = !!found
|
||||
const found = findDeviceInPresenceScan(device, scanned)
|
||||
const gattConnected = connectedFlags[targetIndex]
|
||||
const present = !!found || gattConnected
|
||||
logPresence('device presence', {
|
||||
name: device.name,
|
||||
mac: device.mac,
|
||||
present,
|
||||
foundId: found?.deviceId,
|
||||
bleName: device.mac ? buildBleDeviceName(device.mac) : '',
|
||||
found: !!found,
|
||||
gattConnected,
|
||||
bleName: resolveBleName(device.mac),
|
||||
})
|
||||
const next = applyPresence(device, present)
|
||||
if (shouldSyncOfflineToCloud(device, next)) {
|
||||
@@ -152,9 +186,7 @@ export async function refreshDevicePresence(
|
||||
|
||||
void Promise.allSettled(
|
||||
targets.map(async (device) => {
|
||||
const found = device.mac
|
||||
? findDeviceInBleScan({ mac: device.mac }, scanned, { strictName: true })
|
||||
: null
|
||||
const found = findDeviceInPresenceScan(device, scanned)
|
||||
if (found) {
|
||||
await reportPresenceOnlineToCloud(device)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user