chipweinberger / flutter_blue_plus

Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android, iOS, macOS
Other
791 stars 479 forks source link

[Help]: `java.lang.SecurityException`: Need `BLUETOOTH_PRIVILEGED` permission #699

Closed Mehdi-Mouilbeau closed 1 year ago

Mehdi-Mouilbeau commented 1 year ago

Requirements

Have you checked this problem on the example app?

Yes

FlutterBluePlus Version

1.22.0

Flutter Version

3.13.8

What OS?

Android

OS Version

Android 10 /12 and 14

Bluetooth Module

Honor 9X / Tab Lenovo / Pixel 7

What is your problem?

Hello,

For a week now I can't detect my Bluetooth device. The detection was working fine and then nothing even after several attempts. I tried from another environment, same thing. It will find my device once and then nothing.

Logs

Logs for Android 9/10 :

D/[FBP-Android](18683): [FBP] onMethodCall: startScan
I/BluetoothAdapter(18683): isLeEnabled(): ON
D/BluetoothLeScanner(18683): onScannerRegistered() - status=0 scannerId=15 mScannerId=0
W/Binder  (18683): Caught a RuntimeException from the binder stub implementation.
W/Binder  (18683): java.lang.SecurityException: Need BLUETOOTH_PRIVILEGED permission: Neither user 10143 nor current process has android.permission.BLUETOOTH_PRIVILEGED.
W/Binder  (18683):  at android.os.Parcel.createException(Parcel.java:2091)
W/Binder  (18683):  at android.os.Parcel.readException(Parcel.java:2059)
W/Binder  (18683):  at android.os.Parcel.readException(Parcel.java:2007)
W/Binder  (18683):  at android.bluetooth.IBluetoothGatt$Stub$Proxy.startScan(IBluetoothGatt.java:1526)
W/Binder  (18683):  at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.onScannerRegistered(BluetoothLeScanner.java:609)
W/Binder  (18683):  at android.bluetooth.le.IScannerCallback$Stub.onTransact(IScannerCallback.java:115)
W/Binder  (18683):  at android.os.Binder.execTransactInternal(Binder.java:1028)
W/Binder  (18683):  at android.os.Binder.execTransact(Binder.java:1001)
D/[FBP-Android](18683): [FBP] onMethodCall: stopScan
I/BluetoothAdapter(18683): isLeEnabled(): ON

Logs for Android 12 or higher :

D/[FBP-Android](24721): [FBP] onMethodCall: startScan
D/TrafficStats(24721): tagSocket(156) with statsTag=0xffffffff, statsUid=-1
I/TRuntime.CctTransportBackend(24721): Status Code: 200
D/BluetoothAdapter(24721): isLeEnabled(): ON
D/BluetoothLeScanner(24721): onScannerRegistered() - status=0 scannerId=4 mScannerId=0
D/[FBP-Android](24721): [FBP] onMethodCall: stopScan
D/BluetoothAdapter(24721): isLeEnabled(): ON
D/[FBP-Android](24721): [FBP] onMethodCall: startScan
D/BluetoothAdapter(24721): isLeEnabled(): ON
D/BluetoothLeScanner(24721): onScannerRegistered() - status=0 scannerId=4 mScannerId=0
D/[FBP-Android](24721): [FBP] onMethodCall: stopScan
D/BluetoothAdapter(24721): isLeEnabled(): ON
chipweinberger commented 1 year ago

the logs seem pretty clear.

have you set permissions?

also it appears you are calling stopScan too quickly?

Mehdi-Mouilbeau commented 1 year ago

here's my AndroidManifest for permissions :

New Bluetooth permissions in Android 12 uses-permission android:name="android.permission.BLUETOOTH_SCAN"/> uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> legacy for Android 11 or lower --> uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" /> Needed only if your app makes the device discoverable to Bluetooth devices. uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

That's how the problem arose. I don't think I changed anything. I even went back to an older commit or another branch to test and the problem is still there. I thought it was an environment problem, so I tested on windows and apple, same problem. I haven't changed the way I call stopscan since it worked like that. I can try.

chipweinberger commented 1 year ago

it says you need BLUETOOTH_PRIVILEGED

Mehdi-Mouilbeau commented 1 year ago

I've tried it with and without, and nothing has changed.

chipweinberger commented 1 year ago

lmk if you figure it out

Mehdi-Mouilbeau commented 1 year ago

Hello, We were able to target the problem by removing the withServices parameter from the startscan. There seems to be an incompatibility with Android when trying to access the Guid (hence the bluetooth privileged permission). Fyi, result is the same with or wihtout this permission in the manifest. On the first scan it finds several Bluetooth devices, but on the next ones it finds none.

chipweinberger commented 1 year ago

"Restricted Permission: The BLUETOOTH_PRIVILEGED permission is a protected-level permission. This means it's not available to all apps. Typically, only system apps or apps signed with the system's signing key can obtain this permission. Regular third-party apps cannot use this permission due to security reasons."

yes seems like you're doing something that is not allowed.

chipweinberger commented 1 year ago

what service are you trying to scan for?

cl-lumoshelmet commented 1 year ago

Same happened to me when I tried scanning with the withNames parameter. I ended up dropping it and revert to a manual .where filter

chipweinberger commented 1 year ago

@cl-lumoshelmet which android version?

cl-lumoshelmet commented 1 year ago

I'm running on a Huawei Mate 30 (EMUI 12), Android 10

Mehdi-Mouilbeau commented 1 year ago

Surely something happened on the Android side because overnight, it stopped working without having changed anything in my code. I solved the problem by removing the withServices from startScan.

For information, this is not a system application.

chipweinberger commented 1 year ago

what service are you scanning for

chipweinberger commented 1 year ago

Need BLUETOOTH_PRIVILEGED permission: Neither user 10502 nor current process has android.permission.BLUETOOTH_PRIVILEGED.

According to this issue, your bluetooth is not enabled.

You can update to 1.27.6 or later. I added checks for bluetooth being off before we scan.

If this does not fix it, please reopen a issue.