Whats the easiest way to implement custom nfc manager?

I'm working on Android application which needs to support basic NFC operations on both usual Android phones and Android based payment terminals. These terminals have hardware for payment operations like printing bills and reading contact bank cards. They also have heavily modified firmware for security reasons and they don't support standard Android NFC api. Instead their manufacturers provide SDKs for interacting with hardware.

I want implement custom NFC manager interface which would choose different methods to work with NFC depending on a device running the application. The only thing I need is reading NFC tag id and authorized reading of Mifare Classic blocks.

Since the SDKs don't have anything like foreground dispatch and Tag class, I need to to do something like this:

interface MyNfcManager{
    fun startNfc()
    fun stopNfc()
}

interace MyNfcListener{
    fun onTagIdDiscovered(id: ByteArray)
    fun onMifareDataRead(data: ByteArray)
}

Is there any other easier way to implement it than writing from scratch and without diving into low level and native libraries?

I've found nfc related libraries:
https://github.com/grundid/nfctools
https://github.com/skjolber/external-nfc-api

But I can't find anything to use from them

Upvotes: 0

Views: 410

Answers (1)

Andrew
Andrew

Reputation: 10232

So the short Answer is that it is very unlikely that anybody has done this very custom work, the people most likely to have done it are the Terminal manufacturer and as you don't specify who that is then it is very difficult to answer if this has been done.

Most of the NFC stuff is handled by the OS on standard Android and therefore you are really limited to wrapping it in a library, to simplify doing the custom stuff you want that is outside of the NFC specifications.

Again as you don't specify what the Mifare Classic is being used for then it is impossible to say if anybody has done a library for this.

So with the information given (or lack of it) it boils down to handling this very custom stuff yourself, below is a minimum you will need to do, this does not involve native code and very low level hardware stuff (which you won't be able to do on standard Android unless you have rooted the phone anyway):-

So in standard Android reading a Mifare Classic is supported but at a fairly low level (you need to know which block to read and the format of the data in the block)

See https://developer.android.com/reference/android/nfc/tech/MifareClassic

The way I would approach this to try and identify the device running on to try the right method of access.

One method of identification is to try the standard Android method and if the return of NfcAdapter.getDefaultAdapter(Context)

Is NULL then use custom SDK commands (As I would not expect the custom hardware not be Registered as an NFC_Service with Android)

Is NOT NULL then create a pending Intent for ACTION_TECH_DISCOVERED

enableForegroundDispatch with this Intent to get notified when a Tag is read by the OS

Then in the onNewIntent method:-

Check that MifareClassic is a supported Tech (Not all hardware supports MifareClassic) else use NfcA instead.

Upvotes: 1

Related Questions