|
|
@@ -5,7 +5,7 @@
|
|
|
*/
|
|
|
import { useBleStore } from '@/stores/ble'
|
|
|
import { MODE, MOXI_STATE, POWER, MUTE, TEMPERATURE, CHAIR_ANGLE } from '@/utils/ble/constants.js'
|
|
|
-import { ensureBlePrerequisite } from '@/utils/ble/permission.js'
|
|
|
+import { ensureBlePrerequisite, openBluetoothSettings, openLocationSettings } from '@/utils/ble/permission.js'
|
|
|
|
|
|
export default {
|
|
|
data() {
|
|
|
@@ -61,13 +61,35 @@ export default {
|
|
|
this.$refs.ayToast.error('请开启位置服务后重试')
|
|
|
return
|
|
|
}
|
|
|
+ if (e.message === 'BLE_ADAPTER_OFF') {
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '蓝牙未开启,请先开启蓝牙',
|
|
|
+ cancelText: '取消',
|
|
|
+ confirmText: '去开启',
|
|
|
+ success: (res) => {
|
|
|
+ if (res.confirm) {
|
|
|
+ openBluetoothSettings()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 判断当前是否已连接同一设备,如果是则跳过重连
|
|
|
+ const currentDevice = this.bleStore.device
|
|
|
+ console.log('[BLE] 当前设备', currentDevice,this.bleStore.linked,this.deviceId)
|
|
|
+ if (this.bleStore.linked && currentDevice && currentDevice.deviceId === this.deviceId) {
|
|
|
+ console.log('[BLE] 已连接相同设备,跳过重连', this.deviceId)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- // 2. 断开旧连接并关闭适配器(不解绑全局监听器)
|
|
|
+ // 3. 不同设备或未连接:断开旧连接并关闭适配器(不解绑全局监听器)
|
|
|
try { await this.bleStore.disconnect() } catch (_) {}
|
|
|
try { await this.bleStore.resetAdapter() } catch (_) {}
|
|
|
|
|
|
- // 3. 扫描并连接设备(优先直连,跳过扫描避免 Android 限流)
|
|
|
+ // 4. 扫描并连接设备(优先直连,跳过扫描避免 Android 限流)
|
|
|
try {
|
|
|
const scanOpt = { timeout: 8000 }
|
|
|
// 已有 deviceId 时优先直连,不需要扫描
|
|
|
@@ -194,6 +216,14 @@ export default {
|
|
|
this.bleStore.disconnect().catch(() => {})
|
|
|
},
|
|
|
|
|
|
+ /**
|
|
|
+ * 页面离开时的轻量清理:只停止扫描,不断开BLE连接
|
|
|
+ * 保持蓝牙连接状态,下次进入相同设备页面可复用
|
|
|
+ */
|
|
|
+ cleanupOnLeave() {
|
|
|
+ this.bleStore.stopScan()
|
|
|
+ },
|
|
|
+
|
|
|
// ========== BLE错误码转用户友好提示 ==========
|
|
|
_bleFriendlyMsg(e) {
|
|
|
const code = e && (e.code || e.message || '')
|