flutter_blue
flutter_blue copied to clipboard
Supporting Android 12 Bluetooth permissions.
For #864, I've added codes to handle certain API calls. I'm not sure whether it is fully handle all the calls but at least the example code works correctly.
There are also some problem on declaring permission on AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pauldemarco.flutter_blue">
<!-- New Bluetooth permissions in Android 12
https://developer.android.com/about/versions/12/features/bluetooth-permissions
-->
<!-- Include "neverForLocation" only if you can strongly assert that
your app never derives physical location from Bluetooth scan results. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
</manifest>
The android.permission.BLUETOOTH_SCAN has android:usesPermissionFlags="neverForLocation" but I'm not sure whether all the applications should use the flag. But, without the flag, scanning does not work correctly...
@espresso3389 From the current experience, I advise you not to use compileSdkVersion 31 until flutter move packages to 31 as default. version 30 is backward compatible, so I don't see a reason to upgrade to 31.
@boskokg Yes, I understand what you're saying. But certain features I'm using on my app force me to upgrade the SDK to 31 now...
I understand. But you should also understand that other plugins are not updates and this plugin should not block others that must use other plugins and there is no intentions to upgrate compile version soon, until flutter do that on their plugins. If you must compile with 31, you will probably have to use only your branch.
@boskokg OK, I totally agree with you. Anyway, I want to keep #864 and #940 until flutter_blue get able to accept SDK upgrade.
@espresso3389 perhaps you can link a fork to this with your changes as it seems the repo is no longer maintained?
@espresso3389 @boskokg running boskokg/flutter_blue with this PR merged I'm getting this exception on Android. Verified that apk has BLUETOOTH_CONNECT permission in the manifest.
I also have compileSdkVersion 31 (because the camera plugin is chocking on 30). E.g. see https://github.com/flutter/plugins/commit/9e46048ad2e1f085c1e8f6c77391fa52025e681f
E/AndroidRuntime( 4201): java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for android.content.AttributionSource@97ea7dbe: AdapterService getRemoteName
E/AndroidRuntime( 4201): at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
E/AndroidRuntime( 4201): at android.os.Parcel.createException(Parcel.java:2409)
E/AndroidRuntime( 4201): at android.os.Parcel.readException(Parcel.java:2392)
E/AndroidRuntime( 4201): at android.os.Parcel.readException(Parcel.java:2334)
E/AndroidRuntime( 4201): at android.bluetooth.IBluetooth$Stub$Proxy.getRemoteName(IBluetooth.java:3395)
E/AndroidRuntime( 4201): at android.bluetooth.BluetoothDevice.getName(BluetoothDevice.java:1273)
E/AndroidRuntime( 4201): at com.pauldemarco.flutter_blue.ProtoMaker.from(ProtoMaker.java:99)
E/AndroidRuntime( 4201): at com.pauldemarco.flutter_blue.ProtoMaker.from(ProtoMaker.java:49)
E/AndroidRuntime( 4201): at com.pauldemarco.flutter_blue.FlutterBluePlugin$2.onScanResult(FlutterBluePlugin.java:813)
E/AndroidRuntime( 4201): at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$1.run(BluetoothLeScanner.java:531)
E/AndroidRuntime( 4201): at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime( 4201): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4201): at android.os.Looper.loopOnce(Looper.java:201)
E/AndroidRuntime( 4201): at android.os.Looper.loop(Looper.java:288)
E/AndroidRuntime( 4201): at android.app.ActivityThread.main(ActivityThread.java:7842)
E/AndroidRuntime( 4201): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 4201): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime( 4201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
@ekuleshov Could you please show me how you use flutter_blue on your code? I'm not sure whether the patch covered all the use cases or not; some of the functions are not correctly requiring permission.
@espresso3389 thank you for looking into this
I'm basically doing this:
Future<Stream<TimerScanResult>> _scanForDevices() async {
try {
await _stopScan();
return FlutterBlue.instance.scan(timeout: _scanDuration) //
.map((res) => _createDevice(res))
.where((res) => res != null);
...
I'm running on Android 12 and on a first run of the app I see the system permission request dialog and the app shows a "nearby devices" permission allowed after that.
I just tried to remove that permission from the app. So, the app stopped failing after that and scan obviously returned nothing and no permission request been brought up on the scan attempts.
But once I gave "nearby devices" permission back to the app all seem to be working. No crashes and scan works.
Now I'm puzzled if that has something to do with the old Bluetooth permissions that app had.
@ekuleshov Same issue is reported here but I could not reproduce the issue with flutter_blue sample and my own codes...
Can you provide me with the complete sample that causes the issue?
@espresso3389 Hello, using your solution I get interesting behavior. On Android 11, the application asks for permission for the location, I give it to him, but the scan does not find the device until the location is turned on. On Android 9 and Android 10 everything works fine, can you tell me what could be the reason for this behavior ?
So what is the status for android 12 ? Still not merged ? Do we have to use flutter_blue_plus ? Do we need to use min sdk 31 ?
So what is the status for android 12 ? Still not merged ? Do we have to use flutter_blue_plus ? Do we need to use min sdk 31 ?
I was facing the same problem, but when I used flutter_blue_plus, it shows me the devices near me, thank you a lot @vricosti