6 package com.google.appinventor.components.runtime;
8 import android.app.Activity;
9 import android.content.Intent;
10 import android.nfc.NfcAdapter;
11 import android.util.Log;
35 @DesignerComponent(version = YaVersion.NEARFIELD_COMPONENT_VERSION,
36 description =
"<p>Non-visible component to provide NFC capabilities. " +
37 "For now this component supports the reading and writing of text tags only " +
38 "(if supported by the device)</p>" +
39 "<p>In order to read and write text tags, the component must have its " +
40 "<code>ReadMode</code> property set to True or False respectively.</p>" +
41 "<p><strong>Note:</strong> This component will only work on Screen1 of" +
42 " any App Inventor app.</p>",
43 category = ComponentCategory.SENSORS,
45 iconName =
"images/nearfield.png")
48 @UsesPermissions(permissionNames =
"android.permission.NFC")
51 private static final String TAG =
"nearfield";
52 private Activity activity;
54 private NfcAdapter nfcAdapter;
55 private boolean readMode =
true;
56 private int writeType;
57 private String tagContent =
"";
58 private String textToWrite =
"";
69 super(container.
$form());
77 form.registerForOnResume(
this);
78 form.registerForOnNewIntent(
this);
79 form.registerForOnPause(
this);
80 Log.d(TAG,
"Nearfield component created");
94 Log.d(TAG,
"Tag read: got message " + message);
104 Log.d(TAG,
"Tag written: " + textToWrite);
114 public String LastMessage() {
115 Log.d(TAG,
"String message method stared");
123 defaultValue =
"True")
126 public
boolean ReadMode() {
127 Log.d(TAG,
"boolean method stared");
135 public String TextToWrite() {
136 Log.d(TAG,
"String message method stared");
145 public
int WriteType() {
154 description =
"Specifies whether the NFC hardware should operate in read or write mode.")
155 public
void ReadMode(
boolean newMode) {
156 Log.d(TAG,
"Read mode set to" + newMode);
168 description =
"Specifies the content that will be written to the tag when in write mode. "
169 +
"This method has no effect if ReadMode is true.")
170 public
void TextToWrite(String newText) {
171 Log.d(TAG,
"Text to write set to" + newText);
172 textToWrite = newText;
173 if(!readMode && writeType == 1){
184 Log.d(TAG,
"Nearfield on onNewIntent. Intent is: " + intent);
185 resolveIntent(intent);
191 Intent intent = activity.getIntent();
192 Log.d(TAG,
"Nearfield on onResume. Intent is: " + intent);
195 void resolveIntent(Intent intent) {
196 Log.d(TAG,
"resolve intent. Intent is: " + intent);
207 Log.d(TAG,
"OnPause method started.");
208 if (nfcAdapter !=
null) {