Tag.java revision 590b73bc5b8e5f7b59bff1d9264a52388a5162e6
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;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * Immutable data class, represents a discovered tag.
24 * <p>
25 * A tag is a passive NFC element, such as NFC Forum Tag's, Mifare class Tags,
26 * Sony Felica Tags.
27 * <p>
28 * Tag's have a type and usually have a UID.
29 * <p>
30 * Tag objects are passed to applications via the NfcAdapter.EXTRA_TAG extra
31 * in NfcAdapter.ACTION_TAG_DISCOVERED intents. The Tag object is immutable
32 * and represents the state of the Tag at the time of discovery. It can be
33 * directly queried for its UID and Type, or used to create a TagConnection
34 * (NfcAdapter.createTagConnection()).
35 * <p>
36 * This Tag object can only be used to create a TagConnection while it is in
37 * range. If it is removed and then returned to range then the most recent
38 * Tag object (in ACTION_TAG_DISCOVERED) should be used to create a
39 * TagConnection.
40 */
41public class Tag implements Parcelable {
42
43    /**
44     * @hide
45     */
46    public static final int NFC_TAG_ISO14443_A = 1; /* phNfc_eISO14443_A_PICC */
47
48    /**
49     * @hide
50     */
51    public static final int NFC_TAG_ISO14443_4A = 2; /* phNfc_eISO14443_4A_PICC */
52
53    /**
54     * @hide
55     */
56    public static final int NFC_TAG_ISO14443_3A = 3; /* phNfc_eISO14443_3A_PICC */
57
58    /**
59     * @hide
60     */
61    public static final int NFC_TAG_MIFARE = 4; /* phNfc_eMifare_PICC */
62
63    /**
64     * @hide
65     */
66    public static final int NFC_TAG_ISO14443_B = 5; /* phNfc_eISO14443_B_PICC */
67
68    /**
69     * @hide
70     */
71    public static final int NFC_TAG_ISO14443_4B = 6; /* phNfc_eISO14443_4B_PICC */
72
73    /**
74     * @hide
75     */
76    public static final int NFC_TAG_ISO14443_B_PRIME = 7; /* phNfc_eISO14443_BPrime_PICC */
77
78    /**
79     * @hide
80     */
81    public static final int NFC_TAG_FELICA = 8; /* phNfc_eFelica_PICC */
82
83    /**
84     * @hide
85     */
86    public static final int NFC_TAG_JEWEL = 9; /* phNfc_eJewel_PICC */
87
88    /**
89     * @hide
90     */
91    public static final int NFC_TAG_ISO15693 = 10; /* phNfc_eISO15693_PICC */
92
93    /**
94     * @hide
95     */
96    public static final int NFC_TAG_OTHER = 11; /* phNfc_ePICC_DevType */
97
98
99    public static final String TARGET_ISO_14443_3A = "iso14443_3a";
100
101    public static final String TARGET_ISO_14443_3B = "iso14443_3b";
102
103    public static final String TARGET_ISO_14443_3B_PRIME = "iso14443_3b";
104
105    public static final String TARGET_ISO_14443_4 = "iso14443_4";
106
107    public static final String TARGET_ISO_15693 = "iso15693";
108
109    public static final String TARGET_JIS_X_6319_4 = "jis_x_6319_4";
110
111    public static final String TARGET_TOPAZ = "topaz";
112
113    public static final String TARGET_OTHER = "other";
114
115    /*package*/ final int mType;
116    /*package*/ final boolean mIsNdef;
117    /*package*/ final byte[] mUid;
118    /*package*/ final int mNativeHandle;
119
120    /**
121     * Hidden constructor to be used by NFC service only.
122     * @hide
123     */
124    public Tag(int type, boolean isNdef, byte[] uid, int nativeHandle) {
125        mType = type;
126        mIsNdef = isNdef;
127        mUid = uid.clone();
128        mNativeHandle = nativeHandle;
129    }
130
131    /**
132     * For use by NfcService only.
133     * @hide
134     */
135    public int getHandle() {
136        return mNativeHandle;
137    }
138
139    /**
140     * Return the available targets that this NFC adapter can use to create
141     * a RawTagConnection.
142     *
143     * @return
144     */
145    public String[] getRawTargets() {
146        //TODO
147        throw new UnsupportedOperationException();
148    }
149
150    /**
151     * Get the Tag type.
152     * <p>
153     * The Tag type is one of the NFC_TAG constants. It is read at discovery
154     * time and this method does not cause any further RF activity and does not
155     * block.
156     *
157     * @return a NFC_TAG constant
158     * @hide
159     */
160    public int getType() {
161        return mType;
162    }
163
164    /**
165     * Get the Tag Identifier (if it has one).
166     * <p>
167     * Tag ID is usually a serial number for the tag.
168     * <p>
169     * The Tag ID is read at discovery time and this method does not cause any
170     * further RF activity and does not block.
171     *
172     * @return ID, or null if it does not exist
173     */
174    public byte[] getId() {
175        if (mUid.length > 0) {
176            return mUid.clone();
177        } else {
178            return null;
179        }
180    }
181
182    @Override
183    public int describeContents() {
184        return 0;
185    }
186
187    @Override
188    public void writeToParcel(Parcel dest, int flags) {
189        boolean[] booleans = new boolean[] {mIsNdef};
190        dest.writeInt(mType);
191        dest.writeBooleanArray(booleans);
192        dest.writeInt(mUid.length);
193        dest.writeByteArray(mUid);
194        dest.writeInt(mNativeHandle);
195    }
196
197    public static final Parcelable.Creator<Tag> CREATOR =
198            new Parcelable.Creator<Tag>() {
199        public Tag createFromParcel(Parcel in) {
200            boolean[] booleans = new boolean[1];
201            int type = in.readInt();
202            in.readBooleanArray(booleans);
203            boolean isNdef = booleans[0];
204            int uidLength = in.readInt();
205            byte[] uid = new byte[uidLength];
206            in.readByteArray(uid);
207            int nativeHandle = in.readInt();
208
209            return new Tag(type, isNdef, uid, nativeHandle);
210        }
211        public Tag[] newArray(int size) {
212            return new Tag[size];
213        }
214    };
215}