- Home /
Read NFC tag from Unity3d android
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.
Answer by Gabrio · Sep 24, 2017 at 01:45 PM
Hi, here is what I use to read the message from the tag.
if (!tagFound) {
try {
// Create new NFC Android object
mActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
mIntent = mActivity.Call<AndroidJavaObject>("getIntent");
sAction = mIntent.Call<String>("getAction");
if (sAction == "android.nfc.action.NDEF_DISCOVERED") {
Debug.Log("Tag of type NDEF");
}
else if (sAction == "android.nfc.action.TECH_DISCOVERED") {
Debug.Log("TAG DISCOVERED");
AndroidJavaObject[] mNdefMessage = mIntent.Call<AndroidJavaObject[]>("getParcelableArrayExtra", "android.nfc.extra.NDEF_MESSAGES");
AndroidJavaObject[] mNdefRecord = mNdefMessage[0].Call<AndroidJavaObject[]>("getRecords");
byte[] payLoad = mNdefRecord[0].Call<byte[]>("getPayload");
string text = System.Text.Encoding.UTF8.GetString(payLoad);
Debug.Log("******** " + text.Substring(3));
/*
// Get ID of tag
AndroidJavaObject mNdefMessage = mIntent.Call<AndroidJavaObject>("getParcelableExtra", "android.nfc.extra.TAG");
if (mNdefMessage != null) {
byte[] payLoad = mNdefMessage.Call<byte[]>("getId");
string text = System.Convert.ToBase64String(payLoad);
tag_output_text.text = text;
tagID = text;
InterfaceMgr.instance.TAGTrovato(tagID);
}
else {
tag_output_text.text = "No ID found !";
}
*/
tagFound = true;
Invoke("DeselectNFC",2);
return;
}
else if (sAction == "android.nfc.action.TAG_DISCOVERED") {
Debug.Log("This type of tag is not supported !");
}
else {
tag_output_text.text = "No tag...";
return;
}
}
catch (Exception ex) {
Debug.Log(ex.Message);
}
}
void DeselectNFC(){
mIntent.Call("removeExtra", "android.nfc.extra.TAG");
mIntent.Call("removeExtra", "android.nfc.extra.NDEF_MESSAGES");
tagFound = false;
}
When I use two android phones and both of them are turned on it dose not read the tag but it opens the app in the other phone. But if one of the phones is on sleep than the tag is being read properly. Any idea how to solve this issue?
Your answer
Follow this Question
Related Questions
Calling static jar function from Unity3D 0 Answers
Android manifest does not merge with generated manifest. 0 Answers
Launch main Unity activity in response to Android ACTION_BOOT_COMPLETED? 0 Answers
How to handle an intent from another Android App in Unity? 1 Answer
Android plugin & Activity help needed - "GetMethodID method not found" problems 0 Answers