fix:蓝牙bug解决

This commit is contained in:
李伊帆
2026-10-29 10:09:43 +08:00
parent 91bca314ba
commit 98402548eb
28 changed files with 1018 additions and 186 deletions

View File

@@ -3,10 +3,11 @@ const lastSeenAt = new Map<string, number>()
/** 已向云端上报离线的设备,避免轮询重复写库 */
const cloudOfflineReported = new Set<string>()
/** 连续扫描未命中次数达到该值后才判定离线 */
const OFFLINE_AFTER_MISSES = 2
/** 最近一次 BLE 命中后的宽限期(避免切换页面时瞬时扫不到) */
const ONLINE_GRACE_MS = 25_000
/**
* 扫描未命中次数达到该值后判定离线。
* 关机后广播立刻消失,1 次完整扫描未命中即可标离线(不再做长宽限)。
*/
const OFFLINE_AFTER_MISSES = 1
function deviceKey(deviceId: string | number | undefined): string {
return deviceId != null ? String(deviceId) : ''
@@ -26,21 +27,14 @@ export function recordPresenceMiss(deviceId: string | number) {
missCounts.set(key, (missCounts.get(key) ?? 0) + 1)
}
/** 是否应将设备展示为离线(需连续未命中且超出宽限期) */
/** 是否应将设备展示为离线:完整扫描未命中即离线 */
export function shouldMarkDeviceOffline(
deviceId: string | number,
wasOnline = false,
_wasOnline = false,
): boolean {
if (!wasOnline) return true
const key = deviceKey(deviceId)
const misses = missCounts.get(key) ?? 0
if (misses < OFFLINE_AFTER_MISSES) return false
const lastSeen = lastSeenAt.get(key) ?? 0
if (lastSeen && Date.now() - lastSeen < ONLINE_GRACE_MS) return false
return true
return misses >= OFFLINE_AFTER_MISSES
}
export function resetPresenceState(deviceId?: string | number) {
@@ -67,3 +61,7 @@ export function markPresenceOfflineReportedToCloud(deviceId: string | number) {
if (!key) return
cloudOfflineReported.add(key)
}
export function getLastPresenceSeenAt(deviceId: string | number): number {
return lastSeenAt.get(deviceKey(deviceId)) ?? 0
}