Skip to content

Conversation

@champignoom
Copy link

@champignoom champignoom commented Aug 11, 2025

Add all APIs (78 in total) listed in the official doc for all NFC technologies (NfcA, NfcB, NfcF, NfcV, IsoDep, Ndef, MifareClassic, MifareUltralight, NfcBarcode, NdefFormatable).

See also termux/termux-api#796.

The API call request in JSON format is passed into stdin of termux-api in the format of:

  • either {"op": "discoverTag"}
  • or {"op": "api", "class": "<CLASSNAME>", "method": "<METHODNAME>", "args": [<ARG1>, <ARG2>, ...]}

This is a thin wrapper around the native APIs without UI ergonomic considerations. It's supposed to be used e.g. in bash as a coprocess. To directly use it as a REPL, use rlwrap termux-nfc -.

Examples

Normal usage

$ termux-nfc -
{"op": "discoverTag"}
{"status":"success","result":{"id":{"format":"hex","value":"DEADBEEF"},"techList": "android.nfc.tech.IsoDep","android.nfc.tech.NfcA","android.nfc.tech.MifareClassic"]}}
{"op": "api", "class":"IsoDep", "method": "connect", "args": []}
{"status": "success"}
{"op": "api", "class":"IsoDep", "method": "transceive", "args": [{"format": "hex", "value": "00A40000023F00"}]}
{"status": "success", "result": {"format": "hex", "value": "01020304DEADBEEF"}}}
{"op": "api", "class":"TagTechnology", "method": "close", "args": []}
{"status": "success"}
{"op": "api", "class":"MifareClassic", "method": "connect", "args": []}
{"status": "success"}
{"op": "api", "class":"MifareClassic", "method": "authenticateSectorWithKeyA", "args": [0, {"format":"hex","value":"FFFFFFFFFFFFFFFF"}]}
{"status": "success", "result": true}
{"op": "api", "class":"MifareClassic", "method": "readBlock", "args": [0]}
{"status": "success", "result": {"format": "hex", "value": {"format": "hex", "value": "01020304DEADBEEF"}}}
{"op": "api", "class":"TagTechnology", "method": "close", "args": []}
{"status": "success"}

Lines with "op" are your console inputs, while lines with "status" are output from termux-api.

As REPL

$ rlwrap termux-nfc -
... (same as normal usage)

With rlwrap, you can use advanced command-line editing keybinds like up/down.

One-liner

With the help of bash herestring, commands can be typed before they are invoked:

$ termux-nfc - <<<'{"op": "discoverTag"}'$'\n''{"op": "api", "class":"IsoDep", "method": "connect", "args": []}'$'\n''{"op": "api", "class":"IsoDep", "method": "transceive", "args": [{"format": "hex", "value": "00A40000023F00"}]}'$'\n'
{"status":"success","result":{"id":{"format":"hex","value":"DEADBEEF"},"techList": "android.nfc.tech.IsoDep","android.nfc.tech.NfcA","android.nfc.tech.MifareClassic"]}}
{"status": "success"}
{"status": "success", "result": {"format": "hex", "value": "01020304DEADBEEF"}}}

As bash coprocess

coproc NFC_REPL { termux-nfc - ; }

function run-nfc {
	local msg="$1"
	echo "$msg" >&"${NFC_REPL[1]}"
	local response
	read -r response <&"${NFC_REPL[0]}"
	echo "$response"
}

run-nfc '{"op": "discoverTag"}'
run-nfc '{"op": "api", "class":"IsoDep", "method": "connect", "args": []}'
run-nfc '{"op": "api", "class":"IsoDep", "method": "transceive", "args": [{"format": "hex", "value": "00A40000023F00"}]}'
run-nfc '{"op": "api", "class":"TagTechnology", "method": "close", "args": []}'

@champignoom champignoom marked this pull request as draft August 14, 2025 08:34
@champignoom champignoom marked this pull request as ready for review August 14, 2025 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant