Link-OS SDK for Android | Permissions and Bluetooth API

Steven Si -
3 MIN READ
1038

Nowadays, most of our Zebra printers are equipped with Bluetooth (BT), Bluetooth Low Energy (BLE) or both (BT + BLE). There is a Bluetooth User Guide for Zebra printers, which covers how the Bluetooth technologies (both BT & BLE) are used in Zebra printer production lines, the security mode, pairing and configuration, etc. To take the advantages of the Bluetooth on the printers, the Link-OS SDK for Android provides a number of API, ranging from Printer Discovery through Bluetooth, to Bluetooth Discovery, to Bluetooth Connection, to Multichannel Bluetooth Connection, etc. The Link-OS SDK for Android BTLE offers the equivalent API for using Bluetooth Low Energy (BLE). The documentation of the equivalent API can be found at here.

In this article, we'll specifically address the Android permissions associated with Bluetooth. For specifics on  how to use these Bluetooth API, refer to this SDK API documentation and the demo projects included in their respective SDK packages. 

Permissions Declared in the Application Manifest File

As well documented in Android Developer Guide, to use Bluetooth features (BT or BLE) in your application, you must declare two permissions in your application manifest file. They are BLUETOOTH and ACCESS_FINE_LOCATION (If your app targets Android 9 (API level 28) or lower, you can declare the ACCESS_COARSE_LOCATION permission instead of the ACCESS_FINE_LOCATION permission). In addition, If you want your app to initiate device discovery or manipulate Bluetooth settings, you must declare the BLUETOOTH_ADMIN permission as well.

Therefore, to use the Bluetooth API in Link-OS SDK for Android or for Android BTLE, you must have the following permissions in your application manifest file.

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

    <!-- If your app targets Android 9 or lower, you can declare
         ACCESS_COARSE_LOCATION instead. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    ...
</manifest>

Request App Permissions Explicitly (for Android 6.0 and higher)

Starting from Android 6.0 (API level 23), the app must request the user to approve the "dangerous" permissions declared in the manifest file at runtime. Both BLUETOOTH and BLUETOOTH_ADMIN are normal permissions, which do not need the user approval at runtime. However, both ACCESS_FINE_LOCATION and  ACCESS_COARSE_LOCATION are "dangerous" permissions that require the approval explicitly from the user at runtime.

There are two ways to approve the "dangerous" permissions. One way is to grant the permission through the app’s Settings. The other way is to request the permissions from the app.

Grant the Permission(s) through the App Settings

A user can use the app’s settings to grant the permission(s). The steps are:

  • Go to the phone’s Settings and click on the Apps option,
  • Select the specific app the user wants to give the permission(s) for,
  • Choose the Permissions option under that app,
  • Grant the permission(s)

Request for Permission(s) from the App

The Request App Permissions section in Android Developer Guide has sample code to guide you on how to let the system to manage the permission request or let you manage the permission request yourself. We will let you explore the sample code in the Request App Permissions section. We will not repeat the sample code again here in this article.

Note: If the app is deployed through Zebra EMM (Enterprise Mobility Management), the administrators may automatically grant the runtime permission for location. If the app is installed through Zebra StageNow, the runtime location permission will be automatically granted. Therefore, the user doesn’t need to grant the location permission through the app settings in these cases.

Bluetooth Discovery in Android 10 and Higher

Due to the privacy changes in Android 10, an app must have the ACCESS_FINE_LOCATION permission in order to use the methods in the BluetoothDiscoverer class in Link-OS SDK for Android, if the app targets Android 10 and higher. In this case, the ACCESS_FINE_LOCATION permission must be declared in the app's manifest file and requested explicitly as described above. Otherwise, the Bluetooth discovery won't start.

As always, your questions and comments are welcome. Happy coding!

 

profile

Steven Si

Please Register or Login to post a reply

Replies