Skip to content

Commit 88a5725

Browse files
authored
Merge pull request #228 from privacybydesign/android-poll-fix
Add option to specify `EXTRA_READER_PRESENCE_CHECK_DELAY` on Android
2 parents 0646493 + bb6fb63 commit 88a5725

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.nfc.NfcAdapter
88
import android.nfc.NfcAdapter.*
99
import android.nfc.Tag
1010
import android.nfc.tech.*
11+
import android.os.Bundle
1112
import android.os.Handler
1213
import android.os.HandlerThread
1314
import android.os.Looper
@@ -86,13 +87,13 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
8687
Log.e(TAG, "$desc error", ex)
8788
val excMessage = ex.localizedMessage
8889
when (ex) {
89-
is IOException -> result?.error("500", "Communication error", excMessage)
90-
is SecurityException -> result?.error("503", "Tag already removed", excMessage)
91-
is FormatException -> result?.error("400", "NDEF format error", excMessage)
92-
is InvocationTargetException -> result?.error("500", "Communication error", excMessage)
93-
is IllegalArgumentException -> result?.error("400", "Command format error", excMessage)
94-
is NoSuchMethodException -> result?.error("405", "Transceive not supported for this type of card", excMessage)
95-
else -> result?.error("500", "Unhandled error", excMessage)
90+
is IOException -> result.error("500", "Communication error", excMessage)
91+
is SecurityException -> result.error("503", "Tag already removed", excMessage)
92+
is FormatException -> result.error("400", "NDEF format error", excMessage)
93+
is InvocationTargetException -> result.error("500", "Communication error", excMessage)
94+
is IllegalArgumentException -> result.error("400", "Command format error", excMessage)
95+
is NoSuchMethodException -> result.error("405", "Transceive not supported for this type of card", excMessage)
96+
else -> result.error("500", "Unhandled error", excMessage)
9697
}
9798
}
9899
}
@@ -141,7 +142,7 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
141142
tagTechnology = isoDep
142143
// historicalBytes() may return null but is wrongly typed as ByteArray!
143144
// https://developer.android.com/reference/kotlin/android/nfc/tech/IsoDep#gethistoricalbytes
144-
historicalBytes = (isoDep.historicalBytes as ByteArray?)?.toHexString() ?: ""
145+
historicalBytes = isoDep.historicalBytes?.toHexString() ?: ""
145146
}
146147
tag.techList.contains(MifareClassic::class.java.name) -> {
147148
standard = "ISO 14443-3 (Type A)"
@@ -329,8 +330,10 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
329330
val timeout = call.argument<Int>("timeout")!!
330331
// technology and option bits are set in Dart code
331332
val technologies = call.argument<Int>("technologies")!!
333+
val extraPresenceDelay = call.argument<Int>("extra_reader_presence_check_delay")
334+
332335
runOnNfcThread(result, "Poll") {
333-
pollTag(nfcAdapter, result, timeout, technologies)
336+
pollTag(nfcAdapter, result, timeout, technologies, extraPresenceDelay)
334337
}
335338
}
336339

@@ -583,8 +586,7 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
583586

584587
override fun onDetachedFromActivityForConfigChanges() {}
585588

586-
private fun pollTag(nfcAdapter: NfcAdapter, result: Result, timeout: Int, technologies: Int) {
587-
589+
private fun pollTag(nfcAdapter: NfcAdapter, result: Result, timeout: Int, technologies: Int, extraReaderPresenceCheckDelay: Int?) {
588590
pollingTimeoutTask = Timer().schedule(timeout.toLong()) {
589591
try {
590592
if (activity.get() != null) {
@@ -604,7 +606,11 @@ class FlutterNfcKitPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
604606
result.success(jsonResult)
605607
}
606608

607-
nfcAdapter.enableReaderMode(activity.get(), pollHandler, technologies, null)
609+
val options = Bundle()
610+
if (extraReaderPresenceCheckDelay != null) {
611+
options.putInt(EXTRA_READER_PRESENCE_CHECK_DELAY, extraReaderPresenceCheckDelay)
612+
}
613+
nfcAdapter.enableReaderMode(activity.get(), pollHandler, technologies, options)
608614
}
609615

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

lib/flutter_nfc_kit.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ class FlutterNfcKit {
337337
bool readIso18092 = false,
338338
bool readIso15693 = true,
339339
bool probeWebUSBMagic = false,
340+
Duration? extraReaderPresenceCheckDelay,
340341
}) async {
341342
// use a bitmask for compact representation
342343
int technologies = 0x0;
@@ -354,6 +355,9 @@ class FlutterNfcKit {
354355
'iosMultipleTagMessage': iosMultipleTagMessage,
355356
'technologies': technologies,
356357
'probeWebUSBMagic': probeWebUSBMagic,
358+
if (extraReaderPresenceCheckDelay != null)
359+
"extra_reader_presence_check_delay":
360+
extraReaderPresenceCheckDelay.inMilliseconds,
357361
});
358362
return NFCTag.fromJson(jsonDecode(data));
359363
}

0 commit comments

Comments
 (0)