英文:
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_SCAN
和BLUETOOTH_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:
<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"/>
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 "react-native"
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 "react-native"
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,
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论