2

I've created a sample Wear OS app, which should discover BLE devices, but my code requires Bluetooth permission. When I put these lines in manifest:

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

corresponding permission is not displayed in settings/apps/permissions and every permission request does nothing. By the way, my BLE-devices (a speaker and a esp-32) is not shown in settings/Bluetooth also.

How can I grant Bluetooth permissions for my app or how can I connect BLE device to my watch?

upd: I tried these:

        if (checkSelfPermission(Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(arrayOf<String>(Manifest.permission.BLUETOOTH), 1001)
        }

        if (checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(arrayOf<String>(Manifest.permission.BLUETOOTH_CONNECT), 1001)
        }

        if (checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(arrayOf<String>(Manifest.permission.BLUETOOTH_SCAN), 1001)
        }

        if (checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(arrayOf<String>(Manifest.permission.BLUETOOTH_ADMIN), 1001)
        }

But dialogs windows still are not displayed

Alexey Kolosov
  • 140
  • 2
  • 15

2 Answers2

2

According documentation, you need particular permissions in based of the target Android API version.

If your app targets Android 11 (API level 30) or lower, declare the following permissions in your app's manifest file:

  • BLUETOOTH is necessary to perform any Bluetooth classic or BLE communication, such as requesting a connection, accepting a connection, and transferring data.

  • ACCESS_FINE_LOCATION is necessary because, on Android 11 and lower, a Bluetooth scan could potentially be used to gather information about the location of the user.

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 order to perform a scan to discover BLE devices the app must require explicitaly to the user the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission also to be declared in the AndroidManifest.xml.

In my project (WearOS API version 28) I used this code in the onCreate function of MainActivity class

if (ContextCompat.checkSelfPermission(
            this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED 
    ) {
        ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                1);
    }

And I overrided the onRequestPermissionsResult function

 public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    switch (requestCode) {
        case 1:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                
                Logger.d("MainActivity","Permission approved");
            }  else {
                Logger.d("MainActivity","Error getting permission");
            }
            return;
    }

}

This works for me, I hope would help you

fabik111
  • 143
  • 7
1

there are some permissions like camera, bluetooth which need to be asked first and then manually provided. use this in the activity that loads first in your app.

if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(arrayOf<String>(Manifest.permission.CAMERA), 1001);
        }             //ask camera permissions

make sure to do required changes.

mordor619
  • 154
  • 11
  • 1
    This wear app requests bluetooth permissions https://github.com/garanj/wearwind/blob/866b88e72dff3fa76c752828bd4e5283c3390463/app/src/main/java/com/garan/wearwind/FanActivity.kt#L92 – Yuri Schimke Dec 28 '21 at 09:58
  • @mordor619 I've modified my code with this (see upd description), but It didn't help – Alexey Kolosov Dec 28 '21 at 10:51
  • @Yuri Schimke, thanks, seems that location permission is also needed for bluetooth, I'll double check it and if it is a solution, I'll post an answer here. – Alexey Kolosov Dec 28 '21 at 11:17
  • @andrew try and give different value for each permission like 1001, 1002, so on.. – mordor619 Dec 28 '21 at 14:06