7 package com.google.appinventor.components.runtime.util;
9 import android.app.Activity;
10 import android.content.Intent;
11 import android.content.Context;
12 import android.nfc.NdefMessage;
13 import android.nfc.NdefRecord;
14 import android.nfc.NfcAdapter;
15 import android.nfc.Tag;
16 import android.nfc.tech.Ndef;
17 import android.nfc.tech.NdefFormatable;
18 import android.os.Parcelable;
19 import android.util.Log;
23 import java.net.CookieHandler;
24 import java.net.CookieManager;
25 import java.net.CookieStore;
27 import java.io.IOException;
28 import java.nio.charset.Charset;
29 import java.util.Locale;
44 return new CookieManager();
55 if (cookieHandler instanceof CookieManager) {
56 CookieManager cookieManager = (CookieManager) cookieHandler;
57 CookieStore cookieStore = cookieManager.getCookieStore();
58 if (cookieStore !=
null) {
59 cookieStore.removeAll();
70 return NfcAdapter.getDefaultAdapter(context);
73 public static void enableNFCWriteMode(Activity activity, NfcAdapter nfcAdapter, String textToWrite){
75 NdefMessage msg =
new NdefMessage(
new NdefRecord[] { textRecord });
76 nfcAdapter.enableForegroundNdefPush(activity, msg);
80 nfcAdapter.disableForegroundNdefPush(activity);
84 byte[] langBytes = Locale.getDefault().getLanguage().getBytes(Charset.forName(
"US-ASCII"));
85 Charset utfEncoding = encodeInUtf8 ? Charset.forName(
"UTF-8") : Charset.forName(
"UTF-16");
86 byte[] textBytes = payload.getBytes(utfEncoding);
87 int utfBit = encodeInUtf8 ? 0 : (1 << 7);
88 char status = (char) (utfBit + langBytes.length);
89 byte[] data =
new byte[1 + langBytes.length + textBytes.length];
90 data[0] = (byte) status;
91 System.arraycopy(langBytes, 0, data, 1, langBytes.length);
92 System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
93 NdefRecord record =
new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
94 NdefRecord.RTD_TEXT,
new byte[0], data);
99 String action = intent.getAction();
101 if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
111 Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
113 if (rawMsgs !=
null) {
114 msgs =
new NdefMessage[rawMsgs.length];
115 for (
int i = 0; i < rawMsgs.length; i++) {
116 msgs[i] = (NdefMessage) rawMsgs[i];
122 byte[] empty =
new byte[] {};
123 NdefRecord record =
new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
124 NdefMessage msg =
new NdefMessage(
new NdefRecord[] {record});
125 msgs =
new NdefMessage[] {msg};
127 byte[] payload = msgs[0].getRecords()[0].getPayload();
129 String message =
new String(payload).substring(3);
132 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
133 NdefMessage msg =
null;
136 msg =
new NdefMessage(
new NdefRecord[] { textRecord });
143 Log.e(
"nearfield",
"Unknown intent " + intent);
151 int size = message.toByteArray().length;
153 Ndef ndef = Ndef.get(tag);
156 if (!ndef.isWritable()) {
159 if (ndef.getMaxSize() < size) {
162 ndef.writeNdefMessage(message);
165 NdefFormatable format = NdefFormatable.get(tag);
166 if (format !=
null) {
169 format.format(message);
171 }
catch (IOException e) {
178 }
catch (Exception e) {