I have been trying to read NFC tag from Unity3d with a very little knowledge of JAVA. But I am able to check weather NFC is enable in android or not.
Heres my code for Unity Side.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitySide : MonoBehaviour {
private AndroidJavaObject toastExample = null;
private AndroidJavaObject activityContext = null;
void Start() {
if(toastExample == null) {
using(AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}
using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.rsi.nfc.reader")) {
if(pluginClass != null) {
toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");
toastExample.Call("setContext", activityContext);
activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
toastExample.Call("showMessage", "This is a Toast message");
}));
}
}
}
}
public void ShowToastMessage(string msg)
{
using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.rsi.nfc.reader")) {
if(pluginClass != null) {
toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");
toastExample.Call("setContext", activityContext);
activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
toastExample.Call("showMessage", msg);
}));
}
}
}
public void ShowToastMessage2(string msg)
{
using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.rsi.nfc.reader")) {
if(pluginClass != null) {
toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");
toastExample.Call("setContext", activityContext);
activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
toastExample.Call("showMessage2", msg);
}));
}
}
}
}
This Is my Java Code which I make jar and import in Unity3d
package com.rsi.nfc.reader;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.Context;
import android.widget.Toast;
import android.nfc.NfcAdapter;
import android.os.Parcelable;
import android.nfc.tech.MifareUltralight;
public class MainActivity {
private Context context;
private NfcAdapter nfcAdapter;
public void setContext(Context context) {
this.context = context;
}
public void showMessage(String message) {
nfcAdapter = nfcAdapter.getDefaultAdapter(this.context) ;
if(nfcAdapter.isEnabled())
{
Toast.makeText(this.context,"Adapter is Enabled ", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this.context,"Adapter is not Enabled ", Toast.LENGTH_SHORT).show();
}
}
public void showMessage2(String message) {
Toast.makeText(this.context,message, Toast.LENGTH_SHORT).show();
}
protected void onNewIntent(Intent intent)
{
handleSendText(intent);
}
void handleSendText(Intent intent)
{
Log.v("VM","intent : " + intent);
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null)
{
Log.v("VM","Handling Text : " + sharedText);
// Update UI to reflect text being shared
UnityPlayer.UnitySendMessage("JavaCallback", "HandleText", sharedText);
}
}
}
Can anyone help me and tell me that how I will be going to read the TAG with intent.