NfcB.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-B technology, also known as
27 * ISO1443-3B.
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 NfcB extends BasicTagTechnology {
40    /** @hide */
41    public static final String EXTRA_APPDATA = "appdata";
42    /** @hide */
43    public static final String EXTRA_PROTINFO = "protinfo";
44
45    private byte[] mAppData;
46    private byte[] mProtInfo;
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 NfcB get(Tag tag) {
55        if (!tag.hasTech(TagTechnology.NFC_B)) return null;
56        try {
57            return new NfcB(tag);
58        } catch (RemoteException e) {
59            return null;
60        }
61    }
62
63    /** @hide */
64    public NfcB(Tag tag) throws RemoteException {
65        super(tag, TagTechnology.NFC_B);
66        Bundle extras = tag.getTechExtras(TagTechnology.NFC_B);
67        mAppData = extras.getByteArray(EXTRA_APPDATA);
68        mProtInfo = extras.getByteArray(EXTRA_PROTINFO);
69    }
70
71    /**
72     * Returns the Application Data bytes from the ATQB/SENSB_RES
73     * bytes discovered at tag discovery.
74     */
75    public byte[] getApplicationData() {
76        return mAppData;
77    }
78
79    /**
80     * Returns the Protocol Info bytes from the ATQB/SENSB_RES
81     * bytes discovered at tag discovery.
82     */
83    public byte[] getProtocolInfo() {
84        return mProtInfo;
85    }
86
87    /**
88     * Send data to a tag and receive the response.
89     * <p>
90     * This method will block until the response is received. It can be canceled
91     * with {@link #close}.
92     * <p>Requires {@link android.Manifest.permission#NFC} permission.
93     *
94     * @param data bytes to send
95     * @return bytes received in response
96     * @throws IOException if the target is lost or connection closed
97     */
98    public byte[] transceive(byte[] data) throws IOException {
99        return transceive(data, true);
100    }
101}
102