0

My mobile app has functionality to turn on Bluetooth of mobile and show list of Bluetooth devices on the screen. I am using react-native-ble-plx lib for achieving this functionality. I need to turn on BLE of mobile by using react-native-ble-plx lib. Any method is available in that lib for turn on BLE?

HemantZ
  • 23
  • 1
  • 6

2 Answers2

4

react-native-ble-plx does not have this functionality but you can use react-native-bluetooth-state-manager. It allows you to read the current bluetooth state (enabled/disabled) and open the bluetooth settings page on iOS and android. It is even possible to enable (and disable) bluetooth on android without user interaction but you have to add the BLUETOOTH_ADMIN permission to you manifest.

But I would suggest you do not activate Bluetooth without user interaction. Simply check the state and show a message like

Bluetooth is required to use this app. Please activate it trough your settings

Only allow the usage of the rest of the app after bluetooth got enabled. A user is probably confused if he deactivated bluetooth and it is suddenly activated.

Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23
0

Is now possible to turn ON Bluetooth using react-native-ble-plx. Working example with Alert:

const subscription = BLTManager.onStateChange((state) => {  // check if device bluetooth is powered on, if not alert to enable it!
        if (state === 'PoweredOff') {
          Alert.alert('"App" would like to use Bluetooth.', 'This app uses Bluetooth to connect to and share information with your .....', [
            {
              text: "Don't allow",
              onPress: () => console.log('Cancel Pressed'),
              style: 'cancel',
            },
            { text: "Turn ON", onPress: () => { BLTManager.enable(); scanDevices() } },
          ]);

          subscription.remove();
        }
      }, true);