Near Field Communication in Android 4.4

Written by: on November 18, 2013

NFC stands for Near Field Communication. The field can be roundly measured in centimeters and the technology facilitates such things as bumping two phones together to initiate a multiplayer game or paying for coffee by tapping a phone on an NFC reader.

Prior to Android 4.4, NFC payment transactions had to go through a carrier’s proprietary Secure Element (SE, it can be in a SIM Card for example) on the device using locally stored information related to the device owner’s method of payment. This gave carriers grounds to prevent the availability of apps that make payments through NFC. Since the transaction relied on carrier-owned hardware, the SE, the question arose as to whether a carrier, like Sprint or Verizon, should be required to allow apps to access that hardware.

Host-Based Card Emulation

Host-based Card Emulation (HCE), new to Android in 4.4, allows apps to bypass the Secure Element and emulate an NFC card using cloud payment information or otherwise stored payment information. With HCE, any app can emulate an NFC card and any Android device can act as an NFC Reader.

Check for HCE by looking for FEATURE_NFC_HOST_CARD_EMULATION.

Tell Android your application to use NFC and HCE in AndroidManifest.xml.

HCE needs to be implemented through a Service. Android Services can run in the background without the user interacting with the application that started the Service. The Service allows the NFC transaction to take place without the user having to open an app first. Android 4.4 adds HostApduService. Extend it to create the service that will handle Near Field Communications.

Our NfcHceService needs to override 2 methods; processCommandApdu() and onDeactivated(). A command Application Protocol Data Unit (APDU) is what the NFC Reader sends to NfcHceService. processCommandApdu() sends back a response APDU. onDeactivated() is called when the NFC Reader communicates with another HCE Service on the device or when the device is no longer in the NFC Reader’s field.

Declare the service in AndroidManifest.xml.

The hceService.xml resource, declared in the meta-data tag, tells Android where to find our Application ID group.

Application IDs

An Application ID (AID) allows an NFC Reader to tell a device which emulated card it wants to read (which processCommandApdu() it is expecting a response from). AIDs are defined by ISO/IEC 7816-5 (International Organization for Standardization and the International Electrotechnical Commission). For an Android device to act as a reader, it must have a registered AID.

Our hceService.xml defines an application’s AID Group which, in this case, is a payment group (CardEmulation.CATEGORY_PAYMENT instead of an other group CATEGORY_OTHER). Applications with AID Groups of the payment category require a 260×96 dp asset for an apduServiceBanner.

HCE simplifies what was complex and closed, opening NFC to developers and freeing it from carriers, other potential stakeholders, devices, and limitations.

More about Host-based Card Emulation is available here.

This piece is the sixth of eight in our KitKat Developer’s Guide. Check back later this week for new updates or follow us on twitter.

Lex Hubbard

Lex Hubbard

Lex Hubbard is a Mobile Software Engineer at Double Encore, specializing in Android Development.
Article


Add your voice to the discussion: