admin管理员组

文章数量:1026961

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

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

Share Improve this question asked Nov 16, 2024 at 16:32 Kristi FarrellKristi Farrell 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

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

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

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

Share Improve this question asked Nov 16, 2024 at 16:32 Kristi FarrellKristi Farrell 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

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

本文标签: React Native Detox Accept Bluetooth PermissionsStack Overflow