1
1
package com .oreilly .demo .android .pa .sensordemo ;
2
2
3
3
import java .nio .charset .Charset ;
4
- import java .util .Arrays ;
5
4
6
5
import android .app .Activity ;
7
6
import android .content .Intent ;
@@ -27,7 +26,7 @@ public NdefMessage createNdefMessage(NfcEvent event) {
27
26
};
28
27
29
28
private NdefMessage createNDFMsg (boolean now ) {
30
- String text = "Beam " +(now ? "NOW" : "ON" )+" com.oreilly.demo.android.pa.sensordemo.NFC40 Beam with" + (AARUSE ? "" : "out" ) + " AAR Use" ;
29
+ String text = "Beam " +(now ? "NOW" : "ON" )+" com.oreilly.demo.android.pa.sensordemo Beam with" + (AARUSE ? "" : "out" ) + " AAR Use" ;
31
30
byte [] mimeBytes = "application/com.oreilly.demo.android.pa.sensordemo" .getBytes (Charset .forName ("US-ASCII" ));
32
31
NdefRecord mimeRecord = new NdefRecord (NdefRecord .TNF_MIME_MEDIA , mimeBytes , new byte [0 ], text .getBytes ());
33
32
@@ -87,21 +86,15 @@ public void onClick(View v) {
87
86
findViewById (R .id .beamon ).setVisibility (View .GONE );
88
87
findViewById (R .id .beamnow ).setVisibility (View .GONE );
89
88
90
- findViewById (R .id .tagtype ).setVisibility (View .VISIBLE );
91
- findViewById (R .id .tagid ).setVisibility (View .VISIBLE );
92
89
findViewById (R .id .tagdata ).setVisibility (View .VISIBLE );
93
90
94
91
analyzeIntent (getIntent ());
95
92
} else if (NfcAdapter .getDefaultAdapter (this ) == null || !NfcAdapter .getDefaultAdapter (this ).isEnabled ()) {
96
93
findViewById (R .id .beamon ).setVisibility (View .GONE );
97
94
findViewById (R .id .beamnow ).setVisibility (View .GONE );
98
- findViewById (R .id .tagtype ).setVisibility (View .GONE );
99
- findViewById (R .id .tagdata ).setVisibility (View .GONE );
100
95
101
- ((TextView ) findViewById (R .id .tagid )).setText ("NFC not enabled!" );
96
+ ((TextView ) findViewById (R .id .tagdata )).setText ("NFC not enabled!" );
102
97
} else {
103
- findViewById (R .id .tagtype ).setVisibility (View .GONE );
104
- findViewById (R .id .tagid ).setVisibility (View .GONE );
105
98
findViewById (R .id .tagdata ).setVisibility (View .GONE );
106
99
107
100
findViewById (R .id .beamon ).setVisibility (View .VISIBLE );
@@ -120,257 +113,18 @@ public void onClick(View v) {
120
113
}
121
114
});
122
115
}
123
- }catch (Exception t ) {
124
- ((TextView ) findViewById (R .id .tagid )).setText ("ERROR: " +t .toString ());
116
+ } catch (Exception t ) {
117
+ findViewById (R .id .tagdata ).setVisibility (View .VISIBLE );
118
+ ((TextView ) findViewById (R .id .tagdata )).setText ("ERROR: " +t .toString ());
119
+ t .printStackTrace ();
125
120
}
126
121
}
127
122
128
-
129
- // From here on down is the same code as in com.oreilly.demo.android.pa.sensordemo.NFC
130
- private static enum NFCType {
131
- UNKNOWN , TEXT , URI , SMART_POSTER , ABSOLUTE_URI
132
- }
133
-
134
- private void analyzeIntent (final Intent intent ) {
123
+ private void analyzeIntent (final Intent intent ) {
135
124
if (intent == null ) return ;
136
-
137
- String id = getTagId (intent );
138
- NFCType type = NFCType .UNKNOWN ;
139
- String datastr = null ;
140
- byte [] data = null ;
141
-
142
- NdefMessage tag = getTagData (intent );
143
- if (tag != null ) {
144
- type = getTagType (tag );
145
- if (type != NFCType .UNKNOWN ) {
146
- datastr = getTagData (tag );
147
- } else data = getTagRawData (tag );
148
- }
149
-
150
- if (datastr != null ) updateViewInfo (id , type , datastr );
151
- else updateViewInfo (id , type , data );
152
- }
153
-
154
- private void updateViewInfo (String id , NFCType type , byte [] data ) {
155
- updateViewInfo (id , type , data != null ? getHexString (data ) : null );
156
- }
157
-
158
- private void updateViewInfo (String id , NFCType type , String data ) {
159
- if (id != null ) {
160
- ((TextView ) findViewById (R .id .tagid )).setText ("TagID: " +id );
161
- }
162
-
163
- if (type != NFCType .UNKNOWN ) {
164
- String typestr = "" ;
165
- switch (type ) {
166
- case TEXT : typestr = "Text" ; break ;
167
- case SMART_POSTER : typestr = "Smart Poster" ; break ;
168
- case URI : typestr = "URI" ; break ;
169
- case ABSOLUTE_URI : typestr = "URI (Abs)" ; break ;
170
- default : typestr = "UNKNOWN" ;
171
- break ;
172
- }
173
- ((TextView ) findViewById (R .id .tagtype )).setText ("TagType: " +typestr );
174
- }
175
-
176
- if (data != null ) {
177
- ((TextView ) findViewById (R .id .tagdata )).setText ("TagData:\n " +data );
178
- }
179
- }
180
-
181
- private String getTagId (final Intent intent ) {
182
- if (intent == null ) return null ;
183
- byte [] byte_id = intent .getByteArrayExtra (NfcAdapter .EXTRA_ID );
184
- if (byte_id == null ) return null ;
185
-
186
- return getHexString (byte_id );
187
- }
188
-
189
- private NdefMessage getTagData (final Intent intent ) {
190
- if (intent == null || !intent .hasExtra (NfcAdapter .EXTRA_NDEF_MESSAGES )) return null ;
191
-
125
+ findViewById (R .id .tagdata ).setVisibility (View .VISIBLE );
126
+
192
127
Parcelable [] msgs = intent .getParcelableArrayExtra (NfcAdapter .EXTRA_NDEF_MESSAGES );
193
-
194
- if (msgs == null || msgs .length < 1 ) {
195
- return null ;
196
- }
197
-
198
- NdefMessage [] nmsgs = new NdefMessage [msgs .length ];
199
- for (int i =0 ;i <msgs .length ;i ++) {
200
- nmsgs [i ] = (NdefMessage ) msgs [i ];
201
- }
202
-
203
- // we will only grab the first msg as we are handling only 1 tag at the moment
204
- return nmsgs [0 ];
205
- }
206
-
207
- private NFCType getTagType (final NdefMessage msg ) {
208
- if (msg == null ) return null ;
209
- // we are only grabbing the first recognizable item
210
-
211
- for (NdefRecord record : msg .getRecords ()) {
212
- if (record .getTnf () == NdefRecord .TNF_WELL_KNOWN ) {
213
- if (Arrays .equals (record .getType (), NdefRecord .RTD_TEXT )) {
214
- return NFCType .TEXT ;
215
- }
216
- if (Arrays .equals (record .getType (), NdefRecord .RTD_URI )) {
217
- return NFCType .URI ;
218
- }
219
- if (Arrays .equals (record .getType (), NdefRecord .RTD_SMART_POSTER )) {
220
- return NFCType .SMART_POSTER ;
221
- }
222
- } else if (record .getTnf () == NdefRecord .TNF_ABSOLUTE_URI ) {
223
- return NFCType .ABSOLUTE_URI ;
224
- }
225
- }
226
- return null ;
227
- }
228
-
229
- private String getTagData (final NdefMessage msg ) {
230
- if (msg == null ) return null ;
231
- // we are only grabbing the first recognizable item
232
-
233
- for (NdefRecord record : msg .getRecords ()) {
234
- if (record .getTnf () == NdefRecord .TNF_WELL_KNOWN ) {
235
- if (Arrays .equals (record .getType (), NdefRecord .RTD_TEXT )) {
236
- return getText (record .getPayload ());
237
- }
238
- if (Arrays .equals (record .getType (), NdefRecord .RTD_URI )) {
239
- return getURI (record .getPayload ());
240
- }
241
- if (Arrays .equals (record .getType (), NdefRecord .RTD_SMART_POSTER )) {
242
- if (record .getPayload () == null || record .getPayload ().length < 1 ) return null ;
243
- try {
244
- NdefMessage subrecords = new NdefMessage (record .getPayload ());
245
- return getSubRecordData (subrecords .getRecords ());
246
- } catch (Exception e ) {
247
- e .printStackTrace ();
248
- return null ;
249
- }
250
- }
251
- } else if (record .getTnf () == NdefRecord .TNF_ABSOLUTE_URI ) {
252
- return getAbsoluteURI (record .getPayload ());
253
- }
254
- }
255
- return null ;
256
- }
257
-
258
- private String getSubRecordData (final NdefRecord [] records ) {
259
- if (records == null || records .length < 1 ) return null ;
260
- String data = "" ;
261
- for (NdefRecord record : records ) {
262
- if (record .getTnf () == NdefRecord .TNF_WELL_KNOWN ) {
263
- if (Arrays .equals (record .getType (), NdefRecord .RTD_TEXT )) {
264
- data += getText (record .getPayload ()) + "\n " ;
265
- }
266
- if (Arrays .equals (record .getType (), NdefRecord .RTD_URI )) {
267
- data += getURI (record .getPayload ()) + "\n " ;
268
- } else {
269
- data += "OTHER KNOWN DATA\n " ;
270
- }
271
- } else if (record .getTnf () == NdefRecord .TNF_ABSOLUTE_URI ) {
272
- data += getAbsoluteURI (record .getPayload ()) + "\n " ;
273
- } else data += "OTHER UNKNOW DATA\n " ;
274
- }
275
- return data ;
276
- }
277
-
278
- private byte [] getTagRawData (final NdefMessage msg ) {
279
- if (msg == null || msg .getRecords ().length < 1 ) return null ;
280
- // we are only grabbing the first item
281
- return msg .getRecords ()[0 ].getPayload ();
282
- }
283
-
284
- /*
285
- * the First Byte of the payload contains the "Status Byte Encodings" field, per the NFC Forum "Text Record Type Definition" section 3.2.1.
286
- *
287
- * Bit_7 is the Text Encoding Field.
288
- * * if Bit_7 == 0 the the text is encoded in UTF-8 else if Bit_7 == 1 then the text is encoded in UTF16
289
- * Bit_6 is currently always 0 (reserved for future use)
290
- * Bits 5 to 0 are the length of the IANA language code.
291
- */
292
- private String getText (final byte [] payload ) {
293
- if (payload == null ) return null ;
294
- try {
295
- String textEncoding = ((payload [0 ] & 0200 ) == 0 ) ? "UTF-8" : "UTF-16" ;
296
- int languageCodeLength = payload [0 ] & 0077 ;
297
- return new String (payload , languageCodeLength + 1 , payload .length - languageCodeLength - 1 , textEncoding );
298
- } catch (Exception e ) {
299
- e .printStackTrace ();
300
- }
301
- return null ;
302
- }
303
-
304
- private String getAbsoluteURI (final byte [] payload ) {
305
- if (payload == null ) return null ;
306
- return new String (payload , Charset .forName ("UTF-8" ));
307
- }
308
-
309
- /**
310
- * the First Byte of the payload contains the prefix byte
311
- */
312
- private String getURI (final byte [] payload ) {
313
- if (payload == null || payload .length < 1 ) return null ;
314
- String prefix = convertUriPrefix (payload [0 ]);
315
- return (prefix != null ? prefix : "" ) + new String (Arrays .copyOfRange (payload , 1 ,payload .length ));
316
- }
317
-
318
- /**
319
- * NFC Forum "URI Record Type Definition"
320
- *
321
- * Conversion of prefix based on section 3.2.2 of the NFC Forum URI Record Type Definition document.
322
- */
323
- private String convertUriPrefix (final byte prefix ) {
324
- if (prefix == (byte ) 0x00 ) return "" ;
325
- else if (prefix == (byte ) 0x01 ) return "http://www." ;
326
- else if (prefix == (byte ) 0x02 ) return "https://www." ;
327
- else if (prefix == (byte ) 0x03 ) return "http://" ;
328
- else if (prefix == (byte ) 0x04 ) return "https://" ;
329
- else if (prefix == (byte ) 0x05 ) return "tel:" ;
330
- else if (prefix == (byte ) 0x06 ) return "mailto:" ;
331
- else if (prefix == (byte ) 0x07 ) return "ftp://anonymous:anonymous@" ;
332
- else if (prefix == (byte ) 0x08 ) return "ftp://ftp." ;
333
- else if (prefix == (byte ) 0x09 ) return "ftps://" ;
334
- else if (prefix == (byte ) 0x0A ) return "sftp://" ;
335
- else if (prefix == (byte ) 0x0B ) return "smb://" ;
336
- else if (prefix == (byte ) 0x0C ) return "nfs://" ;
337
- else if (prefix == (byte ) 0x0D ) return "ftp://" ;
338
- else if (prefix == (byte ) 0x0E ) return "dav://" ;
339
- else if (prefix == (byte ) 0x0F ) return "news:" ;
340
- else if (prefix == (byte ) 0x10 ) return "telnet://" ;
341
- else if (prefix == (byte ) 0x11 ) return "imap:" ;
342
- else if (prefix == (byte ) 0x12 ) return "rtsp://" ;
343
- else if (prefix == (byte ) 0x13 ) return "urn:" ;
344
- else if (prefix == (byte ) 0x14 ) return "pop:" ;
345
- else if (prefix == (byte ) 0x15 ) return "sip:" ;
346
- else if (prefix == (byte ) 0x16 ) return "sips:" ;
347
- else if (prefix == (byte ) 0x17 ) return "tftp:" ;
348
- else if (prefix == (byte ) 0x18 ) return "btspp://" ;
349
- else if (prefix == (byte ) 0x19 ) return "btl2cap://" ;
350
- else if (prefix == (byte ) 0x1A ) return "btgoep://" ;
351
- else if (prefix == (byte ) 0x1B ) return "tcpobex://" ;
352
- else if (prefix == (byte ) 0x1C ) return "irdaobex://" ;
353
- else if (prefix == (byte ) 0x1D ) return "file://" ;
354
- else if (prefix == (byte ) 0x1E ) return "urn:epc:id:" ;
355
- else if (prefix == (byte ) 0x1F ) return "urn:epc:tag:" ;
356
- else if (prefix == (byte ) 0x20 ) return "urn:epc:pat:" ;
357
- else if (prefix == (byte ) 0x21 ) return "urn:epc:raw:" ;
358
- else if (prefix == (byte ) 0x22 ) return "urn:epc:" ;
359
- else if (prefix == (byte ) 0x23 ) return "urn:nfc:" ;
360
- return null ;
361
- }
362
-
363
-
364
- private final static char [] HEX = new char []{ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,'8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' };
365
-
366
- // convert bytes to a hex string
367
- private static String getHexString (final byte [] bytes ) {
368
- StringBuffer hex = new StringBuffer (bytes .length * 2 );
369
- for (int i = 0 ; i < bytes .length ; i ++) {
370
- for (int j = 1 ; j >= 0 ; j --) {
371
- hex .append (HEX [(bytes [i ] >> (j * 4 )) & 0xF ]);
372
- }
373
- }
374
- return hex .toString ();
128
+ ((TextView ) findViewById (R .id .tagdata )).setText (new String (((NdefMessage ) msgs [0 ]).getRecords ()[0 ].getPayload ()));
375
129
}
376
130
}
0 commit comments