fix(ble): 停搜索先同步翻转状态再停原生扫描,消除等待回调的卡顿
- stopSearch:setSearching(false) 提到 stopBluetoothDevicesDiscovery 之前, searchSessionId 已递增、事件已关闭,先翻转状态是安全的; 部分机型原生停扫回调慢,等它完成才更新 UI 会让按钮长时间停在 "搜索中"并串行阻塞连接等后续操作 - connectDevice:仅在搜索进行中才停搜索,避免每次连接都白白 调一次原生停扫接口
This commit is contained in:
parent
b8937d942f
commit
b0ba12034c
|
|
@ -677,13 +677,16 @@ export function createBluetoothDiscovery(options = {}) {
|
||||||
searchSessionId += 1
|
searchSessionId += 1
|
||||||
acceptingDeviceEvents = false
|
acceptingDeviceEvents = false
|
||||||
|
|
||||||
await stopDiscoverySilently()
|
// 先同步翻转搜索态,再异步停原生扫描:
|
||||||
|
// stopBluetoothDevicesDiscovery 在部分机型回调慢,若等它完成才 setSearching(false),
|
||||||
// 暂停搜索保留已发现的设备数据,不做 resetDeviceMap
|
// 期间按钮仍显示"搜索中"、连接/二次搜索等后续操作全部被串行拖住(感知为卡顿)。
|
||||||
|
// searchSessionId 已递增、事件已关闭,翻转状态不会引入新的扫描事件
|
||||||
setSearching(false)
|
setSearching(false)
|
||||||
if (showStopToast) {
|
if (showStopToast) {
|
||||||
showToast('已停止搜索')
|
showToast('已停止搜索')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await stopDiscoverySilently()
|
||||||
}
|
}
|
||||||
|
|
||||||
function pickPreferredDeviceId(deviceId) {
|
function pickPreferredDeviceId(deviceId) {
|
||||||
|
|
@ -717,7 +720,11 @@ export function createBluetoothDiscovery(options = {}) {
|
||||||
async function connectDevice(deviceId) {
|
async function connectDevice(deviceId) {
|
||||||
await ensureInitialized()
|
await ensureInitialized()
|
||||||
|
|
||||||
await stopSearch(false)
|
// 搜索进行中点击"网络配置":立即停搜索(状态同步翻转,不等原生回调),
|
||||||
|
// 否则 createBLEConnection 会被扫描信道占用拖慢甚至失败
|
||||||
|
if (isSearching) {
|
||||||
|
await stopSearch(false)
|
||||||
|
}
|
||||||
|
|
||||||
const targetDeviceId = pickPreferredDeviceId(deviceId)
|
const targetDeviceId = pickPreferredDeviceId(deviceId)
|
||||||
if (!targetDeviceId) {
|
if (!targetDeviceId) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue