NfcF.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-F (JIS 6319-4) properties and I/O operations on a {@link Tag}.
27 *
28 * <p>Acquire a {@link NfcF} object using {@link #get}.
29 * <p>The primary NFC-F I/O operation is {@link #transceive}. Applications must
30 * implement their own protocol stack on top of {@link #transceive}.
31 */
32public final class NfcF extends BasicTagTechnology {
33    /** @hide */
34    public static final String EXTRA_SC = "systemcode";
35    /** @hide */
36    public static final String EXTRA_PMM = "pmm";
37
38    private byte[] mSystemCode = null;
39    private byte[] mManufacturer = null;
40
41    /**
42     * Get an instance of {@link NfcF} for the given tag.
43     * <p>Returns null if {@link NfcF} was not enumerated in {@link Tag#getTechList}.
44     * This indicates the tag does not support NFC-F.
45     * <p>Does not cause any RF activity and does not block.
46     *
47     * @param tag an NFC-F compatible tag
48     * @return NFC-F object
49     */
50    public static NfcF get(Tag tag) {
51        if (!tag.hasTech(TagTechnology.NFC_F)) return null;
52        try {
53            return new NfcF(tag);
54        } catch (RemoteException e) {
55            return null;
56        }
57    }
58
59    /** @hide */
60    public NfcF(Tag tag) throws RemoteException {
61        super(tag, TagTechnology.NFC_F);
62        Bundle extras = tag.getTechExtras(TagTechnology.NFC_F);
63        if (extras != null) {
64            mSystemCode = extras.getByteArray(EXTRA_SC);
65            mManufacturer = extras.getByteArray(EXTRA_PMM);
66        }
67    }
68
69    /**
70     * Return the System Code bytes from tag discovery.
71     *
72     * <p>Does not cause any RF activity and does not block.
73     *
74     * @return System Code bytes
75     */
76    public byte[] getSystemCode() {
77      return mSystemCode;
78    }
79
80    /**
81     * Return the Manufacturer bytes from tag discovery.
82     *
83     * <p>Does not cause any RF activity and does not block.
84     *
85     * @return Manufacturer bytes
86     */
87    public byte[] getManufacturer() {
88      return mManufacturer;
89    }
90
91    /**
92     * Send raw NFC-F commands to the tag and receive the response.
93     *
94     * <p>Applications must not append the SoD (length) or EoD (CRC) to the payload,
95     * it will be automatically calculated.
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