NfcBarcode.java revision 7a6dfb2f7294bbfb5ebc4f937db97b659a447607
1/* 2 * Copyright (C) 2012 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 23/** 24 * Provides access to tags containing just a barcode. 25 * 26 * <p>Acquire an {@link NfcBarcode} object using {@link #get}. 27 * 28 */ 29public final class NfcBarcode extends BasicTagTechnology { 30 31 /** Kovio Tags */ 32 public static final int TYPE_KOVIO = 1; 33 public static final int TYPE_UNKNOWN = -1; 34 35 /** @hide */ 36 public static final String EXTRA_BARCODE_TYPE = "barcodetype"; 37 38 private int mType; 39 40 /** 41 * Get an instance of {@link NfcBarcode} for the given tag. 42 * 43 * <p>Returns null if {@link NfcBarcode} was not enumerated in {@link Tag#getTechList}. 44 * 45 * <p>Does not cause any RF activity and does not block. 46 * 47 * @param tag an NfcBarcode compatible tag 48 * @return NfcBarcode object 49 */ 50 public static NfcBarcode get(Tag tag) { 51 if (!tag.hasTech(TagTechnology.NFC_BARCODE)) return null; 52 try { 53 return new NfcBarcode(tag); 54 } catch (RemoteException e) { 55 return null; 56 } 57 } 58 59 /** 60 * Internal constructor, to be used by NfcAdapter 61 * @hide 62 */ 63 public NfcBarcode(Tag tag) throws RemoteException { 64 super(tag, TagTechnology.NFC_BARCODE); 65 Bundle extras = tag.getTechExtras(TagTechnology.NFC_BARCODE); 66 if (extras != null) { 67 mType = extras.getInt(EXTRA_BARCODE_TYPE); 68 } else { 69 throw new NullPointerException("NfcBarcode tech extras are null."); 70 } 71 } 72 73 /** 74 * Returns the NFC Barcode tag type. 75 * 76 * <p>Currently only one of {@link #TYPE_KOVIO} or {@link #TYPE_UNKNOWN}. 77 * 78 * <p>Does not cause any RF activity and does not block. 79 * 80 * @return the NFC Barcode tag type 81 */ 82 public int getType() { 83 return mType; 84 } 85 86 /** 87 * Returns the barcode of an NfcBarcode tag. 88 * 89 * <p> Tags of {@link #TYPE_KOVIO} return 16 bytes: 90 * <ul> 91 * <p> The first byte is 0x80 ORd with a manufacturer ID, corresponding 92 * to ISO/IEC 7816-6. 93 * <p> The second byte describes the payload data format. Defined data 94 * format types include the following:<ul> 95 * <li>0x00: Reserved for manufacturer assignment</li> 96 * <li>0x01: 96-bit URL with "http://www." prefix</li> 97 * <li>0x02: 96-bit URL with "https://www." prefix</li> 98 * <li>0x03: 96-bit URL with "http://" prefix</li> 99 * <li>0x04: 96-bit URL with "https://" prefix</li> 100 * <li>0x05: 96-bit GS1 EPC</li> 101 * <li>0x06-0xFF: reserved</li> 102 * </ul> 103 * <p>The following 12 bytes are payload:<ul> 104 * <li> In case of a URL payload, the payload is encoded in US-ASCII, 105 * following the limitations defined in RF3987, 106 * {@see http://www.ietf.org/rfc/rfc3987.txt}</li> 107 * <li> In case of GS1 EPC daya, {@see http://www.gs1.org/gsmp/kc/epcglobal/tds/} 108 * for more details.</li></ul> 109 * <p>The last 2 bytes comprise the CRC. 110 * </ul> 111 * <p>Does not cause any RF activity and does not block. 112 * 113 * @return a byte array containing the barcode 114 */ 115 public byte[] getBarcode() { 116 switch (mType) { 117 case TYPE_KOVIO: 118 // For Kovio tags the barcode matches the ID 119 return mTag.getId(); 120 default: 121 return null; 122 } 123 } 124} 125