Help with java code extension nfc

Hello
I have this problem

here is the code

this is my nfc extension and i have changed only the enabled function from string to boolean and this happend… please help

package com.matanel.NFCtools;

import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.util.GingerbreadUtil;
import com.google.appinventor.components.runtime.util.SdkLevel;

import java.nio.charset.StandardCharsets;


@DesignerComponent(
		version = 1,
		description = ("This extension has 2 tools, check if NFC exists and check if enabled."),
		category = ComponentCategory.EXTENSION,
		nonVisible = true,
		helpUrl = "https://community.kodular.io/t/nfc-tools-my-third-extension/87894",
		iconName = "https://res.cloudinary.com/mydiaryapp/image/upload/c_scale,h_16,w_16/v1602419125/nfc-icon-31_wmlwd3.png")

@SimpleObject(external = true)
public class NFCtools extends AndroidNonvisibleComponent implements OnResumeListener, OnNewIntentListener {
  private static final String TAG = "nearfield";
  private Activity activity;

  private NfcAdapter nfcAdapter;
  private boolean readMode = true;
  private int writeType;
  private String tagContent = "";
  private String textToWrite = "";

  /* Used to identify the call to startActivityForResult. Will be passed back into the
  resultReturned() callback method. */
  protected int requestCode;

	  
	public NFCtools(ComponentContainer container) {
		super(container.$form());
    activity = container.$context();
    writeType = 1;
    nfcAdapter = (SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD)
        ? GingerbreadUtil.newNfcAdapter(activity)
        : null;
    // register with the forms to that OnResume and OnNewIntent
    // messages get sent to this component
    form.registerForOnResume(this);
    form.registerForOnNewIntent(this);
    form.registerForOnPause(this);
    Log.d(TAG, "Nearfield component created");
	}
		
	 @SimpleFunction(description = "Checks if the NFC sensor is enabled. Return true or false.")
	public boolean Enabled() { 
		NfcManager manager = (NfcManager) activity.getSystemService(Activity.NFC_SERVICE);
		NfcAdapter adapter = manager.getDefaultAdapter();
		if(adapter != null) /* NFC exists - yes*/ {
			 if (adapter.isEnabled()) {
			return true; //NFC sensor is enabled.
			}
			else
			{
			return false; //NFC sensor is not enabled.
			}
		}
	
	}
	
	@SimpleFunction(description = "Check if exists")
	public boolean Exists() {
		NfcManager manager = (NfcManager) activity.getSystemService(Activity.NFC_SERVICE);
		NfcAdapter adapter = manager.getDefaultAdapter();
		
		if(adapter != null) 
			{
			return true;
			}
			
		else 
			{
			return false;
			}
	}
	
	@SimpleEvent()
  public void TagRead(String message) {
    Log.d(TAG, "Tag read: got message " + message);
    //tagContent = message;
    EventDispatcher.dispatchEvent(this, "TagRead", tagContent);
  }
	
	@SimpleProperty(category = PropertyCategory.BEHAVIOR)
	public String LastMessage() {
    Log.d(TAG, "String message method stared");
    return tagContent;
  }
  
	@Override
  public void onNewIntent(Intent intent) {
    Log.d(TAG, "Nearfield on onNewIntent.  Intent is: " + intent);
    resolveIntent(intent);
  }
	
	@Override
  public void onResume() {
    Intent intent = activity.getIntent();
    Log.d(TAG, "Nearfield on onResume.  Intent is: " + intent);
  }
	
	void resolveIntent(Intent intent) {
    Log.d(TAG, "resolve intent. Intent is: " + intent);
    // Parse the intent
    if(SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD){
      // Is the intent for a new NFC Tag Discovery
      if (intent != null && intent.getAction() == NfcAdapter.ACTION_TECH_DISCOVERED) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String s = new String(tag.getId(), StandardCharsets.UTF_8);
        tagContent = s;
      }
    }
  }
  
	}

here the file
NFCtools.java (4.7 ק״ב)

Try to import this class.

import android.nfc.NfcAdapter;

When are we getting fb

already has that in the code…

what?? …/

image

The class you are trying to access is NfcManager which is not defined in your code. Try adding a corresponding import for it.

For more assistance with extension development… ask questions related to it in MIT Community.

1 Like

Yes, I probably messed up…