Android/React Native crashing with react-native-ble-manager on one phone, but not other

huangapple go评论107阅读模式
英文:

Android/React Native crashing with react-native-ble-manager on one phone, but not other

问题

所以我遇到的问题是,在一个手机上(Pixel 6 Android 13)运行BLEManager.scan时,应用程序崩溃,但在运行相同代码的 Android 11 手机上却没有问题。

这是所使用的权限:

  1. <uses-permission android:name="android.permission.BLUETOOTH" />
  2. <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  3. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  4. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  5. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION " />
  6. <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>
  7. <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
  8. <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

而在应用程序中我运行了:

  1. PermissionsAndroid.request(
  2. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  3. )

猜测这可能是一个 Android 版本的问题,但并没有在文档中明确说明。

回答:

我需要在运行时请求这些权限,特别是在 Android 12 之后。我遇到问题是因为我的 react-native/@types/react-native 版本过时,不包含BLUETOOTH_SCANBLUETOOTH_CONNECT权限。我将版本升级到react-native@0.67以上,然后就能在运行时请求这些权限了。

  1. import { PermissionsAndroid } from "react-native"
  2. await PermissionsAndroid.requestMultiple([
  3. PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
  4. PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
  5. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  6. ])

之前我只请求了:

  1. PermissionsAndroid.request(
  2. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  3. )
英文:

So I'm running into an issue where I am getting an app crashing on one phone (Pixel 6 Android 13), but not an Android 11 phone when running BLEManager.scan.

Here are the permissions being used:

  1. &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot; /&gt;
  2. &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_CONNECT&quot; /&gt;
  3. &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot;/&gt;
  4. &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
  5. &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION &quot; /&gt;
  6. &lt;uses-permission-sdk-23 android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot;/&gt;
  7. &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_SCAN&quot; /&gt;
  8. &lt;uses-feature android:name=&quot;android.hardware.bluetooth_le&quot; android:required=&quot;true&quot;/&gt;

And in the app am running

  1. PermissionsAndroid.request(
  2. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  3. )

Guessing this is an android version issue, but not sure as this is not specified in docs.

Answer:

I needed to request these permissions at runtime post Android 12. I ran into issues because my version of react-native/@types/react-native was outdated and didn't contain the BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions. I bumped the version up past react-native@0.67 and then could request these permissions at runtime.

  1. import { PermissionsAndroid } from &quot;react-native&quot;
  2. await PermissionsAndroid.requestMultiple([
  3. PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
  4. PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
  5. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  6. ])

Previously I was only requesting

  1. PermissionsAndroid.request(
  2. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  3. )

答案1

得分: 0

  1. 我需要在运行时请求这些权限,尤其是在 Android 12 之后。我遇到了问题,因为我的 react-native/@types/react-native 版本过旧,没有包含 `BLUETOOTH_SCAN` `BLUETOOTH_CONNECT` 权限。我将版本升级到 `react-native@0.67` 以上,然后就能在运行时请求这些权限。
  2. ```ts
  3. import { PermissionsAndroid } from "react-native"
  4. await PermissionsAndroid.requestMultiple([
  5. PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
  6. PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
  7. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  8. ])

之前我只请求了

  1. PermissionsAndroid.request(
  2. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  3. )
英文:

Answer:

I needed to request these permissions at runtime post Android 12. I ran into issues because my version of react-native/@types/react-native was outdated and didn't contain the BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions. I bumped the version up past react-native@0.67 and then could request these permissions at runtime.

  1. import { PermissionsAndroid } from &quot;react-native&quot;
  2. await PermissionsAndroid.requestMultiple([
  3. PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
  4. PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
  5. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  6. ])

Previously I was only requesting

  1. PermissionsAndroid.request(
  2. PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  3. )

huangapple
  • 本文由 发表于 2023年2月27日 16:56:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定