flutter_blue icon indicating copy to clipboard operation
flutter_blue copied to clipboard

Don't initialize CBCentralManager until needed

Open rohansingh opened this issue 4 years ago • 7 comments

By deferring the initialization of CBCentralManager, we are able to defer prompting for Bluetooth permission on iOS until a flutter_blue method is called. Otherwise, the Bluetooth permission prompt appears right when the app starts up.

rohansingh avatar Jun 03 '20 14:06 rohansingh

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jun 03 '20 14:06 CLAassistant

This is really important for any production app that wants a reasonable user experience!

qimingweng avatar Jun 11 '20 04:06 qimingweng

You saved my life!

papaoloba avatar Jun 18 '20 21:06 papaoloba

@pauldemarco do you have any opinion on this pull request?

qimingweng avatar Jun 19 '20 18:06 qimingweng

For those who want to use this PR before it is merged:

dependencies:
  flutter_blue:
    # TODO: revert to official version of flutter_blue when this is merged: https://github.com/pauldemarco/flutter_blue/pull/599
    git:
      url: https://github.com/tidbyt/flutter_blue.git
      ref: 4ed5e9980139a9521fcd4dcdd7a97084875bb7b6

And of course don't forget to subscribe to this PR.

rockerhieu avatar Jul 05 '20 15:07 rockerhieu

@rohansingh This PR has the unfortunate side effect where calling isOn always returns false if centralManager is initialized in the same call. This is caused by race condition of reading the Bluetooth state while the Bluetooth subsystem is still initializing.

As a workaround, I just added a small delay between the centralManager init call and the isOn call.

jasaw avatar Oct 25 '21 13:10 jasaw

@jasaw Did you insert the delay in FlitterBluePlugin.m? Can you share the implementation?

ihorkozar avatar May 11 '22 13:05 ihorkozar