Skip to content

Commit b4d99f3

Browse files
committed
Add more null check in Android plugin, fix semlette#61 and semlette#71
Signed-off-by: Harry Chen <i@harrychen.xyz>
1 parent cf9a392 commit b4d99f3

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
6161

6262
private fun handleMethodCall(call: MethodCall, result: Result) {
6363

64+
if (activity == null) {
65+
result.error("500", "Cannot call method when not attached to activity", null)
66+
return
67+
}
68+
6469
val nfcAdapter = getDefaultAdapter(activity)
6570

6671
if (nfcAdapter?.isEnabled != true && call.method != "getNFCAvailability") {
@@ -123,7 +128,9 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
123128
} catch (ex: IOException) {
124129
Log.e(TAG, "Close tag error", ex)
125130
}
126-
nfcAdapter.disableReaderMode(activity)
131+
if (activity != null) {
132+
nfcAdapter.disableReaderMode(activity)
133+
}
127134
result.success("")
128135
}
129136
}
@@ -301,7 +308,9 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
301308
private fun pollTag(nfcAdapter: NfcAdapter, result: Result, timeout: Int, technologies: Int) {
302309

303310
pollingTimeoutTask = Timer().schedule(timeout.toLong()) {
304-
nfcAdapter.disableReaderMode(activity)
311+
if (activity != null) {
312+
nfcAdapter.disableReaderMode(activity)
313+
}
305314
result.error("408", "Polling tag timeout", null)
306315
}
307316

0 commit comments

Comments
 (0)