this is for react-native after debugging and follow GitHub and stack overflow i'm able to find the region how to solve this in my build.gradel file i have target sdk version is 33
buildToolsVersion = "33.0.0"
minSdkVersion = 21
compileSdkVersion = 33
targetSdkVersion = 33
androidmainifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.awesomeproject">
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
</manifest>
in js file
import { Platform, PermissionsAndroid } from 'react-native';
// Function to request Bluetooth permissions
const requestBluetoothPermissions = async () => {
if (Platform.OS === 'android') {
if (Platform.Version >= 31) {
// Handle target API 31 and above
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
{
title: 'Bluetooth Permission',
message: 'This app requires Bluetooth Connect permission.',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// Permission granted, continue with Bluetooth functionality
} else {
// Permission denied, handle accordingly
}
} catch (error) {
// Handle error
}
} else {
// Handle target API below 31
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Bluetooth Permission',
message: 'This app requires access to your location to scan for
Bluetooth devices.',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// Permission granted, continue with Bluetooth functionality
} else {
// Permission denied, handle accordingly
}
} catch (error) {
// Handle error
}
}
}
};
// Call the function to request Bluetooth permissions
requestBluetoothPermissions();
note if (Platform.OS === 'android') //important condition.
if (Platform.Version >= 31) //important condition.
my js code i have settime 5 sec to scan the device and show them in ui with help of flatlist i have different requirement so i call BLUETOOTH_CONNECT and ACCESS_FINE_LOCATION at same time
import React, { useState, useEffect } from 'react';
import { View, Text, Button, PermissionsAndroid, FlatList } from
'react-native';
import { BleManager } from 'react-native-ble-plx';
const bleManager = new BleManager();
const BluetoothScreen = () => {
const [scanning, setScanning] = useState(false);
const [devices, setDevices] = useState([]);
const requestBluetoothPermissions = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Bluetooth Permission',
message: 'This app requires access to your Fine location to
scan for Bluetooth devices.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
const granted1 = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
{
title: 'Bluetooth Permission',
message: 'This app requires access to your Fine location to
scan for Bluetooth devices.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
const bluetoothPermission = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
);
console.log('bluetoothPermission',bluetoothPermission)
if (bluetoothPermission === PermissionsAndroid.RESULTS.GRANTED)
{
console.log('Bluetooth permission granted');
startDeviceScan();
} else {
console.log('Bluetooth permission denied');
}
} catch (error) {
console.error('Error requesting Bluetooth permissions:',
error);
}
};
const startDeviceScan = async() => {
// setScanning(true);
bleManager.startDeviceScan(
[],
{allowDuplicates: false},
// null,null,
(error, scannedDevice) => {
if (error) {
console.log('Error scanning devices:', error);
// console.log('PermissionsAndroid', PermissionsAndroid)
return;
}
if (scannedDevice) {
const device=scannedDevice
console.log('Found device:', device);
setDevices(prevDevices => [...prevDevices, scannedDevice]);
setTimeout(()=>{
bleManager.stopDeviceScan();
},5000)
}
});
};
const renderItem = ({ item }) => (
<View>
<Text>{item.name}</Text>
</View>
);
useEffect(() => {
requestBluetoothPermissions();
// Cleanup function
return () => {
bleManager.stopDeviceScan();
};
}, []);
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent:
'center' }}>
<Button title={scanning ? 'Scanning...' : 'Scan Devices'}
onPress={startDeviceScan} disabled={scanning} />
<FlatList
data={devices}
renderItem={renderItem}
keyExtractor={(item,index) => index.toString()}
/>
</View>
);
};
export default BluetoothScreen;