AndroidJavaObject as array for NFC access

Hello, I want to access to the information of a NFC tag. For now, I can access the tag, but I don’t know how to get the messages from the Intent because the return type is an array.

This is the example code from: NFC basics  |  Connectivity  |  Android Developers

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

And this is my code in javascript:

var mActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic.<AndroidJavaObject>("currentActivity");
var mIntent = mActivity.Call.<AndroidJavaObject>("getIntent");
var sAction = mIntent.Call.<String>("getAction");
if(sAction=="android.nfc.action.NDEF_DISCOVERED")
{
   var mNdefMessage = mIntent.Call.<AndroidJavaObject>("getParcelableArrayExtra", "android.nfc.extra.NDEF_MESSAGES");
}

How can I get an array from an AndroidJavaObject call? Any idea?

Thanks in advance,

Joel

Have you tried using the array type directly?

var mNdefMessage = mIntent.Call.<AndroidJavaObject[]>("getParcelableArrayExtra", "android.nfc.extra.NDEF_MESSAGES");

Thanks, finally I rewrite it in c# and it works!

if(sAction=="android.nfc.action.NDEF_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);
}

Hi jsr2k1, could you share all the code for reading NFC tags? Should the code be in the Update loop?
thx

Hi jsr2k1,

i’m trying tu use your code which looks great and usefull but my unity application never read any information from a NFC tag coul’d you please
share the android Manifest part and confirm that this code should read tag value at runtime ?

oh and my android device run a browser because i try to read an URL is that a problem for unity ?

Thanks

Do you have found the solution?
I have same problem as magicjokos …

One more thing to take care of is which tags did he buy and what android is he using. There maybe disparity in reading frequency. So jsr2k1 can you tell us what android device are you using ? and from where did you buy nfc tags?

Thanks

How did you get the sAction to be NDEF_DISCOVERED? Mine is always MAIN…

I have the exact same problem, did you manage to figure anything out ?

I have created a tutorial http://blog.twinsprite.com/2014/05/10/nfc-android-java-plugin-for-unity-3d.html about it. I hope it helps!