Skip to content

Android reference device

An Android phone as a contactless reader, ready to go through an EMV Level 2 lab: the agent in an Android application, reading cards with the phone’s NFC. It shows what a device maker’s own application needs around a7pay. It is EMV-ready, not PCI-ready: the cryptography is software (no secrets are at stake in Level 2’s), there is no PIN entry, and nothing is hardened.

It lives in apps/ref/android:

Path What it is
agent-android/ The Rust library the application loads: the agent on TCP, the phone’s reader and cryptography (a crate of the a7pay workspace)
app/ The Android application (Kotlin): the screen, NFC’s reader mode

You need the Android SDK and NDK, a JDK 17 or later, and Rust through rustup, with:

rustup target add aarch64-linux-android x86_64-linux-android
cargo install cargo-ndk

and ANDROID_HOME (the SDK), ANDROID_NDK_HOME (the NDK) set. Then, from apps/ref/android:

./gradlew assembleDebug            # app/build/outputs/apk/debug/app-debug.apk
./gradlew installDebug             # onto the phone plugged in (USB debugging on)

Each build first builds the Rust library for phones (arm64) and the emulator (x86_64), and generates its Kotlin bindings. Gradle finds cargo on the PATH or where rustup puts it; -Pa7pay.cargo=/path/to/cargo says where otherwise (Android Studio started from the desktop doesn’t get the shell’s PATH). Android Studio opens apps/ref/android as it is.

On the phone, the agent carries every kernel, their optional features and the three schemes’ behaviours: what a device certified for all of them carries.

Start the application and keep it in front: the phone reads cards only then, and its screen stays on. It shows the address to connect to (the phone’s on its Wi-Fi, port 9100), the controller’s connection, and the messages for the cardholder (“Present Card”, “Approved”).

Connect a controller to it: agentctl --agent <address> ... (see Level 2 test agent), or a manual test tool. Over USB instead of Wi-Fi:

adb forward tcp:9100 tcp:9100      # then the controller connects to 127.0.0.1:9100

It has been tried with a real card: a Pixel 4a (Android 13), a Mastercard test card read to its Online Request through a manual test tool.

 controller ── TCP ──▶ agent (Rust, its own thread)
                         │ Reader: CardReader ──▶ Kotlin: IsoDep (NFC reader mode)
                         │ Crypto: SwCrypto  ──▶ Android's random numbers, clock
                         └ AgentListener     ──▶ Kotlin: the screen
  • The Rust library is the agent served on TCP (agent::tcp, shared with the desktop agent), with platform::swcrypto for the cryptography on Android’s random numbers and clock.
  • The application gives it the phone’s reader through an interface it implements (CardReader: is a card there, send a command, show a message, let the card go), and is told the agent’s state (AgentListener). UniFFI generates the Kotlin side of both from the Rust code; the calls go through JNA, so there is no hand-written JNI.
  • The phone’s NFC stack does the Level 1 work (polling, anti-collision, ISO 14443-4), as it does for any payment application on Android: the kernels see a card once Android has connected it.
  • A PIN pad (Online PIN), even a non-secure one.
  • Letting the card go powers the field off on a dedicated reader; Android doesn’t let an application do that, so the card has to leave the field and come back.
  • The agent stops when Android stops the application; it isn’t a foreground service.