Skip to content

Commit

Permalink
Deinitialize in onDetachedFromEngine on Android (#519)
Browse files Browse the repository at this point in the history
* Deinitialize in onDetachedFromEngine on Android

* Fixed crash with no devices connected
Use current instance as method handler to prevent issues
Fixed some typos
  • Loading branch information
Rexios80 authored Feb 21, 2022
1 parent c9310ac commit 8d66469
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class PluginController {
bleStatusChannel.setStreamHandler(bleStatusHandler)
}

internal fun deinitialize() {
scandevicesHandler.stopDeviceScan()
deviceConnectionHandler.disconnectAll()
}

internal fun execute(call: MethodCall, result: Result) {
pluginMethods[call.method]?.invoke(call, result) ?: result.notImplemented()
}
Expand All @@ -81,8 +86,7 @@ class PluginController {
}

private fun deinitializeClient(call: MethodCall, result: Result) {
scandevicesHandler.stopDeviceScan()
deviceConnectionHandler.disconnectAll()
deinitialize()
result.success(null)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,32 @@ import io.flutter.plugin.common.MethodChannel.Result

class ReactiveBlePlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
initalizePlugin(binding.binaryMessenger, binding.applicationContext)
initializePlugin(binding.binaryMessenger, binding.applicationContext, this)
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
// deinitalize logic
deinitializePlugin()
}

companion object {
lateinit var pluginController: PluginController

@JvmStatic
private fun initalizePlugin(messenger: BinaryMessenger, context: Context) {
private fun initializePlugin(
messenger: BinaryMessenger,
context: Context,
plugin: ReactiveBlePlugin
) {
val channel = MethodChannel(messenger, "flutter_reactive_ble_method")
channel.setMethodCallHandler(ReactiveBlePlugin())
channel.setMethodCallHandler(plugin)
pluginController = PluginController()
pluginController.initialize(messenger, context)
}

@JvmStatic
private fun deinitializePlugin() {
pluginController.deinitialize()
}
}

override fun onMethodCall(call: MethodCall, result: Result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ open class ReactiveBleClient(private val context: Context) : BleClient {

lateinit var rxBleClient: RxBleClient
internal set
internal lateinit var activeConnections: MutableMap<String, DeviceConnector>
internal var activeConnections = mutableMapOf<String, DeviceConnector>()
}

override val connectionUpdateSubject: BehaviorSubject<ConnectionUpdate>
Expand Down

0 comments on commit 8d66469

Please sign in to comment.