Browse Source

设备椅操作页面退出后蓝牙不断开

liyuliangjiazai 5 months ago
parent
commit
2adf6fb058

+ 3 - 3
code/ajyApp/pages/device/detail/detail.nvue

@@ -110,9 +110,9 @@ export default {
 	onLoad(options) {
 		const sysInfo = uni.getSystemInfoSync()
 		this.statusBarHeight = sysInfo.statusBarHeight || 44
-		this.name = options.name || ''
-		this.deviceId = options.deviceId || ''
-		this.rssi = options.rssi || ''
+		this.name = decodeURIComponent(options.name || '')
+		this.deviceId = decodeURIComponent(options.deviceId || '')
+		this.rssi = decodeURIComponent(options.rssi || '')
 	},
 	async onShow() {
 		const bleStore = useBleStore()

+ 6 - 4
code/ajyApp/pages/device/deviceInfo/deviceInfo.nvue

@@ -366,9 +366,9 @@ export default {
 		const sysInfo = uni.getSystemInfoSync()
 		this.statusBarHeight = sysInfo.statusBarHeight || 44
 
-		this.deviceId = options.deviceId || ''
-		this.deviceName = options.name || ''
-		this.rssi = options.rssi || ''
+		this.deviceId = decodeURIComponent(options.deviceId || '')
+		this.deviceName = decodeURIComponent(options.name || '')
+		this.rssi = decodeURIComponent(options.rssi || '')
 
 		// TODO: 后续接口确定后实现 - 下载穴位列表
 		// TODO: 后续接口确定后实现 - 下载专业模式推荐方案
@@ -383,7 +383,9 @@ export default {
 	},
 	onUnload() {
 		this.destroyAudio()
-		this.disconnectBle()
+		// 离开页面时不断开蓝牙连接,仅停止扫描
+		// 下次进入相同设备可复用连接
+		this.cleanupOnLeave()
 	},
 	methods: {
 		goBack() {

+ 33 - 3
code/ajyApp/pages/device/deviceInfo/mixins/ble-mixin.js

@@ -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 || '')