flutter_bluetooth_serial icon indicating copy to clipboard operation
flutter_bluetooth_serial copied to clipboard

Android 12 Support

Open rickcasson opened this issue 3 years ago • 8 comments
trafficstars

Starting with Android SDK 31, permissions for scanning for available Bluetooth devices needs BLUETOOTH_SCAN and BLUETOOTH_CONNECT instead of ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION

rickcasson avatar Jan 13 '22 21:01 rickcasson

Hi @edufolly, there now seems to be 3 open PRs related to fixing bluetooth permissions in Android 12 (this one, as well as #162 and #163). Would you mind merging in which ever one you like best and closing the others? Thanks!

rickcasson avatar May 16 '22 23:05 rickcasson

Hey @rickcasson Thanks for creating the PR, I really appreciate your work.

I'm currently trying to use your PR for my own project. I want to use it on my OnePlus 9 Pro on Android 12. However it seems to have an issue with the bluetooth permissions.

When I try to open a bluetooth connection, it throws: "[...] lacks permission android.permission.BLUETOOTH". When I request the permission using the permission_handler package, it fails and logs: "Bluetooth permission missing in manifest".

I set my compileSdkVersion to 31 and therefore it makes sense that the Bluetooth permission is "missing" from the manifest (since maxSdkVersion is set to 30 on the permission). However I don't understand why my phone is expecting the BLUETOOTH permission even though it is Android 12. Do you have an idea what causes it? I am seriously confused.

When I remove android:maxSdkVersion="30" from the Manifest in the flutter_bluetooth_serial package, it works like a charm.

moritz157 avatar May 28 '22 16:05 moritz157

Hi @moritz157, thanks! I'm actually now thinking it might be more appropriate to remove these permissions entirely from the plugin so they can just be managed on the project side to avoid potential conflicts. I'll do some tests in the next few days, and if it still works, I'll make another push and update the README.

With regards to the permissions, can you confirm which ones you are checking for with the permissions_handler plugin? You'll need to make sure you are checking the correct ones depending on which SDK the device is running (Permission.bluetoothScan for > SDK 30, Permission.bluetooth <= SDK 30).

rickcasson avatar May 28 '22 17:05 rickcasson

If I am checking Permission.bluetooth it fails with "Bluetooth permission missing in manifest". If I am instead checking Permission.bluetoothScan it is granted but the connection fails because it lacks android.permission.BLUETOOTH. I expect the permissions_handler behaviour but I dont expect the lack of permission.

I would certainly support removing the permissions, since I couldn't manage to override the maxSdkVersion setting on the BLUETOOTH permission through my own Manifest (maybe you know, how I could override it)

moritz157 avatar May 28 '22 17:05 moritz157

I'm a bit sceptical if this repository is currently actively maintained. Since dec 2021 there was no update

CodeDoctorDE avatar May 28 '22 17:05 CodeDoctorDE

@moritz157 hmm, that's odd... you could try something like this for the time being to override the permission in your AndroidManifest:

  <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" tools:node="remove"/>
  <uses-permission android:name="android.permission.BLUETOOTH"/>

just make sure you have xmlns:tools="http://schemas.android.com/tools" included in your manifest header tag

rickcasson avatar May 28 '22 18:05 rickcasson

@CodeDoctorDE I'm not really sure either, but I've yet to find a good alternative... Also still not sure why there isn't an official Flutter Bluetooth plugin to be honest 😕

rickcasson avatar May 28 '22 18:05 rickcasson

<uses-permission android:name="android.permission.BLUETOOTH" tools:node="replace"/>did it for me 🚀 (just this line in my own Manifest, not 2 lines)

moritz157 avatar May 29 '22 17:05 moritz157