diff --git a/android/src/main/kotlin/com/nankai/flutter_nearby_connections/FlutterNearbyConnectionsPlugin.kt b/android/src/main/kotlin/com/nankai/flutter_nearby_connections/FlutterNearbyConnectionsPlugin.kt index 54f268c..f23465f 100644 --- a/android/src/main/kotlin/com/nankai/flutter_nearby_connections/FlutterNearbyConnectionsPlugin.kt +++ b/android/src/main/kotlin/com/nankai/flutter_nearby_connections/FlutterNearbyConnectionsPlugin.kt @@ -136,9 +136,11 @@ class FlutterNearbyConnectionsPlugin : FlutterPlugin, MethodCallHandler, Activit override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { channel.setMethodCallHandler(null) - serviceBindManager.mService?.stopAdvertising() - serviceBindManager.mService?.stopDiscovery() - serviceBindManager.unbindService() + if (this::serviceBindManager.isInitialized) { + serviceBindManager.mService?.stopAdvertising() + serviceBindManager.mService?.stopDiscovery() + serviceBindManager.unbindService() + } locationHelper = null exitProcess(0) } diff --git a/android/src/main/kotlin/com/nankai/flutter_nearby_connections/NearbyService.kt b/android/src/main/kotlin/com/nankai/flutter_nearby_connections/NearbyService.kt index ab16b94..abf3d53 100644 --- a/android/src/main/kotlin/com/nankai/flutter_nearby_connections/NearbyService.kt +++ b/android/src/main/kotlin/com/nankai/flutter_nearby_connections/NearbyService.kt @@ -92,10 +92,12 @@ class NearbyService : Service() { } override fun onDestroy() { + if (this::connectionsClient.isInitialized) { + stopAdvertising() + stopDiscovery() + connectionsClient.stopAllEndpoints() + } super.onDestroy() - stopAdvertising() - stopDiscovery() - connectionsClient.stopAllEndpoints() } private fun getNotification(): Notification? { @@ -118,4 +120,4 @@ class NearbyService : Service() { internal class LocalBinder(private val nearbyService: NearbyService) : Binder() { val service: NearbyService get() = nearbyService -} \ No newline at end of file +} diff --git a/ios/Classes/MPCManager.swift b/ios/Classes/MPCManager.swift index 9f0014d..b59c70b 100644 --- a/ios/Classes/MPCManager.swift +++ b/ios/Classes/MPCManager.swift @@ -167,6 +167,9 @@ extension MPCManager: MCNearbyServiceBrowserDelegate { func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) { // found peer, create a device with this peerID addNewDevice(for: peerID) + + // Inform about changed device state via NotificationCenter to guarantee main thread execution. + NotificationCenter.default.post(name: MPCManager.Notifications.deviceDidChangeState, object: nil) } func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {