NFC Capture
For scanning the NFC, you must override onNewIntent
and onResume
methods
on the Activity
class are required.
Starting the capture
Overriding the onResume()
to enable NFC Adapter
- To initiate NFC scanning, NFCAdapter() is enabled in onResume(), taking into account the Fragment lifecycle.
override fun onResume() {
super.onResume()
val adapter = NfcAdapter.getDefaultAdapter(this)
if (adapter != null) {
val intent = Intent(applicationContext, this.javaClass)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(this, 0, Intent(this, javaClass)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), FLAG_MUTABLE)
} else{
PendingIntent.getActivity(this, 0, Intent(this, javaClass)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)
}
val filter = arrayOf(arrayOf("android.nfc.tech.IsoDep"))
adapter.enableForegroundDispatch(this, pendingIntent, null, filter)
}
}
Overriding the onPause()
to disable NFC Adapter
- In cases where the user moves away from the NFC screen, NFCAdapter() is disabled in onPause(). In this way, we remove the user's ability to accidentally/intentionally scan NFC after the NFC process.
override fun onPause() {
super.onPause()
val adapter = NfcAdapter.getDefaultAdapter(this)
adapter?.let {
it.disableForegroundDispatch(this)
}
}
Overriding the onNewIntent
for checking the Tag
- The onNewIntent method is automatically triggered by bringing the ID card closer to the NFC antenna area of the device. In this scenario, the NFC scanning process is started by passing the tag taken from the intent and the data group taken from the MRZ to the NFC scan method.
[!NOTE] How to get MRZ data? You can access MRZ with the getMRZ() function in the IDCapture() module. Click on the link for detailed information.
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
val tag = intent.extras!!.parcelable<Tag>(NfcAdapter.EXTRA_TAG)
Amani.sharedInstance().ScanNFC().start(
tag,
context,
birthDate, //MRZ data as YYMMDD format
expireDate, //MRZ data as YYMMDD format
documentNumber//MRZ data
) { bitmap, isSuccess, error ->
if (isSuccess) onComplete.invoke()
else onFailure.invoke()
}
}
- Intent/Bundle Extentions that may help to get tag
inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
Build.VERSION.SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
}
inline fun <reified T : Parcelable> Bundle.parcelable(key: String): T? = when {
Build.VERSION.SDK_INT >= 33 -> getParcelable(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelable(key) as? T
}
Uploading the NFC Data
- The upload method has to be called after the result of the start method is successful.
Otherwise you'll encounter an error in the
Errors()
object when called. :::
Amani.sharedInstance().ScanNFC()
.upload(
requireActivity(),
"document type", // Supplied by Amani. XXX_NF_0 is an example.
(uploadNFCSuccess) -> {
if (uploadNFCSuccess) {
Log.i(TAG, "NFC is uploaded")
} else {
Log.e(TAG, "NFC could not uploaded")
}
})