yanshouwang / bluetooth_low_energy

A Flutter plugin for controlling the bluetooth low energy.
https://pub.dev/packages/bluetooth_low_energy
MIT License
49 stars 16 forks source link

Authorize bluetooth permissions on android #103

Open pimlie opened 1 week ago

pimlie commented 1 week ago

Should bluetooth_low_energy be managing bluetooth permissions by itself on android?

I looked at the android example, but I only see authorize and/or showAppSettings being called but when I call either of these then only the app settings window is shown. And from the app settings dialog, it doesn't grant bluetooth permissions nor is it possible to add those permissions manually.

When using the flutter_blue_plus bluetooth library the Android permissions are granted automatically, so not sure if I'm doing something wrong?

My code basically just looks like this:

CentralManager backend = new CentralManager();
// listen on stateChanged stream and if not authorized then:
await backend.authorize();

but I cannot get the manager.stateChanged stream to return anything other then BluetoothLowEnergyState.unauthorized unfortunately?

yanshouwang commented 4 days ago

If the user has denied the premission, the authorize will always return false and the state will keep unauthorized even if you call the authorize method. Then you should call the showAppSettings method to let the user grant the permission from the app settings page manually. After that, when the app back to the foreground, the stateChanged will return authorized.

You can also request the permission by youself instead of use the methods provided by this plugin. The plugin will always refresh the state when the permission state has changed.