Skip to content

Location usage in mobile app

In order to use BLE, the application needs two things: permission to use location and to check if the location has been turned on.

Location permissions ##

Android ###

In order to receive permission to use location services, you have to enable the right Android permissions in the AndroidManifest.xml file.

The picture below shows which permissions are mandatory in order to use BLE for Android:

(Now also works for Android 12!)

//Method which checks if permission for location services has been given
export async function checkLocationOn() {
  await PermissionsAndroid.check(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  ).then(async response => {
    if (response !== true) {
      await requestLocationPermission();
    }
    getGeolocation();
  });
}
//Method which asks permission so that the app can use location
export async function requestLocationPermission() {
  try {
    await PermissionsAndroid.requestMultiple([
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
      PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
    ]);
  } catch (err) {
    console.log(err);
  }
}

Checking if location services has been turned on

As React-Native does not offer this as part of its framework, we’re supposed to rely on another’s library to check on the Geolocation.

For this task I have been using react-native-geolocation-service in order to check if the location services have been turned on. Link to the library

The image below shows the method which handles asking if the user would like to turn on location services: schematic

If the service has not been turned on, it will show a basic Android prompt asking if you would like to turn on your location.


Last update: June 23, 2023