Skip to content

Commit dacfb96

Browse files
committed
Refine exception handling in Android plugin, fix #91 and #149
Signed-off-by: Shengqi Chen <harry-chen@outlook.com>
1 parent f015aa8 commit dacfb96

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

android/src/main/kotlin/im/nfc/flutter_nfc_kit/FlutterNfcKitPlugin.kt

+14-4
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,17 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
441441
private fun pollTag(nfcAdapter: NfcAdapter, result: Result, timeout: Int, technologies: Int) {
442442

443443
pollingTimeoutTask = Timer().schedule(timeout.toLong()) {
444-
if (activity.get() != null) {
445-
nfcAdapter.disableReaderMode(activity.get())
444+
try {
445+
if (activity.get() != null) {
446+
nfcAdapter.disableReaderMode(activity.get())
447+
}
448+
} catch (ex: Exception) {
449+
Log.w(TAG, "Cannot disable reader mode", ex)
446450
}
447451
result.error("408", "Polling tag timeout", null)
448452
}
449453

450-
nfcAdapter.enableReaderMode(activity.get(), { tag ->
454+
val pollHandler: NfcAdapter.ReaderCallback = { tag ->
451455
pollingTimeoutTask?.cancel()
452456

453457
// common fields
@@ -592,8 +596,14 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
592596
}
593597

594598
result.success(jsonResult.toString())
599+
}
595600

596-
}, technologies, null)
601+
try {
602+
nfcAdapter.enableReaderMode(activity.get(), pollHandler, technologies, null)
603+
} catch (ex: Exception) {
604+
Log.e(TAG, "Cannot enable reader mode", ex)
605+
result.error("500", "Cannot enable reader mode", ex.localizedMessage)
606+
}
597607
}
598608

599609
private class MethodResultWrapper(result: Result) : Result {

0 commit comments

Comments
 (0)