1

I've been trying to find a way to instantiate the Tag object. I'm trying to do this so that i can simulate various types of Tags.

Does anyone know how to get an instance of UltraLight object using

Tag tag = ?????
UltraLight obj = UltraLight.get(tag);

Thank you

gaara87
  • 2,087
  • 3
  • 21
  • 43

1 Answers1

2

In the NFC app example for ICS source code, we can find some code such as:

Bundle extras = new Bundle();
extras.putParcelable(Ndef.EXTRA_NDEF_MSG, ndefMsg);
extras.putInt(Ndef.EXTRA_NDEF_MAXLENGTH, 0);
extras.putInt(Ndef.EXTRA_NDEF_CARDSTATE, Ndef.NDEF_MODE_READ_ONLY);
extras.putInt(Ndef.EXTRA_NDEF_TYPE, Ndef.TYPE_OTHER);
Tag tag = Tag.createMockTag(new byte[] { 0x00 },
    new int[] { TagTechnology.NDEF },
    new Bundle[] { extras });

Code for this createMockTag method is following:

public static Tag createMockTag(byte[] id, int[] techList, Bundle[] techListExtras) {
    // set serviceHandle to 0 to indicate mock tag
    return new Tag(id, techList, techListExtras, 0, null);
}

Maybe this could help.

Romain R.
  • 920
  • 12
  • 21
  • Thanks,.I'm gonna try it and see...from the top of my mind, i cant understand how it might work cuz there is no constructor like that for the Tag class right? – gaara87 Feb 25 '12 at 10:27
  • I did check out the source code. The function cannot be called! :/ – gaara87 Feb 27 '12 at 06:37
  • You're right. Maybe another idea: when using the Tag class, we can use its constant named [CREATOR](http://developer.android.com/reference/android/nfc/Tag.html#CREATOR). This one returns an object [Creator](http://developer.android.com/reference/android/os/Parcelable.Creator.html). Such an object has a method [createFromParcel(Parcel)](http://developer.android.com/reference/android/os/Parcelable.Creator.html#createFromParcel%28android.os.Parcel%29) which can help. – Romain R. Feb 27 '12 at 08:19
  • I tried that out also, since that is the only option that is left, but i dont think i'm sending in the right values for the Tag object to get successfully created. Just a pointer, i couldn't find anything called Ndef.EXTRA_NDEF_CARDSTATE or any of those other Ndef.* constants that you had used. – gaara87 Feb 27 '12 at 10:52