bluetoothAdapter.startDiscovery() returns False always

301 Views Asked by At

This is the code I'm using to try to start Bluetooth discovery. However, I always end up with the "Unable to start discovery" message (see below the code).

Device: Pixel 3 running Android 10 API 29

from grade file: minSdkVersion 16
targetSdkVersion 30

from manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
<uses-permission android:name="android.permission.BLUETOOTH"/>  
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>  
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
<uses-feature android:name="android.hardware.bluetooth" />  

from java:

if (bluetoothAdapter==null)  
    Log.i("BT", "First must enable BT");  
else  
{  
    if(bluetoothAdapter.getState()==bluetoothAdapter.STATE_ON)  
    {  
        Log.i("BT", "BT State on");  
        if (bluetoothAdapter.isDiscovering()) {  
            Log.i("BT", "was already discovering");  
            bluetoothAdapter.cancelDiscovery();  
        }  
        if(bluetoothAdapter.startDiscovery())  
            Log.i("BT", "starting discovery");  
        else  
            Log.i("BT", "Unable to start discovery");  

    }  
    else  
        Log.i("BT", "BT State NOT on");
}

Thanks!

1

There are 1 best solutions below

1
Radu Georgescu On

So despite all permissions from the manifest, the app was declined Location servces by the system and my app wasn't even listed when I was going to the Location settings in the phone menu. I had to go to the drop down menu and enable "Show system" and then a bunch of other apps with weird names appeared, among them my app. I enabled "alwasys allow" and now BT is discovering nearby devices.

How can I request this permission at runtime then so I make sure I have permission without having to go to the Settings Menu?