添加了获取位置信息

This commit is contained in:
17792275749
2025-06-03 11:07:40 +08:00
parent 6e48bf1dc4
commit 68bc3c267e
2 changed files with 232 additions and 139 deletions
+9 -1
View File
@@ -35,7 +35,15 @@
"version": "1.2.15", "version": "1.2.15",
"provider": "wx0ffb48417ce6345c" "provider": "wx0ffb48417ce6345c"
} }
}, },
"permission": {
"scope.userLocation": {
"desc": "您的位置信息将用于发现附近的蓝牙设备,以便连接蓝牙秤。"
}
},
"requiredPrivateInfos": [
"getLocation"
],
"sitemapLocation": "sitemap.json", "sitemapLocation": "sitemap.json",
"lazyCodeLoading": "requiredComponents" "lazyCodeLoading": "requiredComponents"
} }
+223 -138
View File
@@ -1,165 +1,250 @@
const app = getApp(); const app = getApp();
Page({ Page({
data: { data: {
initText: "", initText: "",
devicesList: [], devicesList: [],
searchDevicesList: true, searchDevicesList: true,
}, isShowingModal: false, // 控制模态框是否正在显示
},
onLoad(options) { onLoad(options) {
this.checkBluetoothPermissionAndInit(); this.checkBluetoothPermissionAndInit();
}, },
onShow() { onShow() {
this.checkBluetoothPermissionAndInit(); this.checkBluetoothPermissionAndInit();
}, },
async checkBluetoothPermissionAndInit() {
if (this.data.isShowingModal) {
return;
}
checkBluetoothPermissionAndInit() {
this.setData({ this.setData({
initText: "正在检查蓝牙权限..." initText: "正在检查蓝牙及位置权限..."
}); });
wx.getSetting({
success: (res) => { try {
if (res.authSetting['scope.bluetooth']) { const settingRes = await wx.getSetting();
this.openBluetoothAdapter(); const hasBluetoothPermission = settingRes.authSetting['scope.bluetooth'];
} else { const hasLocationPermission = settingRes.authSetting['scope.userLocation'];
wx.authorize({
scope: 'scope.bluetooth', if (hasBluetoothPermission && hasLocationPermission) {
success: () => { this.openBluetoothAdapter();
this.openBluetoothAdapter(); } else {
}, let scopeToRequest = [];
fail: (err) => { let permissionNames = [];
this.setData({
initText: "蓝牙权限被拒绝。" if (!hasBluetoothPermission) {
}); scopeToRequest.push('scope.bluetooth');
wx.showModal({ permissionNames.push('蓝牙');
title: '提示', }
content: '您拒绝了蓝牙权限,将无法连接蓝牙秤。是否前往设置开启?', if (!hasLocationPermission) {
confirmText: '去开启', scopeToRequest.push('scope.userLocation');
cancelText: '不开启', permissionNames.push('位置');
success: (modalRes) => { }
if (modalRes.confirm) {
wx.openSetting({ if (scopeToRequest.length > 0) {
success: (settingRes) => { try {
console.log('openSetting success', settingRes); await wx.authorize({
}, scope: scopeToRequest[0]
fail: (settingErr) => { });
wx.navigateBack({ this.checkBluetoothPermissionAndInit();
delta: 1 } catch (authErr) {
}); if (this.data.isShowingModal) {
} return;
});
} else {
wx.navigateBack({
delta: 1
});
}
}
});
} }
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({ this.setData({
initText: "正在初始化蓝牙..." initText: "正在初始化蓝牙..."
}); });
wx.openBluetoothAdapter({
success: () => { try {
this.setData({ await wx.openBluetoothAdapter();
initText: "蓝牙初始化成功" 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(); return;
}, }
fail: (err) => {
this.setData({ wx.showModal({
initText: "蓝牙初始化失败。" title: "提示",
}); content: modalContent,
if (err.errCode === 10001) { showCancel: false,
wx.showModal({ success: () => {
title: "提示", this.setData({
content: "请确保手机蓝牙已开启。", isShowingModal: false
showCancel: false,
success: () => {
wx.navigateBack({
delta: 1
});
}
}); });
} else if (err.errMsg && err.errMsg.includes('permission denied')) { wx.navigateBack({
wx.showModal({ delta: 1
title: '提示',
content: '蓝牙权限仍被拒绝,无法使用蓝牙功能。',
showCancel: false,
success: () => {
wx.navigateBack({
delta: 1
});
}
}); });
} else { },
wx.showToast({ fail: () => {
title: `蓝牙适配器打开失败: ${err.errMsg || err.errCode}`, this.setData({
icon: 'none', isShowingModal: false
success: () => {
wx.navigateBack({
delta: 1
});
}
}); });
} }
}, });
}
},
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() { selectDevice() {
// app.globalData.ppScale.plugin.Blue.visibleLog(true); if(this.data.devicesList && this.data.devicesList.length && this.data.searchDevicesList) {
this.setData({ this.setData({
initText: "设备搜索中..." searchDevicesList: false
}); });
app.globalData.ppScale.device.list = []; setTimeout(() => {
let seviceList = app.globalData.ppScale.device.setting; app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList); app.globalData.ppScale.device.list = this.data.devicesList;
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;
wx.redirectTo({ wx.redirectTo({
url: "/pages/configureDevice/configureDevice" url: "/pages/configureDevice/configureDevice"
}) })
}, 1000); }, 1000);
} }
} },
onUnload() {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
this.setData({
isShowingModal: false
});
}
}) })