I have added react-native-ble-plx in my app. I have also linked it using react native link cmd. I have follwed all required step provided in the lib documents. But Its not working. I never ask for user permission and it gives error Deivce is not authorized to use BluetoothLE. Here is my code for
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smartdeviceiot">
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="23"/>
DeviceSearch.js
import colors from '../constants/colors'; import images from '../constants/images'; import { Button, Icon, Text, Container, Header, Left, Body, Title, Right } from 'native-base'; import { HomeStyle } from './styles/home'; import { BleManager } from'react-native-ble-plx' class DevicesSearch extends Component { static navigationOptions = { title: 'DevicesSearch' }; constructor(props) { super(props); const manager = new BleManager(); this.state = { }; } componentWillMount() { } render() { return ( >this.props.navigation.navigate("DrawerOpen")}> DevicesSearch Search Devices ); } scanAndConnect = () => { alert('asd') console.log('cal'); this.manager.startDeviceScan(null, null, (error, device) => { this.info("Scanning..."); console.log(device);
if (error) { this.error(error.message); return } if (device.name ==='MyDevice') { this.info("Connecting to Tappy"); this.manager.stopDeviceScan(); device.connect() .then((device) => { this.info("Discovering services and characteristics"); return device.discoverAllServicesAndCharacteristics() }) .then((device) => { this.info(device.id); device.writeCharacteristicWithResponseForService('12ab', '34cd',
'aGVsbG8gbWlzcyB0YXBweQ==') .then((characteristic) => { this.info(characteristic.value); return }) }) .catch((error) => { this.error(error.message) }) } }); } } function mapStateToProps(state) { //pass the providers return { } } /* Map Actions to Props */ function mapDispatchToProps(dispatch) >{ return { actions: bindActionCreators({ }, dispatch) }; } export default connect( mapStateToProps, mapDispatchToProps )(DevicesSearch);
if my bluetooth is turn off the code console.log me that bluetooth is off, but when its on it log me me that device is not auth to use bluetooth. I have also tired to use AndroidPermission lib but no success. it doesnt take permission from user
You need to explicitly ask the user for the permission. I had the same problem and I solved it by adding this code in a separate file:
export async function requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, {
title: 'Location permission for bluetooth scanning',
message: 'wahtever',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Location permission for bluetooth scanning granted');
return true;
} else {
console.log('Location permission for bluetooth scanning revoked');
return false;
}
} catch (err) {
console.warn(err);
return false;
}
}
and then when I need to scan, before the actual scan code I do the following:
scanAndConnect() {
const permission = requestLocationPermission();
if (permission) {
...here I scan because the user has given permission
For future reference, when targeting newer android devices you must ask for 'ACCESS_FINE_LOCATION' instead of 'ACCESS_COARSE_LOCATION'.
Source: https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With