React Native - Check if Bluetooth is turned on, and if not alert the user?

Viewed 6437

There is a way to check if the Bluetooth is turned on and if not, then alert the user? Thank you!

2 Answers

Try this:

Installation: npm install react-native-bluetooth-status --save

Link this plugin: react-native link react-native-bluetooth-status

import: import { BluetoothStatus } from 'react-native-bluetooth-status';

Check Bluetooth turn on or turn off code :

BluetoothStatus.state() is return boolean . if return true the bluetooth is ON if return false the bluetooth is OFF

componentDidMount() 
{
   this.checkInitialBluetoothState();
}

async checkInitialBluetoothState() 
{
const isEnabled = await BluetoothStatus.state();
console.log("check bluetooth on or off", isEnabled);
if(isEnabled == true){
  Alert.alert(
      'Bluethooth',
      'Bluetooth is turn on'
    );
 } else{
    Alert.alert(
      'Bluethooth',
      'Bluetooth is turn off'
    );
  }
}

react-native bluetooth status

Related