Skip to content
Martin Paljak edited this page Jun 25, 2023 · 23 revisions

NFC tags with PC-s? Yes!

NFC tags are popular with mobile phones, because they require no additional software to interact with. Just tap a tag with a phone (assuming you hit the right place on the phone, where the antenna is) and BOOM! A website opens! Convenient, seamless and ubiquitous.

NFC4PC is a small unix style utility for working with NFC tags. It reads URL-s from tags, transforms them if needed, and runs actions with those URL-s, by default opening a browser. Just like mobile phones do.

What you need:

  • Windows, macOS or Linux
  • a supported USB PC/SC contactless smart card reader (see below)
  • Java 17+ (Zulu JDK x86 recommended)
  • a NFC tag (Type 2 like NXP NTAG-s or Type 4 like Fidesmo wearable with Fidesmo Links)

Choose your reader

Tested (Windows 10 x64, macOS 13.4 M1, Linux amd64 (Debian Bookworm)):

Running nfc4pc

Use Zulu JDK 17+ (for Intel, also on Apple silicon, due to JavaFX) OR use it headless

java -jar nfc4pc-230307.jar

Recommended to add an alias into your ~/.profile:

echo 'alias nfc="JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home java -jar ~/Downloads/nfc4pc-230321.jar"' >> ~/.profile

Now the command to type on your terminal is simply "nfc".

Usage

By default nfc waits for a tap on any of the connected readers, reads the tag, prints the URL to standard output, and exits. Printing to standard output allows to integrate NFC interactions into other unix scripts.

If you don't tap a tag within 30 seconds, nfc exits with exit code 2. The timeout can be changed with --timeout/-t or disabled with --timeout 0.

NFC is most useful with mobile phones. But if your mobile phone does not have NFC support, you can make the nfc utility print a QR code instead, so that you could scan it with your phone. Just add --qrcode/-q to the command line to always print a QR code with the URL to the terminal.

Generating QR codes in the terminal is so useful, that you can simply use nfc -q http://example.com to print random QR codes with it.

During debugging and development it is sometimes needed not to just read a tag once, but to read multiple taps continuously. Use --continue/-c to read multiple tags without exiting. Tags are read until the program is exited with Ctrl-C.

When run interactively (versus daemon mode), nfc usually prints the read URL to the terminal as a string or QR code. If you just want to launch the URL in a browser, use --browser/-b. Your platforms default browser (URL handler) will be opened with the address from the scanned tag. On unix machines, if $BROWSER is set, it is executed instead of the platform default browser, or you can specify a path to a browser (or, in fact, any executable) as argument to --browser.

Transforming URL-s

Normally, the URL that is read from the tag is the URL that is used (opened in browser or printed as a QR code). Sometimes it is useful to construct a new URL from the tag data. nfc utility supports two type of automatic URL generation:

UID-s

java -jar nfc4pc-230307.jar -uid-url http://example.com/foobar

All tags that don't have a URL payload will open with the UID embedded in a pre-defined URL instead, this case at http://example.com/foobar?uid=<hex>

Meta-URL

java -jar nfc4pc-230307.jar -meta-url http://example.com/foobar

All tags (with or without URL) open at a different URL. A tag with original URL of http://example.org/sample will open as http://example.com/foobar?uid=<hex>&url=https://example.org/sample. URL is optional, tags without URL will open with just the UID parameter (so similar to --uid-url)

Webhooks

java -jar nfc4pc-230307.jar -webhook http://example.com/foobar

All tags will generate a type application/x-www-form-urlencoded POST request to http://example.com/foobar with values uid and url (optional)

Use --authorization <header> to add Authorization header to the webhook POST. Values like Bearer XXXXXXXX and Basic ZGVtbzpwQDU1dzByZA== work (full authorization header)

Desktop integration

nfc --desktop will run nfc4pc as a system tray / system menu item.

Tag emulation (experimental)

You can also turn a supported smart card reader into a tag. Tapping the reader with your phone will open the emulated website address. Right now only ACS ACR 1252U reader with ACS drivers is supported.

Clone this wiki locally