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

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

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

问题

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

这是所使用的权限:

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

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

PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)

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

回答:

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

import { PermissionsAndroid } from "react-native"

await PermissionsAndroid.requestMultiple([
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
     PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
])

之前我只请求了:

PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)
英文:

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:

&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_CONNECT&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION &quot; /&gt;
&lt;uses-permission-sdk-23 android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_SCAN&quot; /&gt;
&lt;uses-feature android:name=&quot;android.hardware.bluetooth_le&quot; android:required=&quot;true&quot;/&gt;

And in the app am running

PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)

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.

import { PermissionsAndroid } from &quot;react-native&quot;

await PermissionsAndroid.requestMultiple([
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
     PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
])

Previously I was only requesting

PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)

答案1

得分: 0

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

```ts
import { PermissionsAndroid } from "react-native"

await PermissionsAndroid.requestMultiple([
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
     PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
])

之前我只请求了

PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)
英文:

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.

import { PermissionsAndroid } from &quot;react-native&quot;

await PermissionsAndroid.requestMultiple([
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
     PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
     PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
])

Previously I was only requesting

PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)

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:

确定