我正在尝试获取在 Android 中使用蓝牙的权限,我尝试了不同的选项,单个请求和多个请求,但我没有收到权限请求。此外,还会出现对相机或其他权限的请求 代码:
const requestPermissions = async () => {
try {
const bluetoothPermission = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH,
{
title: 'Bluetooth Permission',
message: 'This app needs Bluetooth access to scan for devices.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
}
);
const bluetoothAdminPermission = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADMIN,
{
title: 'Bluetooth Admin Permission',
message: 'This app needs Bluetooth Admin access to manage Bluetooth settings.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
}
);
const locationPermission = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location Permission',
message: 'This app needs location access to scan for Bluetooth devices.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
}
);
if (
bluetoothPermission !== PermissionsAndroid.RESULTS.GRANTED ||
bluetoothAdminPermission !== PermissionsAndroid.RESULTS.GRANTED ||
locationPermission !== PermissionsAndroid.RESULTS.GRANTED
) {
Alert.alert('Bluetooth permissions not granted');
}
} catch (err) {
console.error('Error requesting permissions:', err);
}
};
我也尝试过这个:
const result = await requestMultiple([
可能 react-native-permissions 库可以更好地处理它。我从未使用过内置权限。
无论如何。您是否也将权限放入清单文件中?您的设备支持蓝牙吗?
另请注意,Android 的权限可能非常烦人,有许多权限不再受支持或需要您放置最小或最大支持的 sdk。 Android 文档