钟铮锁App web部分 需要打包放进对应的安卓项目 生成apk 才能正常使用功能
he wei
2025-01-19 ba2cfa9907623c094e6e2d52d12dc3055ddd587a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 
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中添加必要的权限描述以确保应用能够请求这些权限
}