NfcF.java revision 39cf3a445e507f219ecc8a476f6038f095d9d520
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 *
32 * <p class="note"><strong>Note:</strong> Methods that perform I/O operations
33 * require the {@link android.Manifest.permission#NFC} permission.
34 */
35public final class NfcF extends BasicTagTechnology {
36    /** @hide */
37    public static final String EXTRA_SC = "systemcode";
38    /** @hide */
39    public static final String EXTRA_PMM = "pmm";
40
41    private byte[] mSystemCode = null;
42    private byte[] mManufacturer = null;
43
44    /**
45     * Get an instance of {@link NfcF} for the given tag.
46     * <p>Returns null if {@link NfcF} was not enumerated in {@link Tag#getTechList}.
47     * This indicates the tag does not support NFC-F.
48     * <p>Does not cause any RF activity and does not block.
49     *
50     * @param tag an NFC-F compatible tag
51     * @return NFC-F object
52     */
53    public static NfcF get(Tag tag) {
54        if (!tag.hasTech(TagTechnology.NFC_F)) return null;
55        try {
56            return new NfcF(tag);
57        } catch (RemoteException e) {
58            return null;
59        }
60    }
61
62    /** @hide */
63    public NfcF(Tag tag) throws RemoteException {
64        super(tag, TagTechnology.NFC_F);
65        Bundle extras = tag.getTechExtras(TagTechnology.NFC_F);
66        if (extras != null) {
67            mSystemCode = extras.getByteArray(EXTRA_SC);
68            mManufacturer = extras.getByteArray(EXTRA_PMM);
69        }
70    }
71
72    /**
73     * Return the System Code bytes from tag discovery.
74     *
75     * <p>Does not cause any RF activity and does not block.
76     *
77     * @return System Code bytes
78     */
79    public byte[] getSystemCode() {
80      return mSystemCode;
81    }
82
83    /**
84     * Return the Manufacturer bytes from tag discovery.
85     *
86     * <p>Does not cause any RF activity and does not block.
87     *
88     * @return Manufacturer bytes
89     */
90    public byte[] getManufacturer() {
91      return mManufacturer;
92    }
93
94    /**
95     * Send raw NFC-F commands to the tag and receive the response.
96     *
97     * <p>Applications must not append the SoD (length) or EoD (CRC) to the payload,
98     * it will be automatically calculated.
99     *
100     * <p>This is an I/O operation and will block until complete. It must
101     * not be called from the main application thread. A blocked call will be canceled with
102     * {@link IOException} if {@link #close} is called from another thread.
103     *
104     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
105     *
106     * @param data bytes to send
107     * @return bytes received in response
108     * @throws TagLostException if the tag leaves the field
109     * @throws IOException if there is an I/O failure, or this operation is canceled
110     */
111    public byte[] transceive(byte[] data) throws IOException {
112        return transceive(data, true);
113    }
114}
115