low level access to nfc in an smartphone using libnfc

Ariakan picture Ariakan · Apr 1, 2014 · Viewed 8.5k times · Source

I would like to use libnfc on a smartphone (I can use both Android or Windows phones). I need low level access to the NFC device because the standard API does not allow me to perform all the actions I need.

Is that possible? Is there a way to do with something other than libnfc? Can some one help me?

Answer

Michael Roland picture Michael Roland · Apr 3, 2014

Windows Phone

On a Windows Phone, I don't think that it will be easy for you to bypass the Windows API and directly access the NFC controller's driver layer with your own software stack.

Android

On Android, that depends on what libnfc you are talking about and on how deep you want to go into the device's Android system.

This libnfc supports only NXP's PN532 NFC controller and therefore doesn't work with current NFC controllers integrated into Android devices (NXP's PN544/PN547, Broadcom's BCM2079x). However, if you are talking about libnfc-nxp (for NXP's PN54x) or libnfc-nci (for Broadcom's BCM2079x and possibly other NCI-compliant chips) that's a different story. You might be able to use them or adapt them to your needs.

So the question is now how you could bypass the standard Android API and either use the existing NFC stack (libnfc-nci/nxp or Android NFC service) directly.

I would suggest that you first look into the functionality that the existing Android NFC service provides beyond what is available through the Android API. If that functionality is already sufficient, you could try to directly interface with the NFC service and simply use that functionality.

If you need more functionality it will get complicated: The NFC controller is integrated into the Android system through a kernel driver and is accessible as a Linux device file (under /dev/). The Android NFC service uses libnfc-nxp/nci to access that device file. So under normal circumstances, the device file is already used by the Android NFC service and therefore inaccessible/unusable by other libnfc-* instances. Moreover, only the NFC service (or rather its Linux user) has access to the device file.

So there are two options:

  1. You can modify the whole Android system (i.e. you create a customized Android system firmware). In that case, the easiest option would be to use the existing libnfc-* and Android NFC service source code and extend that with your functionality. You can even adapt the Android API of your custom firmware to your needs.

  2. You want to change a live system (i.e. you do not want to flash a customized firmware onto the device). In that case, you would need to replace the existing Android NFC service with your own NFC service that contains the extended functionality you need. YOu would certainly need root access to achieve any of that (as you will have to force the exsiting NFC service to shut down and as your replacement NFC service will need access to the NFC controller's device file).