From 68bc3c267e67822e83fc9ba316dad1cf1a47aa77 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Tue, 3 Jun 2025 11:07:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.json | 10 +- pages/searchDevice/searchDevice.js | 361 ++++++++++++++++++----------- 2 files changed, 232 insertions(+), 139 deletions(-) diff --git a/app.json b/app.json index 5971f4b..84f9136 100644 --- a/app.json +++ b/app.json @@ -35,7 +35,15 @@ "version": "1.2.15", "provider": "wx0ffb48417ce6345c" } - }, + }, + "permission": { + "scope.userLocation": { + "desc": "您的位置信息将用于发现附近的蓝牙设备,以便连接蓝牙秤。" + } + }, + "requiredPrivateInfos": [ + "getLocation" + ], "sitemapLocation": "sitemap.json", "lazyCodeLoading": "requiredComponents" } \ No newline at end of file diff --git a/pages/searchDevice/searchDevice.js b/pages/searchDevice/searchDevice.js index 973476d..6a54843 100644 --- a/pages/searchDevice/searchDevice.js +++ b/pages/searchDevice/searchDevice.js @@ -1,165 +1,250 @@ const app = getApp(); Page({ - data: { - initText: "", - devicesList: [], - searchDevicesList: true, - }, + data: { + initText: "", + devicesList: [], + searchDevicesList: true, + isShowingModal: false, // 控制模态框是否正在显示 + }, - onLoad(options) { - this.checkBluetoothPermissionAndInit(); - }, + onLoad(options) { + this.checkBluetoothPermissionAndInit(); + }, - onShow() { - this.checkBluetoothPermissionAndInit(); - }, + onShow() { + this.checkBluetoothPermissionAndInit(); + }, + + async checkBluetoothPermissionAndInit() { + if (this.data.isShowingModal) { + return; + } - checkBluetoothPermissionAndInit() { this.setData({ - initText: "正在检查蓝牙权限..." + initText: "正在检查蓝牙及位置权限..." }); - wx.getSetting({ - success: (res) => { - if (res.authSetting['scope.bluetooth']) { - this.openBluetoothAdapter(); - } else { - wx.authorize({ - scope: 'scope.bluetooth', - success: () => { - this.openBluetoothAdapter(); - }, - fail: (err) => { - this.setData({ - initText: "蓝牙权限被拒绝。" - }); - wx.showModal({ - title: '提示', - content: '您拒绝了蓝牙权限,将无法连接蓝牙秤。是否前往设置开启?', - confirmText: '去开启', - cancelText: '不开启', - success: (modalRes) => { - if (modalRes.confirm) { - wx.openSetting({ - success: (settingRes) => { - console.log('openSetting success', settingRes); - }, - fail: (settingErr) => { - wx.navigateBack({ - delta: 1 - }); - } - }); - } else { - wx.navigateBack({ - delta: 1 - }); - } - } - }); + + try { + const settingRes = await wx.getSetting(); + const hasBluetoothPermission = settingRes.authSetting['scope.bluetooth']; + const hasLocationPermission = settingRes.authSetting['scope.userLocation']; + + if (hasBluetoothPermission && hasLocationPermission) { + this.openBluetoothAdapter(); + } else { + let scopeToRequest = []; + let permissionNames = []; + + if (!hasBluetoothPermission) { + scopeToRequest.push('scope.bluetooth'); + permissionNames.push('蓝牙'); + } + if (!hasLocationPermission) { + scopeToRequest.push('scope.userLocation'); + permissionNames.push('位置'); + } + + if (scopeToRequest.length > 0) { + try { + await wx.authorize({ + scope: scopeToRequest[0] + }); + this.checkBluetoothPermissionAndInit(); + } catch (authErr) { + if (this.data.isShowingModal) { + return; } + this.setData({ + isShowingModal: true + }); + + wx.showModal({ + title: '提示', + content: `小程序需要您的${permissionNames.join('和')}权限才能正常使用蓝牙功能。请前往手机设置开启。`, + confirmText: '去开启', + cancelText: '不开启', + success: (modalRes) => { + this.setData({ + isShowingModal: false + }); + if (modalRes.confirm) { + wx.openSetting({ + success: (settingRes) => { + this.checkBluetoothPermissionAndInit(); + }, + fail: (settingErr) => { + wx.navigateBack({ + delta: 1 + }); + } + }); + } else { + this.setData({ + initText: "权限被拒绝。" + }); + wx.navigateBack({ + delta: 1 + }); + } + }, + fail: () => { + this.setData({ + isShowingModal: false + }); + } + }); + } + } + } + } catch (err) { + if (this.data.isShowingModal) { + return; + } + this.setData({ + isShowingModal: true + }); + + wx.showToast({ + title: '获取权限设置失败', + icon: 'none', + duration: 2000, + success: () => { + this.setData({ + isShowingModal: false + }); + wx.navigateBack({ + delta: 1 + }); + }, + fail: () => { + this.setData({ + isShowingModal: false + }); + wx.navigateBack({ + delta: 1 }); } - }, - fail: (err) => { - this.setData({ - initText: "获取权限设置失败。" - }); - wx.showToast({ - title: '获取权限设置失败', - icon: 'none' - }); - wx.navigateBack({ - delta: 1 - }); - } - }); + }); + } }, - - openBluetoothAdapter() { + + async openBluetoothAdapter() { + if (this.data.isShowingModal) { + return; + } + this.setData({ initText: "正在初始化蓝牙..." }); - wx.openBluetoothAdapter({ - success: () => { - this.setData({ - initText: "蓝牙初始化成功" + + try { + await wx.openBluetoothAdapter(); + this.setData({ + initText: "蓝牙初始化成功" + }); + this.getDeviceSettingList(); + } catch (err) { + this.setData({ + initText: "蓝牙初始化失败。" + }); + + if (this.data.isShowingModal) { + return; + } + this.setData({ + isShowingModal: true + }); + + let modalContent = ''; + if (err.errCode === 10001) { + modalContent = "请确保手机蓝牙已开启。"; + } else if (err.errMsg && (err.errMsg.includes('permission denied') || err.errCode === 10003)) { + modalContent = '蓝牙或位置权限仍被拒绝,无法使用蓝牙功能。'; + } else { + wx.showToast({ + title: `蓝牙适配器打开失败: ${err.errMsg || err.errCode}`, + icon: 'none', + duration: 2000, + success: () => { + this.setData({ + isShowingModal: false + }); + wx.navigateBack({ + delta: 1 + }); + }, + fail: () => { + this.setData({ + isShowingModal: false + }); + wx.navigateBack({ + delta: 1 + }); + } }); - this.getDeviceSettingList(); - }, - fail: (err) => { - this.setData({ - initText: "蓝牙初始化失败。" - }); - if (err.errCode === 10001) { - wx.showModal({ - title: "提示", - content: "请确保手机蓝牙已开启。", - showCancel: false, - success: () => { - wx.navigateBack({ - delta: 1 - }); - } + return; + } + + wx.showModal({ + title: "提示", + content: modalContent, + showCancel: false, + success: () => { + this.setData({ + isShowingModal: false }); - } else if (err.errMsg && err.errMsg.includes('permission denied')) { - wx.showModal({ - title: '提示', - content: '蓝牙权限仍被拒绝,无法使用蓝牙功能。', - showCancel: false, - success: () => { - wx.navigateBack({ - delta: 1 - }); - } + wx.navigateBack({ + delta: 1 }); - } else { - wx.showToast({ - title: `蓝牙适配器打开失败: ${err.errMsg || err.errCode}`, - icon: 'none', - success: () => { - wx.navigateBack({ - delta: 1 - }); - } + }, + fail: () => { + this.setData({ + isShowingModal: false }); } - }, + }); + } + }, + + getDeviceSettingList() { + // app.globalData.ppScale.plugin.Blue.visibleLog(true); + this.setData({ + initText: "设备搜索中..." + }); + app.globalData.ppScale.device.list = []; + let seviceList = app.globalData.ppScale.device.setting; + app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList); + let deviceNames = seviceList.map(item => item.deviceName); + app.globalData.ppScale.plugin.Blue.start(deviceNames, false); + app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => { + console.log("searchDevice ===> devicesList", res); + this.setData({ + devicesList: res + }); + this.selectDevice(); }); }, - getDeviceSettingList() { - // app.globalData.ppScale.plugin.Blue.visibleLog(true); - this.setData({ - initText: "设备搜索中..." - }); - app.globalData.ppScale.device.list = []; - let seviceList = app.globalData.ppScale.device.setting; - app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList); - let deviceNames = seviceList.map(item => item.deviceName); - app.globalData.ppScale.plugin.Blue.start(deviceNames, false); - app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => { - console.log("searchDevice ===> devicesList", res); - - this.setData({ - devicesList: res - }); - this.selectDevice(); - }); - }, - - selectDevice() { - if(this.data.devicesList && this.data.devicesList.length && this.data.searchDevicesList) { - this.setData({ - searchDevicesList: false - }); - setTimeout(() => { - app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); - app.globalData.ppScale.device.list = this.data.devicesList; + selectDevice() { + if(this.data.devicesList && this.data.devicesList.length && this.data.searchDevicesList) { + this.setData({ + searchDevicesList: false + }); + setTimeout(() => { + app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); + app.globalData.ppScale.device.list = this.data.devicesList; wx.redirectTo({ url: "/pages/configureDevice/configureDevice" }) - }, 1000); - } - } + }, 1000); + } + }, + + onUnload() { + app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); + this.setData({ + isShowingModal: false + }); + } }) \ No newline at end of file