NfcBarcode.java revision 0bec15ebed8b8639076cba184af3235e17f48718
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>Does not cause any RF activity and does not block.
90     *
91     * @return a byte array containing the barcode
92     */
93    public byte[] getBarcode() {
94        switch (mType) {
95            case TYPE_KOVIO:
96                // For Kovio tags the barcode matches the ID
97                return mTag.getId();
98            default:
99                return null;
100        }
101    }
102}
103