Skip to content

Commit

Permalink
Refine exception handling in Android plugin, fix #91 and #149
Browse files Browse the repository at this point in the history
Signed-off-by: Shengqi Chen <harry-chen@outlook.com>
  • Loading branch information
Harry-Chen committed Apr 4, 2024
1 parent f015aa8 commit dacfb96
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,17 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private fun pollTag(nfcAdapter: NfcAdapter, result: Result, timeout: Int, technologies: Int) {

pollingTimeoutTask = Timer().schedule(timeout.toLong()) {
if (activity.get() != null) {
nfcAdapter.disableReaderMode(activity.get())
try {
if (activity.get() != null) {
nfcAdapter.disableReaderMode(activity.get())
}
} catch (ex: Exception) {
Log.w(TAG, "Cannot disable reader mode", ex)
}
result.error("408", "Polling tag timeout", null)
}

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

// common fields
Expand Down Expand Up @@ -592,8 +596,14 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}

result.success(jsonResult.toString())
}

}, technologies, null)
try {
nfcAdapter.enableReaderMode(activity.get(), pollHandler, technologies, null)
} catch (ex: Exception) {
Log.e(TAG, "Cannot enable reader mode", ex)
result.error("500", "Cannot enable reader mode", ex.localizedMessage)
}
}

private class MethodResultWrapper(result: Result) : Result {
Expand Down

0 comments on commit dacfb96

Please sign in to comment.