Scanning documents with NFC
Overview
MobbScan allows you to get all the information contained on a Passport or Spanish ID card v3 by scanning an NFC chip in case of being available on the device. NFC a 100% reliable data source and only a few configuration steps are required to get it done.
Configuration
Before starting the development you will need to follow these steps:
Add
MobbScan NFC extension
dependency to yourbuild.gradle
configuration file (dependencies section).Note: Please remember to change the versions for the ones available in the distribution file.
dependencies { ... implementation(name: 'mobbscan-nfc-extension-android', version: 'X.Y.Z', ext: 'aar') implementation ('org.jmrtd:jmrtd:0.7.18') implementation ('net.sf.scuba:scuba-sc-android:0.0.20') ... }
Add NFC permission and feature in the
AndroidManifest.xml
on your project:<!-- Required to access the NFC chip --> <uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
NFC scanning process
Note: To start the nfc scanning process you should have done the
startScan
method to get thescan id
previously. If you don't know how to to this, please visit the getting started guide.
// Set this value to true if you want to get the face of the document.
val getImageFace = true
NfcDocumentReader.getInstance().configureService(activity, getImageFace, object : OnNfcDocumentReaderFinish {
override fun onScanSuccess(scanResultData: MobbScanScanResultData) {
// Your scan success code here
}
override fun onScanFailed(error: NfcError) {
// Your scan failed code here
}
}) { nfcEvent, progress ->
// Your onEvent code here
}
// Set this value to true if you want to get the face of the document.
boolean getImageFace = true;
NfcDocumentReader.getInstance().configureService(activity, getImageFace, new OnNfcDocumentReaderFinish() {
@Override
public void onScanSuccess(final MobbScanScanResultData scanResultData) {
// Your scan success code here
}
@Override
public void onScanFailed(final NfcError error) {
// Your scan failed code here
}
}, new OnNfcEvent() {
@Override
public void onEvent(NFCEvent nfcEvent, int progress) {
// Your onEvent code here
}
});
This method receives four parameters, the activity, a flag that indicates if you want to get the image face of the document and two listeners. These listeners are used to update the interface and notify to the user at what point the process is. To know more about these listeners you can take a look to this section.
After calling the configureService(...)
method, you have to indicate to the reader to start the process. To do this, you have to call the method readDocumentNFC
. This method has two implementations:
Receives only one parameter, the NFC CAN number. This method starts the NFC reader to read ONLY the Spanish ID card V3.
NfcDocumentReader.getInstance().readDocumentNFC(canNumber);
Receives three parameters, the document number, the date of birth and the date of expiry. This method starts the NFC reader to read both documents, the Spanish ID card V3 and the Passport.
NfcDocumentReader.getInstance().readDocumentNFC(documentNumber, dateOfBirth, dateOfExpiry);
Tip: You can use a default scan to obtain all this data instead of asking for it to the user.
Listeners
Due to the scanning process being long and confusing for the final user, MobbScan provides the OnNfcEvent
listener in order to give the developers the option of updating the interface during the NFC scan process with information about the state of the scan.
The listener receives two parameters, the event and the progress.
private fun getOnNfcEvent(): OnNfcEvent? {
return OnNfcEvent { nfcEvent, progress ->
when (nfcEvent) {
NFCEvent.CONFIGURING_SERVICE -> { // Update UI}
NFCEvent.AUTHENTICATING -> { // Update UI }
NFCEvent.READING_DATA -> { // Update UI }
NFCEvent.READING_IMAGE -> { // Update UI }
}
}
}
private OnNfcEvent getOnNfcEvent() {
return new OnNfcEvent() {
@Override
public void onEvent(NFCEvent nfcEvent, int progress) {
switch (nfcEvent) {
case CONFIGURING_SERVICE:
// Update UI
break;
case AUTHENTICATING:
// Update UI
break;
case READING_DATA:
// Update UI
break;
case READING_IMAGE:
// Update UI
break;
}
}
};
}
Once we have finished the NFC events, we can manage the process with OnNfcDocumentReaderFinish
method (both for success and failure resuts):
private fun getOnNfcDocumentReaderFinish(): OnNfcDocumentReaderFinish? {
return object : OnNfcDocumentReaderFinish {
override fun onScanSuccess(scanResultData: MobbScanScanResultData) {
// Your scan success code here
}
override fun onScanFailed(error: NfcError) {
// Your scan failed code here
}
}
}
private OnNfcDocumentReaderFinish getOnNfcDocumentReaderFinish() {
return new OnNfcDocumentReaderFinish() {
@Override
public void onScanSuccess(MobbScanScanResultData scanResultData) {
// Your scan success code here
}
@Override
public void onScanFailed(NfcError error) {
// Your scan error code here
}
};
}
Tip: You can see an example of the NFC scan on the sample app provided with the SDK.