flutter_reactive_ble
flutter_reactive_ble copied to clipboard
Make Plugin Controller not static so it creates new instance for each Flutter Engine
I encountered a bug with this package when using together with flutter_foreground_task plugin.
flutter_foreground_task plugin creates a separate Flutter Engine in android foreground task to perform some work. This engine also initializes flutter_reactive_ble plugin.
However, because the PluginController instance is static, it would deinitialize the plugin when Flutter Engine that the app uses is destroyed (on app force close) which leaves the plugin deinitialized for the potentionally running Flutter Engine in the foreground task.
This PR ensures that each Flutter Engine has its own instance of the PluginController which deintializes only if the given engine is destroyed. This allows for plugin usage within background contexts like the foreground task on android.
The change is only for android at the moment. I don't have equipment to implement and test this on the iOS side.
Fixes https://github.com/PhilipsHue/flutter_reactive_ble/issues/687 https://github.com/PhilipsHue/flutter_reactive_ble/issues/672
This fix is important since the underlying bug turns the plugin useless in any project that creates – or has plugins that create – a new FlutterEngine. An additional issue remains: Whenever a FlutterEngine is destroyed the plugin will also disconnect from all connections or stop scanning. E.g. if a background task is finished or in hybrid apps that have some parts of the UI native and some parts in Flutter.