NfcB.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-B (ISO 14443-3B) properties and I/O operations on a {@link Tag}. 27 * 28 * <p>Acquire a {@link NfcB} object using {@link #get}. 29 * <p>The primary NFC-B I/O operation is {@link #transceive}. Applications must 30 * implement their own protocol stack on top of {@link #transceive}. 31 */ 32public final class NfcB extends BasicTagTechnology { 33 /** @hide */ 34 public static final String EXTRA_APPDATA = "appdata"; 35 /** @hide */ 36 public static final String EXTRA_PROTINFO = "protinfo"; 37 38 private byte[] mAppData; 39 private byte[] mProtInfo; 40 41 /** 42 * Get an instance of {@link NfcB} for the given tag. 43 * <p>Returns null if {@link NfcB} was not enumerated in {@link Tag#getTechList}. 44 * This indicates the tag does not support NFC-B. 45 * <p>Does not cause any RF activity and does not block. 46 * 47 * @param tag an NFC-B compatible tag 48 * @return NFC-B object 49 */ 50 public static NfcB get(Tag tag) { 51 if (!tag.hasTech(TagTechnology.NFC_B)) return null; 52 try { 53 return new NfcB(tag); 54 } catch (RemoteException e) { 55 return null; 56 } 57 } 58 59 /** @hide */ 60 public NfcB(Tag tag) throws RemoteException { 61 super(tag, TagTechnology.NFC_B); 62 Bundle extras = tag.getTechExtras(TagTechnology.NFC_B); 63 mAppData = extras.getByteArray(EXTRA_APPDATA); 64 mProtInfo = extras.getByteArray(EXTRA_PROTINFO); 65 } 66 67 /** 68 * Return the Application Data bytes from ATQB/SENSB_RES at tag discovery. 69 * 70 * <p>Does not cause any RF activity and does not block. 71 * 72 * @return Application Data bytes from ATQB/SENSB_RES bytes 73 */ 74 public byte[] getApplicationData() { 75 return mAppData; 76 } 77 78 /** 79 * Return the Protocol Info bytes from ATQB/SENSB_RES at tag discovery. 80 * 81 * <p>Does not cause any RF activity and does not block. 82 * 83 * @return Protocol Info bytes from ATQB/SENSB_RES bytes 84 */ 85 public byte[] getProtocolInfo() { 86 return mProtInfo; 87 } 88 89 /** 90 * Send raw NFC-B 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 not send commands that manage the polling 95 * loop and initialization (SENSB_REQ, SLOT_MARKER etc). 96 * 97 * <p>This is an I/O operation and will block until complete. It must 98 * not be called from the main application thread. A blocked call will be canceled with 99 * {@link IOException} if {@link #close} is called from another thread. 100 * 101 * @param data bytes to send 102 * @return bytes received in response 103 * @throws TagLostException if the tag leaves the field 104 * @throws IOException if there is an I/O failure, or this operation is canceled 105 */ 106 public byte[] transceive(byte[] data) throws IOException { 107 return transceive(data, true); 108 } 109} 110