kenjdavidson / react-native-bluetooth-classic

⚛ Bluetooth classic Android(Bluetooth)/IOS(ExternalAccessory) module for serial communication
https://kenjdavidson.github.io/react-native-bluetooth-classic
MIT License
250 stars 93 forks source link

[iOS] Ask for bluetooth permission only when truly required #191

Closed fdrault closed 2 years ago

fdrault commented 2 years ago

Hello, first of all, thank you for your amazing work

On iOS, using your library automatically prompt a "Turn On Bluetooth to Allow [app name] to Connect to Accessories" dialog on app launch. IMG_2828

For a better user experience, it is recommended to ask the user to enable bluetooth when the application really need it.

Found out that initialize CBCentralManager automatically prompt the bluetooth dialog by default. See CBCentralManagerOptionShowPowerAlertKey

To have the dialog prompt at right timing, CBManager can be initialized lazily, therefore, the permission dialog is prompt when needed instead of startup. The first call of any bluetooth related method will trigger checkBluetoothAdapter() , initialize CBManager and prompt dialog.

They might be another way to achieve this result, (like setting CBCentralManagerOptionShowPowerAlertKey to 0 and ask to enable bluetooth later on) but I found that asking for enabling bluetooth on usage is the most natural way.

kenjdavidson commented 2 years ago

You're a swift master! Thanks for the feature.