From 6fe36f59feceda0b7c88ca76fc3260445b523d2b Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Mon, 24 Aug 2026 09:12:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(device):=20=E6=89=AB=E6=8F=8F=E5=89=8D?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E7=B3=BB=E7=BB=9F=E8=93=9D=E7=89=99/?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E5=BC=80=E5=85=B3=EF=BC=8C=E5=AE=9A=E4=BD=8D?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=97=B6=E4=B8=BB=E5=8A=A8=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- .../pages/connectedDevice/connectedDevice.ts | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/miniprogram/pages/connectedDevice/connectedDevice.ts b/miniprogram/pages/connectedDevice/connectedDevice.ts index 02f8dc9..63d79a0 100644 --- a/miniprogram/pages/connectedDevice/connectedDevice.ts +++ b/miniprogram/pages/connectedDevice/connectedDevice.ts @@ -131,18 +131,53 @@ Page({ connectCleanup = null }, - /** 申请位置权限后打开蓝牙适配器(蓝牙权限由 openBluetoothAdapter 自动触发) */ - async _requestPermissionAndScan() { + /** 检查系统蓝牙开关 */ + _checkBluetoothEnabled(): boolean { + try { + if (wx.getSystemSetting().bluetoothEnabled === false) { + this._showBluetoothOff() + return false + } + return true + } catch { + // 基础库不支持 wx.getSystemSetting 时跳过,交由 openBluetoothAdapter 报错兜底 + return true + } + }, + + /** 检查系统定位开关 */ + _checkLocationService(): boolean { + try { + if (wx.getSystemSetting().locationEnabled === false) { + this._showLocationOff() + return false + } + return true + } catch { + return true + } + }, + + /** 申请微信“附近设备/位置”授权 */ + async _ensureLocationAuth(): Promise { try { const { authSetting } = await wx.getSetting() const setting = authSetting as Record if (!setting['scope.userLocation']) { await wx.authorize({ scope: 'scope.userLocation' }) } + return true } catch { this._showLocationGuide() - return + return false } + }, + + /** 编排:蓝牙开关 → 定位开关 → 微信授权 → 打开蓝牙并扫描 */ + async _requestPermissionAndScan() { + if (!this._checkBluetoothEnabled()) return + if (!this._checkLocationService()) return + if (!(await this._ensureLocationAuth())) return await this._openAdapterAndScan() }, @@ -280,6 +315,7 @@ Page({ content: '安卓手机搜索蓝牙设备需开启定位服务,请前往系统设置开启后重试。', showCancel: false, confirmText: '知道了', + success: () => this.setData({ status: 'idle' }), }) },