Tag.java revision 1253ebc74a8453a88dc47a2b698145098d201681
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.nfc.technology.IsoDep; 20import android.nfc.technology.MifareClassic; 21import android.nfc.technology.NfcV; 22import android.nfc.technology.Ndef; 23import android.nfc.technology.NfcA; 24import android.nfc.technology.NfcB; 25import android.nfc.technology.NfcF; 26import android.nfc.technology.TagTechnology; 27import android.os.Bundle; 28import android.os.Parcel; 29import android.os.Parcelable; 30import android.os.RemoteException; 31 32import java.util.Arrays; 33 34/** 35 * Represents a (generic) discovered tag. 36 * <p> 37 * A tag is a passive NFC element, such as NFC Forum Tag's, Mifare class Tags, 38 * Sony Felica Tags. 39 * <p> 40 * Tag's have a type and usually have a UID. 41 * <p> 42 * {@link Tag} objects are passed to applications via the {@link NfcAdapter#EXTRA_TAG} extra 43 * in {@link NfcAdapter#ACTION_TAG_DISCOVERED} intents. A {@link Tag} object is immutable 44 * and represents the state of the tag at the time of discovery. It can be 45 * directly queried for its UID and Type, or used to create a {@link TagTechnology} 46 * (with {@link Tag#getTechnology(int)}). 47 * <p> 48 * A {@link Tag} can be used to create a {@link TagTechnology} only while the tag is in 49 * range. If it is removed and then returned to range, then the most recent 50 * {@link Tag} object (in {@link NfcAdapter#ACTION_TAG_DISCOVERED}) should be used to create a 51 * {@link TagTechnology}. 52 * <p>This is an immutable data class. All properties are set at Tag discovery 53 * time and calls on this class will retrieve those read-only properties, and 54 * not cause any further RF activity or block. Note however that arrays passed to and 55 * returned by this class are *not* cloned, so be careful not to modify them. 56 * @hide 57 */ 58public class Tag implements Parcelable { 59 /*package*/ final byte[] mId; 60 /*package*/ final int[] mTechList; 61 /*package*/ final Bundle[] mTechExtras; 62 /*package*/ final int mServiceHandle; // for use by NFC service, 0 indicates a mock 63 64 /** 65 * Hidden constructor to be used by NFC service and internal classes. 66 * @hide 67 */ 68 public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle) { 69 if (techList == null) { 70 throw new IllegalArgumentException("rawTargets cannot be null"); 71 } 72 mId = id; 73 mTechList = Arrays.copyOf(techList, techList.length); 74 // Ensure mTechExtras is as long as mTechList 75 mTechExtras = Arrays.copyOf(techListExtras, techList.length); 76 mServiceHandle = serviceHandle; 77 } 78 79 /** 80 * Construct a mock Tag. 81 * <p>This is an application constructed tag, so NfcAdapter methods on this 82 * Tag such as {@link #getTechnology} may fail with 83 * {@link IllegalArgumentException} since it does not represent a physical Tag. 84 * <p>This constructor might be useful for mock testing. 85 * @param id The tag identifier, can be null 86 * @param techList must not be null 87 * @return freshly constructed tag 88 */ 89 public static Tag createMockTag(byte[] id, int[] techList, Bundle[] techListExtras) { 90 // set serviceHandle to 0 to indicate mock tag 91 return new Tag(id, techList, techListExtras, 0); 92 } 93 94 /** 95 * For use by NfcService only. 96 * @hide 97 */ 98 public int getServiceHandle() { 99 return mServiceHandle; 100 } 101 102 /** 103 * Get the Tag Identifier (if it has one). 104 * <p>Tag ID is usually a serial number for the tag. 105 * 106 * @return ID, or null if it does not exist 107 */ 108 public byte[] getId() { 109 return mId; 110 } 111 112 /** 113 * Returns technologies present in the tag that this implementation understands, 114 * or a zero length array if there are no supported technologies on this tag. 115 */ 116 public int[] getTechnologyList() { 117 return Arrays.copyOf(mTechList, mTechList.length); 118 } 119 120 /** 121 * Returns the technology, or null if not present 122 */ 123 public TagTechnology getTechnology(int tech) { 124 int pos = -1; 125 for (int idx = 0; idx < mTechList.length; idx++) { 126 if (mTechList[idx] == tech) { 127 pos = idx; 128 break; 129 } 130 } 131 if (pos < 0) { 132 return null; 133 } 134 135 Bundle extras = mTechExtras[pos]; 136 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(); 137 try { 138 switch (tech) { 139 case TagTechnology.NFC_A: { 140 return new NfcA(adapter, this, extras); 141 } 142 case TagTechnology.NFC_B: { 143 return new NfcB(adapter, this, extras); 144 } 145 case TagTechnology.ISO_DEP: { 146 return new IsoDep(adapter, this, extras); 147 } 148 case TagTechnology.NFC_V: { 149 return new NfcV(adapter, this, extras); 150 } 151 case TagTechnology.TYPE_1: 152 case TagTechnology.TYPE_2: 153 case TagTechnology.TYPE_3: 154 case TagTechnology.TYPE_4: { 155 return new Ndef(adapter, this, tech, extras); 156 } 157 case TagTechnology.NFC_F: { 158 return new NfcF(adapter, this, extras); 159 } 160 case TagTechnology.MIFARE_CLASSIC: { 161 return new MifareClassic(adapter, this, extras); 162 } 163 164 default: { 165 throw new UnsupportedOperationException("Tech " + tech + " not supported"); 166 } 167 } 168 } catch (RemoteException e) { 169 return null; 170 } 171 } 172 173 @Override 174 public String toString() { 175 StringBuilder sb = new StringBuilder("TAG ") 176 .append("uid = ") 177 .append(mId) 178 .append(" Tech ["); 179 for (int i : mTechList) { 180 sb.append(i) 181 .append(", "); 182 } 183 return sb.toString(); 184 } 185 186 /*package*/ static byte[] readBytesWithNull(Parcel in) { 187 int len = in.readInt(); 188 byte[] result = null; 189 if (len >= 0) { 190 result = new byte[len]; 191 in.readByteArray(result); 192 } 193 return result; 194 } 195 196 /*package*/ static void writeBytesWithNull(Parcel out, byte[] b) { 197 if (b == null) { 198 out.writeInt(-1); 199 return; 200 } 201 out.writeInt(b.length); 202 out.writeByteArray(b); 203 } 204 205 @Override 206 public int describeContents() { 207 return 0; 208 } 209 210 @Override 211 public void writeToParcel(Parcel dest, int flags) { 212 writeBytesWithNull(dest, mId); 213 dest.writeInt(mTechList.length); 214 dest.writeIntArray(mTechList); 215 dest.writeTypedArray(mTechExtras, 0); 216 dest.writeInt(mServiceHandle); 217 } 218 219 public static final Parcelable.Creator<Tag> CREATOR = 220 new Parcelable.Creator<Tag>() { 221 @Override 222 public Tag createFromParcel(Parcel in) { 223 // Tag fields 224 byte[] id = Tag.readBytesWithNull(in); 225 int[] techList = new int[in.readInt()]; 226 in.readIntArray(techList); 227 Bundle[] techExtras = in.createTypedArray(Bundle.CREATOR); 228 int serviceHandle = in.readInt(); 229 230 return new Tag(id, techList, techExtras, serviceHandle); 231 } 232 233 @Override 234 public Tag[] newArray(int size) { 235 return new Tag[size]; 236 } 237 }; 238} 239