Skip to content

Commit

Permalink
Fix: resolve android crashes (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
matejglejtek authored Jan 5, 2025
2 parents f343d0f + 6d85ac4 commit bdbd3b4
Showing 1 changed file with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,36 @@ class FlutterOpendroneidPlugin : FlutterPlugin, ActivityAware, Pigeon.Api {
Pigeon.Api.setup(flutterPluginBinding.binaryMessenger, this)

StreamHandler.bindMultipleHandlers(
flutterPluginBinding.binaryMessenger,
mapOf(
"flutter_odid_data_bt" to bluetoothOdidPayloadStreamHandler,
"flutter_odid_data_wifi" to wifiOdidPayloadStreamHandler,
"flutter_odid_state_bt" to bluetoothStateStreamHandler,
"flutter_odid_state_wifi" to wifiStateStreamHandler
)
flutterPluginBinding.binaryMessenger,
mapOf(
"flutter_odid_data_bt" to bluetoothOdidPayloadStreamHandler,
"flutter_odid_data_wifi" to wifiOdidPayloadStreamHandler,
"flutter_odid_state_bt" to bluetoothStateStreamHandler,
"flutter_odid_state_wifi" to wifiStateStreamHandler
)
)

context = flutterPluginBinding.applicationContext

val wifiManager: WifiManager? =
context.getSystemService(Context.WIFI_SERVICE) as WifiManager?
context.getSystemService(Context.WIFI_SERVICE) as WifiManager?
val wifiAwareManager: WifiAwareManager? =
context.getSystemService(Context.WIFI_AWARE_SERVICE) as WifiAwareManager?

context.getSystemService(Context.WIFI_AWARE_SERVICE) as WifiAwareManager?

wifiScanner =
WifiScanner(
wifiOdidPayloadStreamHandler, wifiStateStreamHandler, wifiManager, context
)
WifiScanner(
wifiOdidPayloadStreamHandler, wifiStateStreamHandler, wifiManager, context
)
wifiNaNScanner =
WifiNaNScanner(
wifiOdidPayloadStreamHandler, wifiStateStreamHandler, wifiAwareManager, context
)
WifiNaNScanner(
wifiOdidPayloadStreamHandler, wifiStateStreamHandler, wifiAwareManager, context
)
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
binding.activity.registerReceiver(
scanner.adapterStateReceiver,
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
scanner.adapterStateReceiver,
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
)
binding.activity.registerReceiver(
wifiScanner.adapterStateReceiver,
Expand All @@ -82,20 +81,24 @@ class FlutterOpendroneidPlugin : FlutterPlugin, ActivityAware, Pigeon.Api {
boundActivity = binding.activity
}

override fun onDetachedFromActivityForConfigChanges() {
boundActivity?.unregisterReceiver(scanner.adapterStateReceiver)
}
override fun onDetachedFromActivityForConfigChanges() {}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {}

override fun onDetachedFromActivity() {}
override fun onDetachedFromActivity() {
boundActivity?.unregisterReceiver(scanner.adapterStateReceiver)
boundActivity?.unregisterReceiver(wifiScanner.adapterStateReceiver)
}

@RequiresApi(Build.VERSION_CODES.O)
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
Pigeon.Api.setup(binding.binaryMessenger, null)
scanner.cancel()
wifiScanner.cancel()
wifiNaNScanner.cancel()
if(scanner.isScanning)
scanner.cancel()
if(wifiScanner.isScanning)
wifiScanner.cancel()
if(wifiNaNScanner.isScanning)
wifiNaNScanner.cancel()
StreamHandler.clearMultipleHandlers(
binding.binaryMessenger,
listOf(
Expand Down

0 comments on commit bdbd3b4

Please sign in to comment.