fix:蓝牙bug解决
This commit is contained in:
@@ -244,15 +244,41 @@ function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
/** 建立 BLE 连接;若设备已连接则视为成功 */
|
||||
export async function ensureBleConnection(bleDeviceId: string): Promise<void> {
|
||||
/** 指定 deviceId 是否已在系统已连接列表中 */
|
||||
export async function isBleDeviceConnected(bleDeviceId: string): Promise<boolean> {
|
||||
if (!bleDeviceId || typeof uni.getConnectedBluetoothDevices !== 'function') return false
|
||||
try {
|
||||
const res = await new Promise<UniApp.GetConnectedBluetoothDevicesSuccess>((resolve, reject) => {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success: resolve,
|
||||
fail: reject,
|
||||
})
|
||||
})
|
||||
return (res.devices ?? []).some((item) => item.deviceId === bleDeviceId)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立 BLE 连接;已连接则直接返回,避免重复 createBLEConnection
|
||||
*(部分机型对已连接设备再 create 会闪断,导致连上又断开的循环)
|
||||
*/
|
||||
export async function ensureBleConnection(
|
||||
bleDeviceId: string,
|
||||
options: { settleMs?: number } = {},
|
||||
): Promise<void> {
|
||||
if (await isBleDeviceConnected(bleDeviceId)) return
|
||||
|
||||
try {
|
||||
await createBleConnection(bleDeviceId)
|
||||
} catch (err) {
|
||||
if (!isBleAlreadyConnectedError(err)) throw err
|
||||
return
|
||||
}
|
||||
// Android / 微信小程序连接后 GATT 表需短暂就绪
|
||||
await delay(400)
|
||||
const settleMs = options.settleMs ?? 280
|
||||
if (settleMs > 0) await delay(settleMs)
|
||||
}
|
||||
|
||||
export function createBleConnection(bleDeviceId: string): Promise<void> {
|
||||
@@ -305,13 +331,16 @@ export async function releaseBleDevice(bleDeviceId?: string): Promise<void> {
|
||||
await closeBleConnection(bleDeviceId).catch(() => {})
|
||||
}
|
||||
|
||||
/** 从系统已连接 BLE 列表中按 MAC 查找(GATT 已连接时扫描往往找不到设备) */
|
||||
/** 从系统已连接 BLE 列表中按 MAC / 广播名 / 已知 deviceId 查找 */
|
||||
export async function getConnectedBleDeviceByMac(
|
||||
mac: string,
|
||||
options: { name?: string; preferredBleId?: string } = {},
|
||||
): Promise<UniApp.BluetoothDeviceInfo | null> {
|
||||
if (typeof uni.getConnectedBluetoothDevices !== 'function') return null
|
||||
const normalizedMac = normalizeMacAddress(mac)
|
||||
if (!normalizedMac) return null
|
||||
const targetName = options.name?.trim()
|
||||
const preferredBleId = options.preferredBleId?.trim()
|
||||
if (!normalizedMac && !targetName && !preferredBleId) return null
|
||||
|
||||
try {
|
||||
const res = await new Promise<UniApp.GetConnectedBluetoothDevicesSuccess>((resolve, reject) => {
|
||||
@@ -321,7 +350,10 @@ export async function getConnectedBleDeviceByMac(
|
||||
})
|
||||
})
|
||||
for (const device of res.devices ?? []) {
|
||||
if (deviceMatchesMac(device, normalizedMac)) return device
|
||||
if (preferredBleId && device.deviceId === preferredBleId) return device
|
||||
if (normalizedMac && deviceMatchesMac(device, normalizedMac)) return device
|
||||
// iOS deviceId 非 MAC,需靠标准广播名识别
|
||||
if (targetName && deviceMatchesName(device, targetName)) return device
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
@@ -376,7 +408,12 @@ export async function scanForBleDeviceByMac(
|
||||
matched = device
|
||||
return true
|
||||
}
|
||||
if (!normalizedMac && targetName && deviceMatchesName(device, targetName)) {
|
||||
// 有 MAC 时也按广播名匹配(iOS deviceId 是 UUID)
|
||||
if (targetName && deviceMatchesNameStrict(device, targetName)) {
|
||||
matched = device
|
||||
return true
|
||||
}
|
||||
if (targetName && deviceMatchesName(device, targetName)) {
|
||||
matched = device
|
||||
return true
|
||||
}
|
||||
@@ -458,7 +495,7 @@ export async function scanBluetoothDevices(
|
||||
return [...collected.values()]
|
||||
}
|
||||
|
||||
/** 在扫描结果中匹配设备(有 MAC 时仅按 MAC 匹配) */
|
||||
/** 在扫描结果中匹配设备(优先 MAC;iOS 无 MAC 命中时回退广播名) */
|
||||
export function findDeviceInBleScan(
|
||||
target: { mac?: string; name?: string },
|
||||
scanned: UniApp.BluetoothDeviceInfo[],
|
||||
@@ -471,7 +508,6 @@ export function findDeviceInBleScan(
|
||||
for (const device of scanned) {
|
||||
if (deviceMatchesMac(device, normalizedMac)) return device
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
if (!targetName) return null
|
||||
|
||||
Reference in New Issue
Block a user