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' }), }) },