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 17 package android.nfc.tech; 18 19 import android.nfc.Tag; 20 import android.os.Bundle; 21 import android.os.RemoteException; 22 23 import 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 * 32 * <p class="note"><strong>Note:</strong> Methods that perform I/O operations 33 * require the {@link android.Manifest.permission#NFC} permission. 34 */ 35 public final class NfcB extends BasicTagTechnology { 36 /** @hide */ 37 public static final String EXTRA_APPDATA = "appdata"; 38 /** @hide */ 39 public static final String EXTRA_PROTINFO = "protinfo"; 40 41 private byte[] mAppData; 42 private byte[] mProtInfo; 43 44 /** 45 * Get an instance of {@link NfcB} for the given tag. 46 * <p>Returns null if {@link NfcB} was not enumerated in {@link Tag#getTechList}. 47 * This indicates the tag does not support NFC-B. 48 * <p>Does not cause any RF activity and does not block. 49 * 50 * @param tag an NFC-B compatible tag 51 * @return NFC-B object 52 */ get(Tag tag)53 public static NfcB get(Tag tag) { 54 if (!tag.hasTech(TagTechnology.NFC_B)) return null; 55 try { 56 return new NfcB(tag); 57 } catch (RemoteException e) { 58 return null; 59 } 60 } 61 62 /** @hide */ NfcB(Tag tag)63 public NfcB(Tag tag) throws RemoteException { 64 super(tag, TagTechnology.NFC_B); 65 Bundle extras = tag.getTechExtras(TagTechnology.NFC_B); 66 mAppData = extras.getByteArray(EXTRA_APPDATA); 67 mProtInfo = extras.getByteArray(EXTRA_PROTINFO); 68 } 69 70 /** 71 * Return the Application Data bytes from ATQB/SENSB_RES at tag discovery. 72 * 73 * <p>Does not cause any RF activity and does not block. 74 * 75 * @return Application Data bytes from ATQB/SENSB_RES bytes 76 */ getApplicationData()77 public byte[] getApplicationData() { 78 return mAppData; 79 } 80 81 /** 82 * Return the Protocol Info bytes from ATQB/SENSB_RES at tag discovery. 83 * 84 * <p>Does not cause any RF activity and does not block. 85 * 86 * @return Protocol Info bytes from ATQB/SENSB_RES bytes 87 */ getProtocolInfo()88 public byte[] getProtocolInfo() { 89 return mProtInfo; 90 } 91 92 /** 93 * Send raw NFC-B 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 not send commands that manage the polling 98 * loop and initialization (SENSB_REQ, SLOT_MARKER etc). 99 * 100 * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes 101 * that can be sent with {@link #transceive}. 102 * 103 * <p>This is an I/O operation and will block until complete. It must 104 * not be called from the main application thread. A blocked call will be canceled with 105 * {@link IOException} if {@link #close} is called from another thread. 106 * 107 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 108 * 109 * @param data bytes to send 110 * @return bytes received in response 111 * @throws TagLostException if the tag leaves the field 112 * @throws IOException if there is an I/O failure, or this operation is canceled 113 */ transceive(byte[] data)114 public byte[] transceive(byte[] data) throws IOException { 115 return transceive(data, true); 116 } 117 118 /** 119 * Return the maximum number of bytes that can be sent with {@link #transceive}. 120 * @return the maximum number of bytes that can be sent with {@link #transceive}. 121 */ getMaxTransceiveLength()122 public int getMaxTransceiveLength() { 123 return getMaxTransceiveLengthInternal(); 124 } 125 } 126