Courtesy: GitHub

Android CardEmulation Sample

This sample demonstrates how to emulate an NFC card, using the "host card emulation" feature added in Android 4.4. This sample makes the device appear as a loyalty card whenever the screen is on and the user taps their device on an appropriately configured NFC reader.

The "CardReader" sample can be used to read the loyalty card implemented in this sample.


  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.


If you've found an error in this sample, please file an issue:

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see for more details.


Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile ""

compile ""

compile ""

compile ""

compileSdkVersion 26

minSdkVersion 19

targetSdkVersion 26


versionCode 1

versionName 1.0


versionCode 1

versionName 1.0


  • android.permission.NFC



A simple launcher activity containing a summary sample description, sample log and a custom {@link} which can display a view.

For devices with displays with a width of 720dp or greater, the sample log is always visible, on other devices it's visibility is controlled by an item on the Action Bar.

Create a chain of targets that will receive log data



This is a sample APDU Service which demonstrates how to interface with the card emulation support added in Android 4.4, KitKat. *

This sample replies to any requests sent with the string "Hello World". In real-world situations, you would need to modify this code to implement your desired communication protocol. *

This sample will be invoked for any terminals selecting AIDs of 0xF11111111, 0xF22222222, or 0xF33333333. See src/main/res/xml/aid_list.xml for more details. *

Note: This is a low-level interface. Unlike the NdefMessage many developers are familiar with for implementing Android Beam in apps, card emulation only provides a byte-array based communication channel. It is left to developers to implement higher level protocol support as needed.

Called if the connection to the NFC card is lost, in order to let the application know the cause for the disconnection (either a lost link, or another AID being selected by the reader). * @param reason Either DEACTIVATION_LINK_LOSS or DEACTIVATION_DESELECTED

This method will be called when a command APDU has been received from a remote device. A response APDU can be provided directly by returning a byte-array in this method. In general response APDUs must be sent as quickly as possible, given the fact that the user is likely holding his device over an NFC reader when this method is called. *

If there are multiple services that have registered for the same AIDs in their meta-data entry, you will only get called if the user has explicitly selected your service, either as a default or just for the next tap. *

This method is running on the main thread of your application. If you cannot return a response APDU immediately, return null and use the {@link #sendResponseApdu(byte[])} method later. * @param commandApdu The APDU that received from the remote device @param extras A bundle containing extra data. May be null. @return a byte-array containing the response APDU, or null if no response APDU can be sent at this point.

Build APDU for SELECT AID command. This command indicates which service a reader is interested in communicating with. See ISO 7816-4. * @param aid Application ID (AID) to select @return APDU for SELECT AID command

Utility method to convert a byte array to a hexadecimal string. * @param bytes Bytes to convert @return String, containing hexadecimal representation.

Utility method to convert a hexadecimal string to a byte string. *

Behavior with input strings containing non-hexadecimal characters is undefined. * @param s String containing hexadecimal characters to convert @return Byte array generated from input @throws java.lang.IllegalArgumentException if input length is incorrect

Utility method to concatenate two byte arrays. @param first First array @param rest Any remaining arrays @return Concatenated copy of input arrays