fix(device): 扫描前检查系统蓝牙/定位开关,定位关闭时主动提示

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-08-24 09:12:10 +08:00
co-authored by Claude Sonnet 4.6
parent 9a81092c99
commit 6fe36f59fe
@@ -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<boolean> {
try {
const { authSetting } = await wx.getSetting()
const setting = authSetting as Record<string, boolean | undefined>
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' }),
})
},