NfcA.java revision 4e21e1d21a877cce4db5ec8c5786604cc10f2d7e
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 * A low-level connection to a {@link Tag} using the NFC-A technology, also known as
27 * ISO1443-3A.
28 *
29 * <p>You can acquire this kind of connection with {@link #get}.
30 * Use this class to send and receive data with {@link #transceive transceive()}.
31 *
32 * <p>Applications must implement their own protocol stack on top of
33 * {@link #transceive transceive()}.
34 *
35 * <p class="note"><strong>Note:</strong>
36 * Use of this class requires the {@link android.Manifest.permission#NFC}
37 * permission.
38 */
39public final class NfcA extends BasicTagTechnology {
40    /** @hide */
41    public static final String EXTRA_SAK = "sak";
42    /** @hide */
43    public static final String EXTRA_ATQA = "atqa";
44
45    private short mSak;
46    private byte[] mAtqa;
47
48    /**
49     * Returns an instance of this tech for the given tag. If the tag doesn't support
50     * this tech type null is returned.
51     *
52     * @param tag The tag to get the tech from
53     */
54    public static NfcA get(Tag tag) {
55        if (!tag.hasTech(TagTechnology.NFC_A)) return null;
56        try {
57            return new NfcA(tag);
58        } catch (RemoteException e) {
59            return null;
60        }
61    }
62
63    /** @hide */
64    public NfcA(Tag tag) throws RemoteException {
65        super(tag, TagTechnology.NFC_A);
66        Bundle extras = tag.getTechExtras(TagTechnology.NFC_A);
67        mSak = extras.getShort(EXTRA_SAK);
68        mAtqa = extras.getByteArray(EXTRA_ATQA);
69    }
70
71    /**
72     * Returns the ATQA/SENS_RES bytes discovered at tag discovery.
73     */
74    public byte[] getAtqa() {
75        return mAtqa;
76    }
77
78    /**
79     * Returns the SAK/SEL_RES discovered at tag discovery.
80     */
81    public short getSak() {
82        return mSak;
83    }
84
85    /**
86     * Send data to a tag and receive the response.
87     * <p>
88     * This method will block until the response is received. It can be canceled
89     * with {@link #close}.
90     * <p>Requires {@link android.Manifest.permission#NFC} permission.
91     *
92     * @param data bytes to send
93     * @return bytes received in response
94     * @throws IOException if the target is lost or connection closed
95     */
96    public byte[] transceive(byte[] data) throws IOException {
97        return transceive(data, true);
98    }
99}
100