Kristi Farrell
Kristi Farrell

Reputation: 11

React Native Detox: Accept Bluetooth Permissions

I am working on a test suite for a react native app that requires bluetooth permissions to work. I know this is common for apps like ours but, for some reason, there is not a bluetooth option for permissions applied like this:

await device.launchApp({ permissions: { bluetooth: 'YES' } });

Is there another way to accept bluetooth permissions? I have read through documentation and done some searching with no luck so far.

await device.launchApp({ permissions: { bluetooth: 'YES' } });

Expectation: Bluetooth permissions are accepted

Actual: bluetooth is not an option in applesimutils for permissions

Upvotes: 1

Views: 16

Answers (1)

Eternal Dreamer
Eternal Dreamer

Reputation: 515

You might take a look at this answer

It requires @config-plugins/react-native-ble-plx and react-native-ble-plx ;

npx expo install react-native-ble-plx @config-plugins/react-native-ble-plx

If you use Expo (if not, you should) add this to your app.json or app.config.js ;

{
    "expo": {
        "plugins": ["@config-plugins/react-native-ble-plx"]
    }
}

Then take a look at the documentation here.

import { BleManager } from 'react-native-ble-plx'

export const manager = new BleManager()


export const requestBluetoothPermission = async () => {
  if (Platform.OS === 'ios') {
    return true
  }
  if (Platform.OS === 'android' && PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION) {
    const apiLevel = parseInt(Platform.Version.toString(), 10)

    if (apiLevel < 31) {
      const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
      return granted === PermissionsAndroid.RESULTS.GRANTED
    }
    if (PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN && PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT) {
      const result = await PermissionsAndroid.requestMultiple([
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
      ])

      return (
        result['android.permission.BLUETOOTH_CONNECT'] === PermissionsAndroid.RESULTS.GRANTED &&
        result['android.permission.BLUETOOTH_SCAN'] === PermissionsAndroid.RESULTS.GRANTED &&
        result['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED
      )
    }
  }

  return false
}

Note: iOS does not need a request permission like Android does

Upvotes: 0

Related Questions