NfcF.java revision faca12adc62d148505fadfd286e6a2752c197fa0
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.ErrorCodes;
20import android.nfc.Tag;
21import android.os.Bundle;
22import android.os.RemoteException;
23import android.util.Log;
24
25import java.io.IOException;
26
27/**
28 * Provides access to NFC-F (JIS 6319-4) properties and I/O operations on a {@link Tag}.
29 *
30 * <p>Acquire a {@link NfcF} object using {@link #get}.
31 * <p>The primary NFC-F I/O operation is {@link #transceive}. Applications must
32 * implement their own protocol stack on top of {@link #transceive}.
33 *
34 * <p class="note"><strong>Note:</strong> Methods that perform I/O operations
35 * require the {@link android.Manifest.permission#NFC} permission.
36 */
37public final class NfcF extends BasicTagTechnology {
38    private static final String TAG = "NFC";
39
40    /** @hide */
41    public static final String EXTRA_SC = "systemcode";
42    /** @hide */
43    public static final String EXTRA_PMM = "pmm";
44
45    private byte[] mSystemCode = null;
46    private byte[] mManufacturer = null;
47
48    /**
49     * Get an instance of {@link NfcF} for the given tag.
50     * <p>Returns null if {@link NfcF} was not enumerated in {@link Tag#getTechList}.
51     * This indicates the tag does not support NFC-F.
52     * <p>Does not cause any RF activity and does not block.
53     *
54     * @param tag an NFC-F compatible tag
55     * @return NFC-F object
56     */
57    public static NfcF get(Tag tag) {
58        if (!tag.hasTech(TagTechnology.NFC_F)) return null;
59        try {
60            return new NfcF(tag);
61        } catch (RemoteException e) {
62            return null;
63        }
64    }
65
66    /** @hide */
67    public NfcF(Tag tag) throws RemoteException {
68        super(tag, TagTechnology.NFC_F);
69        Bundle extras = tag.getTechExtras(TagTechnology.NFC_F);
70        if (extras != null) {
71            mSystemCode = extras.getByteArray(EXTRA_SC);
72            mManufacturer = extras.getByteArray(EXTRA_PMM);
73        }
74    }
75
76    /**
77     * Return the System Code bytes from tag discovery.
78     *
79     * <p>Does not cause any RF activity and does not block.
80     *
81     * @return System Code bytes
82     */
83    public byte[] getSystemCode() {
84      return mSystemCode;
85    }
86
87    /**
88     * Return the Manufacturer bytes from tag discovery.
89     *
90     * <p>Does not cause any RF activity and does not block.
91     *
92     * @return Manufacturer bytes
93     */
94    public byte[] getManufacturer() {
95      return mManufacturer;
96    }
97
98    /**
99     * Send raw NFC-F commands to the tag and receive the response.
100     *
101     * <p>Applications must not append the SoD (length) or EoD (CRC) to the payload,
102     * it will be automatically calculated.
103     *
104     * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum amount of bytes
105     * that can be sent with {@link #transceive}.
106     *
107     * <p>This is an I/O operation and will block until complete. It must
108     * not be called from the main application thread. A blocked call will be canceled with
109     * {@link IOException} if {@link #close} is called from another thread.
110     *
111     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
112     *
113     * @param data bytes to send
114     * @return bytes received in response
115     * @throws TagLostException if the tag leaves the field
116     * @throws IOException if there is an I/O failure, or this operation is canceled
117     */
118    public byte[] transceive(byte[] data) throws IOException {
119        return transceive(data, true);
120    }
121
122    /**
123     * Return the maximum number of bytes that can be sent with {@link #transceive}.
124     * @return the maximum number of bytes that can be sent with {@link #transceive}.
125     */
126    public int getMaxTransceiveLength() {
127        return getMaxTransceiveLengthInternal();
128    }
129
130    /**
131     * Set the timeout of {@link #transceive} in milliseconds.
132     * <p>The timeout only applies to NfcF {@link #transceive}, and is
133     * reset to a default value when {@link #close} is called.
134     * <p>Setting a longer timeout may be useful when performing
135     * transactions that require a long processing time on the tag
136     * such as key generation.
137     *
138     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
139     *
140     * @param timeout timeout value in milliseconds
141     * @hide
142     */
143    // TODO Unhide for ICS
144    public void setTimeout(int timeout) {
145        try {
146            int err = mTag.getTagService().setTimeout(TagTechnology.NFC_F, timeout);
147            if (err != ErrorCodes.SUCCESS) {
148                throw new IllegalArgumentException("The supplied timeout is not valid");
149            }
150        } catch (RemoteException e) {
151            Log.e(TAG, "NFC service dead", e);
152        }
153    }
154
155    /**
156     * Gets the currently set timeout of {@link #transceive} in milliseconds.
157     *
158     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
159     *
160     * @return timeout value in milliseconds
161     * @hide
162     */
163    // TODO Unhide for ICS
164    public int getTimeout() {
165        try {
166            return mTag.getTagService().getTimeout(TagTechnology.NFC_F);
167        } catch (RemoteException e) {
168            Log.e(TAG, "NFC service dead", e);
169            return 0;
170        }
171    }
172}
173