Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables background usage of flutter-blue #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rmawatson
Copy link

@rmawatson rmawatson commented Feb 14, 2019

Android:
Swaps activity() calls for context() where only a context is required. The only place an activity is required is when requesting permissions. This patch will return an error when the plugin is used where permissions have not been granted, and there is no activity available to request permissions

iOS:
To use bluetooth-central background role and continue to processes in the background it is required that a unique ID (CBCentralManagerOptionRestoreIdentifierKey) be set when the CBCentralManager is initialized. (https://developer.apple.com/documentation/corebluetooth/cbcentralmanageroptionrestoreidentifierkey?language=objc).

Delays instanciation of the cbcentralmanager to when it is first used, so allow setting of a unique id from dart.

When CBCentralManagerOptionRestoreIdentifierKey is set, the CBCentralManagerDelegate must implement centralManager:willRestoreState. The current implementation is empty as it appears that setting a unique ID is sufficient (and the bluetooth-central role) is sufficient to stop iOS killing the app after a few minutes once it has been backgrounded iff a there is ongoing bluetooth activity (connection is active or pending, or a scan is in progress).

This has a side effect of printing an API-MISUSE warning when CBCentralManagerOptionRestoreIdentifierKey is not used, because the method centralManager:willRestoreState is always implemented. The warning is harmless when not using CBCentralManagerOptionRestoreIdentifierKey.

Dart:

To support the setting of a unique Id, introduces setUniqueId(String). This should be called before any methods that will instanciate the native instance of the cbcentralmanager.

@CLAassistant
Copy link

CLAassistant commented Feb 14, 2019

CLA assistant check
All committers have signed the CLA.

@rmawatson
Copy link
Author

rmawatson commented Feb 14, 2019

Additional Notes:

iOS:

Even when a unique ID is set, the app could still theoretically be killed for other reasons by iOS (although I have not experienced this).

If the app is killed by iOS at any point and bluetooth-tasks are ongoing, iOS will restart the app and willRestoreState method will be called on the CBCentralManagerDelegate. It allows about 10 seconds to handle the event, before the app is killed again. It may be nice to allow registering a handler in flutter that can be called to handle such events. This would be iOS only.

States that can be saved and restored:

https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/central_manager_state_restoration_options?language=objc

Additionally it would be good to be able to pass through the application restart reason. Currently flutter has no way of knowing why it was started and main() will run if it was started to handle something through willRestoreState. If it was started to handle this callback, the ideal would be run this in a standalone isolate, passing the various restored states to the handler, but allow the main to exit without attempting to run any code by checking the start reason.

Android:
Swaps activity() calls for context() where only a context is required. The only place an activity is required is when requesting permissions. This patch will return an error when the plugin is used where permissions have not been granted, and there is no activity available to request permissions

iOS:
To use bluetooth-central background role and continue to processes in the background it is required that a unique ID (CBCentralManagerOptionRestoreIdentifierKey) be set when the CBCentralManager is initialized. (https://developer.apple.com/documentation/corebluetooth/cbcentralmanageroptionrestoreidentifierkey?language=objc).

Delays instanciation of the cbcentralmanager to when it is first used, so allow setting of a unique id from dart.

When CBCentralManagerOptionRestoreIdentifierKey is set, the CBCentralManagerDelegate must implement centralManager:willRestoreState. The current implmentation is empty as it appears that setting a unique ID is sufficient to stop iOS killing the app once it has been backgrounded, if a bluetooth connection is active, or a scan is in progress.

This has a side effect of printing an API-MISUSE warning when CBCentralManagerOptionRestoreIdentifierKey is not used, because the method centralManager:willRestoreState is always implemented. The warning is harmless when not using CBCentralManagerOptionRestoreIdentifierKey.

Dart:

To support the setting of a unique Id, introduces setUniqueId(String). This should be called before any methods that will instanciate the native instance of the cbcentralmanager.
@rmawatson
Copy link
Author

changed PR branch and created new PR here

@fvisticot
Copy link

Any update of this issue ?

@AlexanderCollins
Copy link

Any Update :) ?

@pauldemarco pauldemarco added the isolate Using flutter_blue in a background isolate label Oct 18, 2019
@bobulus77dev
Copy link

is there a timeline or even a plan to intgrate this?

@freedreamer82
Copy link

any update ? I've been loooking a way to use the lib in background...

@valtbarbos
Copy link

Can we get this merged?
@pauldemarco is this on the roadmap to be merged? Lots of people needing this functionality.

@Drkstr
Copy link

Drkstr commented Feb 4, 2020

Do we have any updates on this ?

@icaglar
Copy link

icaglar commented Feb 6, 2020 via email

@TobiasHeidingsfeld
Copy link

We really need this. Any ETA?

@Arkangel12
Copy link

hello, i was working in somehit to help this covid pandemic and this its feature its gonna be really helpful, do you have a time where this will be implemented? i actually try to add the code to my code but is still not working.

will really appreciate any updated. thanks!!

@arc41t3ct
Copy link

Any new on this? Need this feature really bad and would love to get feedback.

@allanwhatsbusy
Copy link

any updates on this?

@vjgoutham
Copy link

vjgoutham commented Sep 4, 2020

Hi Paul, We all need this.Could you please let us know road map to merge this PR ?

@DevNico
Copy link

DevNico commented Sep 28, 2020

An ETA would be great also is it possible to allow for multiple unique ids?

@mdridley
Copy link

Looks like some merge conflicts, but definitely would be useful to have this ASAP.

@ramashish-headerlabs
Copy link

is there any update how can i scan results when app is in background

@JuniorJPDJ
Copy link

Why is this still open as you created #210 instead?

@M3sca
Copy link

M3sca commented Jan 27, 2021

Could you have this fix in last version of plugin??

@dorsamet
Copy link

dorsamet commented Mar 9, 2021

Can this fix go to the latest version? 👍 I consider this a highly requested feature by users

@ezranbayantemur
Copy link

Any update please?

@Jeancr123
Copy link

What is the current status of flutter blue background processes?

@xbug42
Copy link

xbug42 commented May 10, 2023

What is the current status of flutter blue background processes?

I think the future is to switch to FlutterBluePlus which is a fork of this inactive repository. It's almost a drop-in replacement and it works well in background...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
isolate Using flutter_blue in a background isolate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet