Skip to content

Commit dcb6986

Browse files
committed
fix state related issue
1 parent 76a8b01 commit dcb6986

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

android/src/main/kotlin/im/nfc/ccid/CcidPlugin.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ class CcidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
5353
}
5454
}
5555
}
56+
if (intent.action == UsbManager.ACTION_USB_DEVICE_DETACHED) {
57+
synchronized(this) {
58+
val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
59+
device?.let { detachedDevice ->
60+
readers.entries.removeIf { (_, reader) ->
61+
reader.deviceName == detachedDevice.deviceName
62+
}
63+
Log.d(TAG, "USB device detached: $detachedDevice")
64+
}
65+
}
66+
}
5667
}
5768
}
5869

@@ -120,6 +131,7 @@ class CcidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
120131
} else {
121132
context.registerReceiver(usbReceiver, IntentFilter(ACTION_USB_PERMISSION))
122133
}
134+
context.registerReceiver(usbReceiver, IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED))
123135
}
124136

125137
override fun onDetachedFromActivityForConfigChanges() {}
@@ -153,6 +165,12 @@ class CcidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
153165
}
154166
}
155167

168+
newReaders.forEach { (name, _) ->
169+
if (readers.containsKey(name)) {
170+
newReaders[name] = readers[name]!!
171+
}
172+
}
173+
156174
readers = newReaders
157175
return readers.keys.toList()
158176
}

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ void main() {
66
}
77

88
class MyApp extends StatelessWidget {
9-
const MyApp({Key? key}) : super(key: key);
9+
const MyApp({super.key});
1010

1111
@override
1212
Widget build(BuildContext context) {
@@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {
2121
}
2222

2323
class MyHomePage extends StatefulWidget {
24-
const MyHomePage({Key? key}) : super(key: key);
24+
const MyHomePage({super.key});
2525

2626
@override
2727
State<MyHomePage> createState() => _MyHomePageState();

0 commit comments

Comments
 (0)