|
import { Capacitor } from '@capacitor/core'; // 如果你使用的是Capacitor
|
import { Geolocation } from '@capacitor/geolocation';
|
import { BluetoothLe } from '@capacitor-community/bluetooth-le';
|
|
export default async function requestLocationPermission() {
|
// 对于 Android,请求 BLUETOOTH 和 BLUETOOTH_CONNECT 权限
|
if (Capacitor.getPlatform() === 'android') {
|
const permissionStatus = await Geolocation.requestPermissions();
|
// console.log('BluetoothLe.requestPermissions', BluetoothLe, BluetoothLe.requestPermissions, '=============');
|
|
try {
|
|
const permissions = await BluetoothLe.requestPermissions();
|
|
console.log('permissions', permissions, '=============');
|
} catch (error) {
|
console.log('申请蓝牙权限失败', error, '=============');
|
|
}
|
|
|
|
// if (permissions.bleScan && permissions.bleAdvertise && permissions.bleConnect && permissionStatus.location === 'granted') {
|
if (permissionStatus.location === 'granted') {
|
// 权限已授予,获取位置
|
// const position = await Geolocation.getCurrentPosition();
|
// console.log('Latitude:', position.coords.latitude);
|
// console.log('Longitude:', position.coords.longitude);
|
console.log('位置权限已授予');
|
return true;
|
} else {
|
console.log('位置权限未授予');
|
return false;
|
}
|
}
|
|
// if (Capacitor.isNativePlatform() && Capacitor.getPlatform() === 'android') { // 如果你使用的是Capacitor
|
// let _premission = null;
|
// switch (type) {
|
// case 'scan':
|
// _premission = permissionInstance.PERMISSION.BLUETOOTH_SCAN;
|
// break;
|
// case 'connect':
|
// // _premission = permissionInstance.PERMISSION.BLUETOOTH
|
// _premission = 'android.permission.BLUETOOTH_CONNECT';
|
// break;
|
// case 'location':
|
// _premission = permissionInstance.PERMISSION.ACCESS_FINE_LOCATION;
|
// break;
|
// }
|
// // const androidPermissions = await import('@ionic-native/android-permissions/ngx').then(mod => mod.AndroidPermissions);
|
|
// // 检查是否已经获得位置权限
|
// const hasPermission = await permissionInstance.checkPermission(_premission);
|
|
// if (!hasPermission) {
|
// // 如果没有获得权限,则请求权限
|
// const result = await permissionInstance.requestPermission(_premission);
|
|
// console.log('result1312313123213213', result, '=============');
|
|
// // 根据请求结果执行相应操作
|
// if (result.hasPermission) {
|
// console.log('Location permission granted 12313213.');
|
// // 继续执行需要位置权限的逻辑
|
// return true;
|
// } else {
|
// console.error('Location permission denied.');
|
// // 处理权限被拒绝的情况
|
// // 注意:在某些情况下,用户可能选择了“不再询问”,此时可能需要引导用户到应用的设置页面手动授予权限
|
// return false;
|
// }
|
// } else {
|
// console.log('Location permission already granted.');
|
// // 继续执行需要位置权限的逻辑(如果之前没有执行的话)
|
// return true
|
// }
|
// }
|
// 对于iOS,通常不需要在这里显式请求权限,因为系统会在需要时提示用户
|
// 但你可以在Info.plist中添加必要的权限描述以确保应用能够请求这些权限
|
}
|