OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.device.nfc; | |
6 | |
7 import android.nfc.FormatException; | |
8 import android.nfc.NdefMessage; | |
9 import android.nfc.Tag; | |
10 import android.nfc.TagLostException; | |
11 import android.nfc.tech.Ndef; | |
12 import android.nfc.tech.NdefFormatable; | |
13 import android.nfc.tech.TagTechnology; | |
14 | |
15 import java.io.IOException; | |
16 | |
17 /** | |
18 * Utility class that provides I/O operations for NFC tags. | |
19 */ | |
20 public class NfcTagHandler { | |
21 private final TagTechnology mTech; | |
22 private final TagTechnologyHandler mTechHandler; | |
23 private boolean mWasConnected; | |
24 | |
25 /** | |
26 * Factory method that creates NfcTagHandler for a given NFC Tag. | |
27 * | |
28 * @param tag @see android.nfc.Tag | |
29 * @return NfcTagHandler or null when unsupported Tag is provided. | |
30 */ | |
31 public static NfcTagHandler create(Tag tag) { | |
32 if (tag == null) return null; | |
33 | |
34 Ndef ndef = Ndef.get(tag); | |
35 if (ndef != null) return new NfcTagHandler(ndef, new NdefHandler(ndef)); | |
36 | |
37 NdefFormatable formattable = NdefFormatable.get(tag); | |
38 if (formattable != null) { | |
39 return new NfcTagHandler(formattable, new NdefFormattableHandler(for
mattable)); | |
40 } | |
41 | |
42 return null; | |
43 } | |
44 | |
45 /** | |
46 * NdefFormatable and Ndef interfaces have different signatures for operatin
g with NFC tags. | |
47 * This interface provides generic methods. | |
48 */ | |
49 private interface TagTechnologyHandler { | |
50 public void write(NdefMessage message) | |
51 throws IOException, TagLostException, FormatException, IllegalSt
ateException; | |
52 public NdefMessage read() | |
53 throws IOException, TagLostException, FormatException, IllegalSt
ateException; | |
54 } | |
55 | |
56 /** | |
57 * Implementation of TagTechnologyHandler that uses Ndef tag technology. | |
58 * @see android.nfc.tech.Ndef | |
59 */ | |
60 private static class NdefHandler implements TagTechnologyHandler { | |
61 private final Ndef mNdef; | |
62 | |
63 NdefHandler(Ndef ndef) { | |
64 mNdef = ndef; | |
65 } | |
66 | |
67 @Override | |
68 public void write(NdefMessage message) | |
69 throws IOException, TagLostException, FormatException, IllegalSt
ateException { | |
70 mNdef.writeNdefMessage(message); | |
71 } | |
72 | |
73 @Override | |
74 public NdefMessage read() | |
75 throws IOException, TagLostException, FormatException, IllegalSt
ateException { | |
76 return mNdef.getNdefMessage(); | |
77 } | |
78 } | |
79 | |
80 /** | |
81 * Implementation of TagTechnologyHandler that uses NdefFormatable tag techn
ology. | |
82 * @see android.nfc.tech.NdefFormatable | |
83 */ | |
84 private static class NdefFormattableHandler implements TagTechnologyHandler
{ | |
85 private final NdefFormatable mNdefFormattable; | |
86 | |
87 NdefFormattableHandler(NdefFormatable ndefFormattable) { | |
88 mNdefFormattable = ndefFormattable; | |
89 } | |
90 | |
91 @Override | |
92 public void write(NdefMessage message) | |
93 throws IOException, TagLostException, FormatException, IllegalSt
ateException { | |
94 mNdefFormattable.format(message); | |
95 } | |
96 | |
97 @Override | |
98 public NdefMessage read() throws FormatException { | |
99 return NfcTypeConverter.emptyNdefMessage(); | |
100 } | |
101 } | |
102 | |
103 protected NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler) { | |
104 mTech = tech; | |
105 mTechHandler = handler; | |
106 } | |
107 | |
108 /** | |
109 * Connects to NFC tag. | |
110 */ | |
111 public void connect() throws IOException, TagLostException { | |
112 if (!mTech.isConnected()) { | |
113 mTech.connect(); | |
114 mWasConnected = true; | |
115 } | |
116 } | |
117 | |
118 /** | |
119 * Checks if NFC tag is connected. | |
120 */ | |
121 public boolean isConnected() { | |
122 return mTech.isConnected(); | |
123 } | |
124 | |
125 /** | |
126 * Closes connection. | |
127 */ | |
128 public void close() throws IOException { | |
129 mTech.close(); | |
130 } | |
131 | |
132 /** | |
133 * Writes NdefMessage to NFC tag. | |
134 */ | |
135 public void write(NdefMessage message) | |
136 throws IOException, TagLostException, FormatException, IllegalStateE
xception { | |
137 mTechHandler.write(message); | |
138 } | |
139 | |
140 public NdefMessage read() | |
141 throws IOException, TagLostException, FormatException, IllegalStateE
xception { | |
142 return mTechHandler.read(); | |
143 } | |
144 | |
145 /** | |
146 * If tag was previously connected and subsequent connection to the same tag
fails, consider | |
147 * tag to be out of range. | |
148 */ | |
149 public boolean isTagOutOfRange() { | |
150 try { | |
151 connect(); | |
152 } catch (IOException e) { | |
153 return mWasConnected; | |
154 } | |
155 return false; | |
156 } | |
157 } | |
OLD | NEW |