In my app I want to search nearby users using both GPS and BLE. I came across the SettingsClient
which is used to enable GPS by showing a Popup in case it isn't enabled.
Interestingly, it also has a setNeedBle
method which as per the docs
Sets whether the client wants BLE scan to be enabled. When this flag is set to true, if the platform supports BLE scan mode and Bluetooth is off, the dialog will prompt the user to enable BLE scan. If the platform doesn't support BLE scan mode, the dialog will prompt to enable Bluetooth.
This is perfect for me. If I can scan for BLE devices when Bluetooth is off that would be amazing. I followed this official guide to setup things and I see the popup like this.
But after selecting OK when I try to do a BLE scan of nearby devices, I get this error.
java.lang.SecurityException: Need BLUETOOTH_PRIVILEGED permission: Neither user 10834 nor current process has android.permission.BLUETOOTH_PRIVILEGED.
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.os.Parcel.readException(Parcel.java:1868)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.startScan(IBluetoothGatt.java:947)
at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.onScannerRegistered(BluetoothLeScanner.java:460)
at android.bluetooth.le.IScannerCallback$Stub.onTransact(IScannerCallback.java:57)
at android.os.Binder.execTransact(Binder.java:731)
Apparently this indicates that I need the permission BLUETOOTH_PRIVILEGED
which isn't available to third-party apps.
I am confused then what is even the point of having the setNeedBle
function if only system apps can use it? Or am I missing something? Is it possible to do a BLE scan with this permission or will I have to ask users to enable the Bluetooth? Thanks!