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-V (ISO 15693) properties and I/O operations on a {@link Tag}.
27  *
28  * <p>Acquire a {@link NfcV} object using {@link #get}.
29  * <p>The primary NFC-V 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 NfcV extends BasicTagTechnology {
36     /** @hide */
37     public static final String EXTRA_RESP_FLAGS = "respflags";
38 
39     /** @hide */
40     public static final String EXTRA_DSFID = "dsfid";
41 
42     private byte mRespFlags;
43     private byte mDsfId;
44 
45     /**
46      * Get an instance of {@link NfcV} for the given tag.
47      * <p>Returns null if {@link NfcV} was not enumerated in {@link Tag#getTechList}.
48      * This indicates the tag does not support NFC-V.
49      * <p>Does not cause any RF activity and does not block.
50      *
51      * @param tag an NFC-V compatible tag
52      * @return NFC-V object
53      */
get(Tag tag)54     public static NfcV get(Tag tag) {
55         if (!tag.hasTech(TagTechnology.NFC_V)) return null;
56         try {
57             return new NfcV(tag);
58         } catch (RemoteException e) {
59             return null;
60         }
61     }
62 
63     /** @hide */
NfcV(Tag tag)64     public NfcV(Tag tag) throws RemoteException {
65         super(tag, TagTechnology.NFC_V);
66         Bundle extras = tag.getTechExtras(TagTechnology.NFC_V);
67         mRespFlags = extras.getByte(EXTRA_RESP_FLAGS);
68         mDsfId = extras.getByte(EXTRA_DSFID);
69     }
70 
71     /**
72      * Return the Response Flag bytes from tag discovery.
73      *
74      * <p>Does not cause any RF activity and does not block.
75      *
76      * @return Response Flag bytes
77      */
getResponseFlags()78     public byte getResponseFlags() {
79         return mRespFlags;
80     }
81 
82     /**
83      * Return the DSF ID bytes from tag discovery.
84      *
85      * <p>Does not cause any RF activity and does not block.
86      *
87      * @return DSF ID bytes
88      */
getDsfId()89     public byte getDsfId() {
90         return mDsfId;
91     }
92 
93     /**
94      * Send raw NFC-V commands to the tag and receive the response.
95      *
96      * <p>Applications must not append the CRC to the payload,
97      * it will be automatically calculated. The application does
98      * provide FLAGS, CMD and PARAMETER bytes.
99      *
100      * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum amount 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     /**
120      * Return the maximum number of bytes that can be sent with {@link #transceive}.
121      * @return the maximum number of bytes that can be sent with {@link #transceive}.
122      */
getMaxTransceiveLength()123     public int getMaxTransceiveLength() {
124         return getMaxTransceiveLengthInternal();
125     }
126 }
127