NfcV.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 NFC vicinity technology, also known as
27 * ISO15693.
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 NfcV extends BasicTagTechnology {
40    /** @hide */
41    public static final String EXTRA_RESP_FLAGS = "respflags";
42
43    /** @hide */
44    public static final String EXTRA_DSFID = "dsfid";
45
46    private byte mRespFlags;
47    private byte mDsfId;
48
49    /**
50     * Returns an instance of this tech for the given tag. If the tag doesn't support
51     * this tech type null is returned.
52     *
53     * @param tag The tag to get the tech from
54     */
55    public static NfcV get(Tag tag) {
56        if (!tag.hasTech(TagTechnology.NFC_V)) return null;
57        try {
58            return new NfcV(tag);
59        } catch (RemoteException e) {
60            return null;
61        }
62    }
63
64    /** @hide */
65    public NfcV(Tag tag) throws RemoteException {
66        super(tag, TagTechnology.NFC_V);
67        Bundle extras = tag.getTechExtras(TagTechnology.NFC_V);
68        mRespFlags = extras.getByte(EXTRA_RESP_FLAGS);
69        mDsfId = extras.getByte(EXTRA_DSFID);
70    }
71
72    public byte getResponseFlags() {
73        return mRespFlags;
74    }
75
76    public byte getDsfId() {
77        return mDsfId;
78    }
79
80    /**
81     * Send data to a tag and receive the response.
82     * <p>
83     * This method will block until the response is received. It can be canceled
84     * with {@link #close}.
85     * <p>Requires {@link android.Manifest.permission#NFC} permission.
86     *
87     * @param data bytes to send
88     * @return bytes received in response
89     * @throws IOException if the target is lost or connection closed
90     */
91    public byte[] transceive(byte[] data) throws IOException {
92        return transceive(data, true);
93    }
94}
95