Search Unity

AndroidJavaObject as array for NFC access

Discussion in 'Android' started by jsr2k1, Dec 17, 2012.

  1. jsr2k1

    jsr2k1

    Joined:
    Aug 11, 2010
    Posts:
    118
    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: http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

    Code (csharp):
    1. Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    And this is my code in javascript:

    Code (csharp):
    1. var mActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic.<AndroidJavaObject>("currentActivity");
    2. var mIntent = mActivity.Call.<AndroidJavaObject>("getIntent");
    3. var sAction = mIntent.Call.<String>("getAction");
    4. if(sAction=="android.nfc.action.NDEF_DISCOVERED")
    5. {
    6.    var mNdefMessage = mIntent.Call.<AndroidJavaObject>("getParcelableArrayExtra", "android.nfc.extra.NDEF_MESSAGES");
    7. }
    How can I get an array from an AndroidJavaObject call? Any idea?

    Thanks in advance,

    Joel
     
  2. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    Have you tried using the array type directly?

    Code (csharp):
    1. var mNdefMessage = mIntent.Call.<AndroidJavaObject[]>("getParcelableArrayExtra", "android.nfc.extra.NDEF_MESSAGES");
     
  3. jsr2k1

    jsr2k1

    Joined:
    Aug 11, 2010
    Posts:
    118
    Thanks, finally I rewrite it in c# and it works!

    Code (csharp):
    1. if(sAction=="android.nfc.action.NDEF_DISCOVERED")
    2. {
    3.   AndroidJavaObject[] mNdefMessage = mIntent.Call<AndroidJavaObject[]>("getParcelableArrayExtra", "android.nfc.extra.NDEF_MESSAGES");
    4.   AndroidJavaObject[] mNdefRecord = mNdefMessage[0].Call<AndroidJavaObject[]>("getRecords");
    5.   byte[] payLoad = mNdefRecord[0].Call<byte[]>("getPayload");
    6.   string text = System.Text.Encoding.UTF8.GetString(payLoad);
    7. }
     
  4. mruce

    mruce

    Joined:
    Jul 23, 2012
    Posts:
    28
    Hi jsr2k1, could you share all the code for reading NFC tags? Should the code be in the Update loop?
    thx
     
  5. magicjokos

    magicjokos

    Joined:
    Feb 12, 2012
    Posts:
    3
    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
     
    Last edited: Oct 9, 2013
  6. gionta7

    gionta7

    Joined:
    Oct 30, 2013
    Posts:
    1
    Do you have found the solution?
    I have same problem as magicjokos ...
     
  7. druffzy

    druffzy

    Joined:
    Sep 19, 2009
    Posts:
    55
    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
     
  8. DP_S4y

    DP_S4y

    Joined:
    Jan 24, 2014
    Posts:
    8

    How did you get the sAction to be NDEF_DISCOVERED? Mine is always MAIN...
     
  9. vetkaashaas

    vetkaashaas

    Joined:
    Mar 21, 2014
    Posts:
    2
    I have the exact same problem, did you manage to figure anything out ?
     
  10. pablodigitoyx

    pablodigitoyx

    Joined:
    Sep 17, 2013
    Posts:
    2