Bluetooth Low Energy permissions

In Android project structure, there is a file named AndroidManifest.xml which contains a high level overview of the Android application. This file contains the information about the activities, services, receivers, providers, libraries, and permissions. It is essential to give explicit permissions of Bluetooth. This can be achieved by providing the following line of code in the Manifest file:

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

This permission ensures that the application is able to access Bluetooth. Another important permission for the proper execution of Bluetooth Low Energy scanning is:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Starting Android Marshmallow v6.0, it is essential to give coarse and fine location permissions to start BLE scan. It is because Bluetooth Low Energy scanning works in the background and is blocked unless these permissions are given. This permission needs to be given in the Manifest file as well as on runtime. To request the location permission at runtime you need to write:

// ------------------------ MANUAL PERMISSIONS --------------------------- 
String[] permissionString = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
yourActivity.requestPermissions(permissionString, yourPermissionRequestCode);
// ----------------------- CATCHING PERMISSIONS --------------------------
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
if(requestCode == yourPermissionRequestCode)
{
... //Do something based on the user click.
}
}

This will ensure that the user is notified to give coarse and fine location permission before the BLE process starts. Since this permission is only required for Android Marshmallow and higher version, it is a good practice to put the previously mentioned code in a version check.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.70.157