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

@@ -0,0 +1,51 @@
import { compactMacAddress } from '@/utils/bluetooth'
const STORAGE_KEY = 'czzj_ble_device_id_by_mac'
const memory = new Map<string, string>()
function macKey(mac: string): string {
return compactMacAddress(mac)
}
function readStorage(): Record<string, string> {
try {
const raw = uni.getStorageSync(STORAGE_KEY)
if (raw && typeof raw === 'object') return raw as Record<string, string>
} catch {
// ignore
}
return {}
}
function writeStorage(map: Record<string, string>) {
try {
uni.setStorageSync(STORAGE_KEY, map)
} catch {
// ignore
}
}
/** 记住 MAC → 系统 ble deviceId(尤其 iOS UUID),加速二次重连 */
export function rememberBleDeviceId(mac: string, bleDeviceId: string) {
const key = macKey(mac)
const id = bleDeviceId.trim()
if (!key || key.length !== 12 || !id) return
memory.set(key, id)
const stored = readStorage()
if (stored[key] === id) return
stored[key] = id
writeStorage(stored)
}
export function recallBleDeviceId(mac: string): string | undefined {
const key = macKey(mac)
if (!key) return undefined
const fromMem = memory.get(key)
if (fromMem) return fromMem
const fromStore = readStorage()[key]
if (fromStore) {
memory.set(key, fromStore)
return fromStore
}
return undefined
}