NFC

From postmarketOS
Jump to navigation Jump to search

Near-field communication (NFC) can be used for contactless exchange of data over short distances. Different approaches exist on Linux to support it:

  • libnfc: userspace implementation, but mostly for desktop (e.g. USB) readers
  • Android NFC HAL: doesn't really work outside Android, probably mostly proprietary implementations
  • Linux NFC subsystem: NFC abstraction in the (mainline) Linux kernel with support for many different (also embedded) NFC chips

For smartphones/embedded devices the Linux NFC subsystem is the most suitable one, because it can be set up normally in the device tree like other hardware. Unfortunately, the NFC subsystem has been somewhat unmaintained for quite some time, and especially the userspace portion of it: Neard.

However, the main problem when testing NFC is a lack of applications that can make use of the Linux NFC subsystem. If you know any useful applications, please add them here. Also note that there is support for NFC/Neard in Qt if you want to build an application that makes use of NFC yourself.

Devices with NFC support

If your device runs the mainline kernel, and has a NFC chip that is supported in mainline's Linux NFC subsystem it should be fairly easy to add it to the device tree. Check the available NFC device tree bindings for details. Otherwise, these devices in postmarketOS already support NFC to some extent:

Device Codename Category
Samsung Galaxy A3 (2015) samsung-a3 community
Samsung Galaxy A5 (2015) samsung-a5 community
Samsung Galaxy E7 samsung-e7 community
Samsung Galaxy S4 Mini Value Edition samsung-serranove community
Xiaomi Mi Note 2 xiaomi-scorpio community
Google Pixel 3a google-sargo testing
Google (ASUS) Nexus 7 (2012) asus-grouper testing
Google (ASUS) Nexus 7 (2012) GSM asus-tilapia testing
Huawei Ascend G7 huawei-g7 testing
LGE Optimus Vu lg-p895 testing
Microsoft Lumia 640 microsoft-dempsey testing
Nokia Lumia 730 nokia-superman testing
Nokia Lumia 830 nokia-tesla testing
OnePlus 3T oneplus-oneplus3t testing
OnePlus 5 oneplus-cheeseburger testing
OnePlus 5T oneplus-dumpling testing
Samsung Galaxy A7 (2015) samsung-a7 testing
Samsung Galaxy Ace 2 GT-I8160 samsung-codina testing
Samsung Galaxy Ace 4 samsung-heatqlte testing
Samsung Galaxy Core Prime LTE samsung-cprime testing
Samsung Galaxy Grand Prime samsung-gprime, samsung-gprimeltecan, samsung-gprimeltexx, samsung-fortuna3g, samsung-fortunaltezt testing
Samsung Galaxy S Advance GT-I9070 samsung-janice testing
Xiaomi Mi 5 xiaomi-gemini testing
Xiaomi Mi 5s Plus xiaomi-natrium testing
Xiaomi Mi 6 xiaomi-sagit testing

Testing

At the moment there are not many useful applications for testing NFC. As a basic test, you can use nfctool (from apk add neard) to poll for devices:

$ nfctool -d nfc0 -1 -p
nfc0:
          Tags: [ ]
          Devices: [ ]
          Protocols: [ Felica MIFARE Jewel ISO-DEP ISO-15693 ]
          Powered: Yes
          RF Mode: None
          lto: 0
          rw: 0
          miux: 0

Start polling on nfc0 as initiator

Targets found for nfc0
  Tags: [ tag0 ]
  Devices: [ ]

If you hold some other NFC-capable device close to the NFC antenna, it should find a NFC tag and show it. Many chip cards nowadays have NFC, or maybe you can use another smartphone.

Note Note: You may need to search for a while to find a working spot on the phone close to the NFC antenna. It could be anywhere on the back or anywhere on the front so just search for a bit before you give up.

Reading NFC tags

For advanced usage you should start the Neard daemon (rc-service neard start). Neard then provides a DBUS API similar to BlueZ for Bluetooth that allows reading/writing NFC tags for example.

The easiest way to test it is using the Neard test scripts, for example with test-adapter and test-tag. Unfortunately Neard hasn't been updated for a long time so they still use Python 2, but there is an open pull request which you can use with Python 3

Upstream is looking for potential maintainers and testers of the Linux NFC subsystem, so if you would like to help, consider posting on the linux-nfc mailing list.

See also