| 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 | |
| 17 | package android.nfc.tech; |
| 18 | |
| 19 | import android.nfc.Tag; |
| 20 | import android.os.Bundle; |
| 21 | import 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 | */ |
| 29 | public 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 RFC3987. |
| 106 | * {@see <a href="http://www.ietf.org/rfc/rfc3987.txt">RFC 3987</a>}</li> |
| 107 | * <li> In case of GS1 EPC data, see <a href="http://www.gs1.org/gsmp/kc/epcglobal/tds/"> |
| 108 | * GS1 Electronic Product Code (EPC) Tag Data Standard (TDS)</a> for more details. |
| 109 | * </li> |
| 110 | * </ul> |
| 111 | * <p>The last 2 bytes comprise the CRC. |
| 112 | * </ul> |
| 113 | * <p>Does not cause any RF activity and does not block. |
| 114 | * |
| 115 | * @return a byte array containing the barcode |
| 116 | * @see <a href="http://www.thinfilm.no/docs/thinfilm-nfc-barcode-datasheet.pdf"> |
| 117 | * Thinfilm NFC Barcode tag specification (previously Kovio NFC Barcode)</a> |
| 118 | * @see <a href="http://www.thinfilm.no/docs/thinfilm-nfc-barcode-data-format.pdf"> |
| 119 | * Thinfilm NFC Barcode data format (previously Kovio NFC Barcode)</a> |
| 120 | */ |
| 121 | public byte[] getBarcode() { |
| 122 | switch (mType) { |
| 123 | case TYPE_KOVIO: |
| 124 | // For Kovio tags the barcode matches the ID |
| 125 | return mTag.getId(); |
| 126 | default: |
| 127 | return null; |
| 128 | } |
| 129 | } |
| 130 | } |