NfcA.java revision 39cf3a445e507f219ecc8a476f6038f095d9d520
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.nfc.tech; 18 19import android.nfc.Tag; 20import android.os.Bundle; 21import android.os.RemoteException; 22 23import java.io.IOException; 24 25/** 26 * Provides access to NFC-A (ISO 14443-3A) properties and I/O operations on a {@link Tag}. 27 * 28 * <p>Acquire a {@link NfcA} object using {@link #get}. 29 * <p>The primary NFC-A I/O operation is {@link #transceive}. Applications must 30 * implement their own protocol stack on top of {@link #transceive}. 31 * 32 * <p class="note"><strong>Note:</strong> Methods that perform I/O operations 33 * require the {@link android.Manifest.permission#NFC} permission. 34 */ 35public final class NfcA extends BasicTagTechnology { 36 /** @hide */ 37 public static final String EXTRA_SAK = "sak"; 38 /** @hide */ 39 public static final String EXTRA_ATQA = "atqa"; 40 41 private short mSak; 42 private byte[] mAtqa; 43 44 /** 45 * Get an instance of {@link NfcA} for the given tag. 46 * <p>Returns null if {@link NfcA} was not enumerated in {@link Tag#getTechList}. 47 * This indicates the tag does not support NFC-A. 48 * <p>Does not cause any RF activity and does not block. 49 * 50 * @param tag an NFC-A compatible tag 51 * @return NFC-A object 52 */ 53 public static NfcA get(Tag tag) { 54 if (!tag.hasTech(TagTechnology.NFC_A)) return null; 55 try { 56 return new NfcA(tag); 57 } catch (RemoteException e) { 58 return null; 59 } 60 } 61 62 /** @hide */ 63 public NfcA(Tag tag) throws RemoteException { 64 super(tag, TagTechnology.NFC_A); 65 Bundle extras = tag.getTechExtras(TagTechnology.NFC_A); 66 mSak = extras.getShort(EXTRA_SAK); 67 mAtqa = extras.getByteArray(EXTRA_ATQA); 68 } 69 70 /** 71 * Return the ATQA/SENS_RES bytes from tag discovery. 72 * 73 * <p>Does not cause any RF activity and does not block. 74 * 75 * @return ATQA/SENS_RES bytes 76 */ 77 public byte[] getAtqa() { 78 return mAtqa; 79 } 80 81 /** 82 * Return the SAK/SEL_RES bytes from tag discovery. 83 * 84 * <p>Does not cause any RF activity and does not block. 85 * 86 * @return SAK bytes 87 */ 88 public short getSak() { 89 return mSak; 90 } 91 92 /** 93 * Send raw NFC-A commands to the tag and receive the response. 94 * 95 * <p>Applications must not append the EoD (CRC) to the payload, 96 * it will be automatically calculated. 97 * <p>Applications must only send commands that are complete bytes, 98 * for example a SENS_REQ is not possible (these are used to 99 * manage tag polling and initialization). 100 * 101 * <p>This is an I/O operation and will block until complete. It must 102 * not be called from the main application thread. A blocked call will be canceled with 103 * {@link IOException} if {@link #close} is called from another thread. 104 * 105 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 106 * 107 * @param data bytes to send 108 * @return bytes received in response 109 * @throws TagLostException if the tag leaves the field 110 * @throws IOException if there is an I/O failure, or this operation is canceled 111 */ 112 public byte[] transceive(byte[] data) throws IOException { 113 return transceive(data, true); 114 } 115} 116