使用 BLE 反应原生 Android Manifest 权限设置

问题描述 投票:0回答:1

我会使用React Native来开发移动应用程序
我需要使用该应用程序连接低功耗蓝牙 (BLE)

我对 AndroidManifest.xml 上的 Android 权限设置和本机反应有一些疑问

  1. 如果我有旧设备,例如小米手机运行Android 10(sdk 28或sdk 29),是否允许使用蓝牙功能?我需要将 android:maxSdkVersion="30" 设置为更低?

  2. Android 13 BLE 是否需要任何新的权限?

我在AndroidManifest.xml中添加了使用权限,

<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />

    
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
        android:usesPermissionFlags="neverForLocation"
        tools:targetApi="s" />

    <!-- Needed only if your app looks for Bluetooth devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <!-- Needed only if your app makes the device discoverable to Bluetooth devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.ACCESS_BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

另一方面,我会在扫描设备时添加检查

  requestPermissionBluetoothConnectAndroid = async () => {
    const status = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
    );

    if (status === PermissionsAndroid.RESULTS.GRANTED) {
      return true;
    }

    if (status === PermissionsAndroid.RESULTS.DENIED) {
      ToastAndroid.show(
        'Location permission denied by user.',
        ToastAndroid.LONG,
      );
    } else if (status === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
      ToastAndroid.show(
        'Location permission revoked by user.',
        ToastAndroid.LONG,
      );
    }
  };

但是,当我扫描BLE设备时,应用程序会崩溃

android react-native bluetooth bluetooth-lowenergy
1个回答
0
投票

还必须获得用户的蓝牙扫描权限。我认为这可能是您收到错误的原因。

您必须按如下方式从用户处获取蓝牙和位置权限,并在每次扫描前进行检查;

  const handleAndroidPermissionCheck = async (cb: VoidCallback) => {
    if (Platform.OS === 'android') {
      const res = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      );
      await PermissionsAndroid.requestMultiple([
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
      ]);
      cb(res === PermissionsAndroid.RESULTS.GRANTED);
    } else {
      cb(true);
    }
  };

  const startScan = () => {
    try {
      BleManager.scan([SERVICE_UUID], seconds, false).then(() => {});
    } catch (error) {}
  };

AndroidManifest 应该是这样的;

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

我的构建gradle是这样的;

buildToolsVersion = "33.0.0"
minSdkVersion = 23
compileSdkVersion = 33
targetSdkVersion = 33
© www.soinside.com 2019 - 2024. All rights reserved.