NfcA.java revision 74fe6c6b245ebe7d3b3d96962c32980d88dca4f5
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 */ 32public final class NfcA extends BasicTagTechnology { 33 /** @hide */ 34 public static final String EXTRA_SAK = "sak"; 35 /** @hide */ 36 public static final String EXTRA_ATQA = "atqa"; 37 38 private short mSak; 39 private byte[] mAtqa; 40 41 /** 42 * Get an instance of {@link NfcA} for the given tag. 43 * <p>Returns null if {@link NfcA} was not enumerated in {@link Tag#getTechList}. 44 * This indicates the tag does not support NFC-A. 45 * <p>Does not cause any RF activity and does not block. 46 * 47 * @param tag an NFC-A compatible tag 48 * @return NFC-A object 49 */ 50 public static NfcA get(Tag tag) { 51 if (!tag.hasTech(TagTechnology.NFC_A)) return null; 52 try { 53 return new NfcA(tag); 54 } catch (RemoteException e) { 55 return null; 56 } 57 } 58 59 /** @hide */ 60 public NfcA(Tag tag) throws RemoteException { 61 super(tag, TagTechnology.NFC_A); 62 Bundle extras = tag.getTechExtras(TagTechnology.NFC_A); 63 mSak = extras.getShort(EXTRA_SAK); 64 mAtqa = extras.getByteArray(EXTRA_ATQA); 65 } 66 67 /** 68 * Return the ATQA/SENS_RES bytes from tag discovery. 69 * 70 * <p>Does not cause any RF activity and does not block. 71 * 72 * @return ATQA/SENS_RES bytes 73 */ 74 public byte[] getAtqa() { 75 return mAtqa; 76 } 77 78 /** 79 * Return the SAK/SEL_RES bytes from tag discovery. 80 * 81 * <p>Does not cause any RF activity and does not block. 82 * 83 * @return SAK bytes 84 */ 85 public short getSak() { 86 return mSak; 87 } 88 89 /** 90 * Send raw NFC-A commands to the tag and receive the response. 91 * 92 * <p>Applications must not append the EoD (CRC) to the payload, 93 * it will be automatically calculated. 94 * <p>Applications must only send commands that are complete bytes, 95 * for example a SENS_REQ is not possible (these are used to 96 * manage tag polling and initialization). 97 * 98 * <p>This is an I/O operation and will block until complete. It must 99 * not be called from the main application thread. A blocked call will be canceled with 100 * {@link IOException} if {@link #close} is called from another thread. 101 * 102 * @param data bytes to send 103 * @return bytes received in response 104 * @throws TagLostException if the tag leaves the field 105 * @throws IOException if there is an I/O failure, or this operation is canceled 106 */ 107 public byte[] transceive(byte[] data) throws IOException { 108 return transceive(data, true); 109 } 110} 111