|
|
@@ -51,6 +51,7 @@ function _getShared() {
|
|
|
onDeviceFound: null, // 当前扫描的设备发现回调
|
|
|
scanStartHistory: [], // 近期 startDiscovery 调用时间戳,用于检测 Android 限流
|
|
|
intentionalDisconnect: false, // 标记主动断开,防止系统回调误触发重连
|
|
|
+ _connectedBeforeScan: false, // 标记扫描前是否处于已连接状态
|
|
|
parser: null,
|
|
|
config: { ...DEFAULT_CONFIG }
|
|
|
}
|
|
|
@@ -137,10 +138,23 @@ export const useBleStore = defineStore('ble', {
|
|
|
// ============== 内部状态管理 ==============
|
|
|
_setState(s) {
|
|
|
if (this.bleState === s) return
|
|
|
+ const prevState = this.bleState
|
|
|
this.bleState = s
|
|
|
- this.linked = (s === BLE_STATE.READY_COMM)
|
|
|
+ // 进入扫描状态时,如果当前有活跃连接,不应丢失 linked 状态
|
|
|
+ if (s === BLE_STATE.SCANNING && prevState === BLE_STATE.READY_COMM) {
|
|
|
+ _S()._connectedBeforeScan = true
|
|
|
+ // linked 保持 true,仅更新 searching
|
|
|
+ } else if (s === BLE_STATE.READY && _S()._connectedBeforeScan && this.device) {
|
|
|
+ // 扫描结束恢复连接状态:物理连接仍存活
|
|
|
+ _S()._connectedBeforeScan = false
|
|
|
+ this.bleState = BLE_STATE.READY_COMM
|
|
|
+ this.linked = true
|
|
|
+ } else {
|
|
|
+ _S()._connectedBeforeScan = false
|
|
|
+ this.linked = (s === BLE_STATE.READY_COMM)
|
|
|
+ }
|
|
|
this.searching = (s === BLE_STATE.SCANNING)
|
|
|
- logger.info(`[instanceId=${_instanceId()}] state ->`, s)
|
|
|
+ logger.info(`[instanceId=${_instanceId()}] state ->`, s, this.linked ? '(linked)' : '')
|
|
|
},
|
|
|
|
|
|
// ============== 初始化 / 释放 ==============
|
|
|
@@ -398,6 +412,8 @@ export const useBleStore = defineStore('ble', {
|
|
|
this.searching = false
|
|
|
this.scanThrottled = false
|
|
|
if (this.bleState === BLE_STATE.SCANNING) {
|
|
|
+ // _setState(READY) 内部会检测 _connectedBeforeScan,
|
|
|
+ // 若扫描前有连接则自动恢复为 READY_COMM
|
|
|
this._setState(BLE_STATE.READY)
|
|
|
}
|
|
|
},
|
|
|
@@ -761,10 +777,9 @@ export const useBleStore = defineStore('ble', {
|
|
|
*/
|
|
|
_showBleOffPrompt(message) {
|
|
|
const s = _S()
|
|
|
- // 防抖:5秒内不重复弹窗
|
|
|
- const now = Date.now()
|
|
|
- if (s._lastBleOffPromptTime && now - s._lastBleOffPromptTime < 5000) return
|
|
|
- s._lastBleOffPromptTime = now
|
|
|
+ // 弹窗正在显示中,不重复弹出
|
|
|
+ if (s._bleOffPromptShowing) return
|
|
|
+ s._bleOffPromptShowing = true
|
|
|
|
|
|
uni.showModal({
|
|
|
title: '蓝牙未开启',
|
|
|
@@ -772,6 +787,7 @@ export const useBleStore = defineStore('ble', {
|
|
|
confirmText: '去设置',
|
|
|
cancelText: '取消',
|
|
|
success: (res) => {
|
|
|
+ s._bleOffPromptShowing = false
|
|
|
if (res.confirm) {
|
|
|
// #ifdef APP-PLUS
|
|
|
const platform = uni.getSystemInfoSync().platform
|