Git Product home page Git Product logo

nfchcendefemulator's Introduction

Nfc HCE Ndef Emulator and Reader

This app emulates an NFC Forum Type 4 Tag on an Android device using Host-based Card Emulation ("HCE").

To be compliant with the specification a service is running independent on the app opened that serves an Application Identifier ("AID") with the value D2760000850101.

The app is running up to Android 13 (SDK 33, the actual one when app was created) and is tested on real Android devices with Android 9 and Android 13.

There are 3 main functionalities:

  1. Send a message using HCE to another device in NDEF format
  2. read a message using the Android's NFC reader and reading an incoming intent with NDEF data using NDEF protocol (high level API)
  3. read a message using the Android's NFC reader and run the complete reading sequence using IsoDep protocol (low level API)

As there are very less articles and source code for NDEF emulation via HCE available on the Internet I was very happy to find one repository on GitHub containing a running service class, so all credits go to the original author.
I just made the class a little more "handy" to accept individual messages.

The code is from the author "TechBooster" and is available in the repository "C85-Android-4.4-Sample"; the code for the "NdefHostApduService.java" class is available here:

https://github.com/TechBooster/C85-Android-4.4-Sample/blob/master/chapter08/NdefCard/src/com/example/ndefcard/NdefHostApduService.java

As the source code of the service class is under Apache License 2.0 my app got the same license.

Note: please keep in mind that the app is active since installation as it runs a HostApduService. If there are other apps on your phone providing data for NFC - AID "D2760000850101" (NDEF access) you can run into problems ("routing conflict") that needs to get resolved each time a card reader is tapped to the device and asking for an NDEF message. On your device is under the "Settings menu" an entry "Connections" - "NFC" - "Payments" (or similar) and a sub-tab "others" - there you can choose which services are active at one time.

A second note regarding the length of an emulated NDEF message: the maximum length of an NDEF message is 0x7FFF == (decimal) 32767 bytes, but I'm limiting the length to a much smaller value (see the CAPABILITY_CONTAINER_FILE):

    private final static byte[] CAPABILITY_CONTAINER_FILE = new byte[] {
            0x00, 0x0f, // CCLEN
            0x20, // Mapping Version
            0x00, 0x3b, // Maximum R-APDU data size
            0x00, 0x34, // Maximum C-APDU data size
            0x04, 0x06, // Tag & Length
            (byte)0xe1, 0x04, // NDEF File Identifier
            (byte) 0x00, (byte) 0xff, // Maximum NDEF size, do NOT extend this value
            0x00, // NDEF file read access granted
            (byte)0xff, // NDEF File write access denied
    };

You find more detailed information in https://stackoverflow.com/a/75933443/8166854 and more information here: https://stackoverflow.com/a/75664265/8166854:

In the Type 4 Tag documentation you linked in the question at the bottom of Section 5.1.2.1 it says

The maximum size of the NDEF file is limited by the Offset and Length Le fields of ReadBinary and NDEF 
Update C-APDUs (see Table 16, Table 17, Table 22, and Table 23). The maximum size of the NDEF file is 
reduced to 7FFFh + FFh = 80FEh bytes.

So Android have probably made a bug fix to match the max NDEF file size defined in the spec.

Link to the documentation: http://apps4android.org/nfc-specifications/NFCForum-TS-Type-4-Tag_2.0.pdf 

nfchcendefemulator's People

Contributors

fluttercrypto avatar michaelsplayground avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nfchcendefemulator's Issues

nfc closed

When running the app, the system's NFC functionality automatically turns off, and it cannot be manually enabled.
os system: android 8.1.0

2023-10-08 15:32:08.240 1691-3134 zygote com.android.nfc A runtime.cc:524] Runtime aborting...
2023-10-08 15:32:08.241 1691-3134 zygote com.android.nfc A runtime.cc:524] Dumping all threads without appropriate locks held: thread list lock mutator lock
2023-10-08 15:32:08.241 1691-3134 zygote com.android.nfc A runtime.cc:524] All threads:
2023-10-08 15:32:08.241 1691-3134 zygote com.android.nfc A runtime.cc:524] DALVIK THREADS (17):
2023-10-08 15:32:08.241 1691-3134 zygote com.android.nfc A runtime.cc:524] "applyRouting" prio=5 tid=13 Runnable
2023-10-08 15:32:08.241 1691-3134 zygote com.android.nfc A runtime.cc:524] | group="" sCount=0 dsCount=0 flags=0 obj=0x12c4af68 self=0xa0af5800
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] | sysTid=3134 nice=0 cgrp=default sched=0/0 handle=0x8f51c970
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] | state=R schedstat=( 27245192 270116 6 ) utm=1 stm=1 core=3 HZ=100
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] | stack=0x8f41a000-0x8f41c000 stackSize=1038KB
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] | held mutexes= "abort lock" "mutator lock"(shared held)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #00 pc 002ecd87 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits>&, int, BacktraceMap*, char const*, art::ArtMethod*, void*)+130)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #1 pc 00380075 /system/lib/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits>&, bool, BacktraceMap*, bool) const+204)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #2 pc 0037c5af /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits>&, bool, BacktraceMap*, bool) const+34)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #03 pc 00394541 /system/lib/libart.so (art::DumpCheckpoint::Run(art::Thread*)+684)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #04 pc 0038e05f /system/lib/libart.so (art::ThreadList::RunCheckpoint(art::Closure*, art::Closure*)+322)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #05 pc 0038db4f /system/lib/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits>&, bool)+542)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #06 pc 0036cf17 /system/lib/libart.so (art::AbortState::Dump(std::__1::basic_ostream<char, std::__1::char_traits>&) const+210)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #07 pc 00361877 /system/lib/libart.so (art::Runtime::Abort(char const*)+142)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #08 pc 0042225d /system/lib/libart.so (android::base::LogMessage::~LogMessage()+452)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #09 pc 00281061 /system/lib/libart.so (art::JNI::FatalError(_JNIEnv*, char const*)+120)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #10 pc 000145d7 /system/lib/libnfc_nci_jni.so (???)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] native: #11 pc 0000077d /data/dalvik-cache/arm/system@app@NfcNci@[email protected] (Java_com_android_nfc_dhimpl_NativeNfcManager_doAbort__Ljava_lang_String_2+92)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] at com.android.nfc.dhimpl.NativeNfcManager.doAbort(Native method)
2023-10-08 15:32:08.243 1691-3134 zygote com.android.nfc A runtime.cc:524] at com.android.nfc.NfcService$WatchDogThread.run(NfcService.java:2191)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.