在AndroidManifest.xml文件中添加权限声明,并添加以下代码示例来检查设备是否有蓝牙功能:
import {Platform, PermissionsAndroid} from 'react-native';
import BluetoothStateManager from "react-native-bluetooth-state-manager";
async function checkBluetoothSupport() {
if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.BLUETOOTH,
);
if (granted) {
BluetoothStateManager.getStatus().then((status) => {
if (status !== 'PoweredOn') {
console.log("Bluetooth is not available");
}
});
} else {
console.warn('Bluetooth permission not granted');
}
} catch (err) {
console.warn(err);
}
}
}
使用上述代码示例可以在React Native应用中实现Android 12设备上的蓝牙启用。