Skip to content
Martin Paljak edited this page Jul 7, 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.

PC-s don't have such software built in, but 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:

Running nfc4pc

Using from command line or in headless mode should work with any Java (17+). For desktop integration Java with JavaFX (like Zulu JDK) is required.

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.

Desktop integration

nfc --desktop will run nfc4pc as a system tray / system menu item. NB! Java with JavaFX is required!

Developers and integrators: 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:

Meta-URL

nfc --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)

UID URL triggers

nfc -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>. This is handy for making use of existing or locked NFC devices for automation purposes.

Webhooks and headless operation

nfc --headless --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)

Experimental features

Tag emulation

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 PC/SC driver installed is supported. This will NOT work with Windows CCID driver or open source CCID driver present on Linux and macOS.

nfc --emulate https://example.com -qr # print the QR code of the emulated URL and emulate a Mifare Ultralight tag with the URL
Clone this wiki locally