Reading a Mifare tag using Windows Phone 8 NFC?
Asked Answered
M

4

15

Does Windows Phone 8 NFC support Mifare Ultralight/Classic based tags? I use this code to access NFC device on Nokia Lumia 920 (code example was taken from NDEF Tag Reader – NFC NDEF Tag Reader)

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        ProximityDevice device = ProximityDevice.GetDefault();
        device.DeviceArrived += DeviceArrived;
        device.DeviceDeparted += DeviceDeparted;
        device.SubscribeForMessage("NDEF", MessageReceived);
    }

    private void DeviceArrived(ProximityDevice sender)
    {
        // this event occurs when I am tapping any of my tags (tried 5 different Mifare Ultralight/Classic)
    }

    private void DeviceDeparted(ProximityDevice sender)
    {
        // this event occurs when I am moving away any tag
    }

    private void MessageReceived(ProximityDevice sender, ProximityMessage message)
    {
        // this event is never fired!!! :(
    }
}

Is NFC on WP8 defective or is this code wrong?

Update: From this document NFC Forum Type Tags you can find that Mifare Ultralight is compatible with NDEF. Android devices can read tags of this type easily.

Maul answered 3/11, 2012 at 8:24 Comment(1)
Please taka a look at my topic: #21240036Actress
F
14

Mifrare is supported on WP8 and on the Lumia 920. I'm guessing here, but it's likely your Mifare NFC tag isn't formatted/initialized to NDEF. You can ask your NFC tags to be NDEF formatted when you buy NFC tags.

The Lumia 920 chip (NXP PN544 family) supports the following tag types (at least):

  • Type 1: Topaz family
  • Type 2: Mifare Ultralight family, my-d-move, NTag
  • Type 3: Felica family
  • Type 4: Desfire family
  • Non standardized: Mifare Standard

Regarding NFC tags NDEF formating:

  • WP8 only supports NDEF level access to these tags, which means that the tag needs to be NDEF formatted or have an exsiting NDEF message to it (can be an empty one). If you try to use the APIs on a non-formatted NFC tags they won’t work (as WP8 lacks support for low level Tag Type specific commands/access)
  • If you want to NDEF format your tags you have the following options: when ordering tags request them to be NDEF formatted (or/and contain an empty NDEF message and the tag to be unlocked), use an NFC USB Reader/Writer HW for PC or use an Symbian/MeeGo/Android NFC device with an NFC writing app

Sincerely,
-- Justin Angel
Principal Engineer for Windows Phone Developer Experience at Nokia

Forseti answered 5/11, 2012 at 7:49 Comment(5)
The tags I’m using are already written and cannot be rewritten. Anyway thank you for the reply!Maul
To add to Justin's great response, here is another good post for follow-up dotnet-redzone.blogspot.com/2012/12/…Borodino
@Forseti Will the NFC SDK be extended in the future to allow low level access? Maybe in the second WP8 update? In its current form there is no real opportunity to use WP8 in a business setting with NFC tags.Imperturbation
@Forseti does this mean the encryption/decryption stuff won't work?Fastidious
Could you please take a look at my topic about NFC PLEASE : ( : #21240036Actress
E
5

The code you posted is supposed to read NDEF messages from a NFC tag.

Whereas Mifare is also used with NFC tags, that's where the similarity ends: it's a completely different protocol with its own (proprietary) data format.

So, this code isn't really wrong (nor is NFC on WP8 generally 'defective'), but if you expect it to read Mifare tags, this approach won't work for you.

I don't know if it's possible to read Mifare tags with WP8: this depends on the hardware (as Mifare uses some non-ISO frames) as well as the API support. A quick Google search suggests that the Java SDK for older Nokia phones does support Mifare, so the hardware support may be there. Didn't find anything for WP8, though, so this will most likely require some extensive coding, if it works at all.

To give you some idea of what's needed: after you get your DeviceArrived event (which means that the reader detected an ISO NFC tag), you need to obtain the UID of the card. This should be pretty easy, as that's standard ISO functionality.

Next, you need the ability to directly send Mifare authentication and read/write sector commands to the tag. Since these commands aren't ISO-standard, this is where things get more tricky and reader-dependent. Getting past this stage really required protocol documentation and a working Mifare test tool for your phone. Finally, most Mifare cards are completely unreadable unless you at least know one sector key, and the application data format is proprietary (specific to the card issuer) as well, so even after all that work, it's not guaranteed you can get useful information off the card...

Egalitarian answered 3/11, 2012 at 9:20 Comment(1)
Thank you for the reply. From this doc nfc-forum.org/resources/white_papers/… you can find that Mifare Ultralight is compatible with NDEF and is supported by NFC Forum. Android devices can read tags of this type easily. Your answer concerns only Mifare Classic and not very helpful, there is no low-level NFC API on WP8.Maul
U
3

Windows Phone 8 Apps only have access to the very high level libraries, and cannot read tags that are not NDEF formatted.

MIFARE UL tags must use the NFC Forum NDEF Type 2 standard, which is simplistic due to the small 48 byte user memory of the tag.

If you have a low level reader/writer, you can make your UL tag NDEF compliant without modifying the majority of your data, but you will need to sacrifice:

  • all 4 bytes of OTP memory at page 3 (for the NDEF Capability Container)
  • the first 6 bytes of user programmable memory (6 at the very minimum, might want to pad to 8 to cleanly fill first two pages).

The tag is formatted like so: (reference - www.nfc-forum.org/specs/spec_list/#tagtypes)

  • PAGE 3, BYTES 0-3: CC [MAGIC NUMBER, VERSION, USER MEM SIZE, READ/WRITE]
  • PAGE 4, BYTES 0-1: NDEF MESSAGE TLV HEADER [Type, Length] (Recommend type ExternalRtd)
  • PAGE 4, BYTES 2-3: RECORD DESCRIPTOR (Includes a string that specifies record type, suggest making it 1 byte long to save space, or 3 bytes long for neat padding)
  • PAGE 5, BYTES 0-1 or 3: RECORD DESCRIPTOR
  • REST OF PAGES/BYTES ARE RECORD/DATA

EXAMPLE: Tag has message type ExternalRtd, and record type "abc" (record type should technically be of the format "urn:nfc:ext:companyname.com:typename" to be fully NDEF compliant, but we can't afford to use that much space)

[Page No., Byte No.] , Value , Comment

  • [3, 0] , 0xE1 , The magic number
  • [3, 1] , 0x10 , the NDEF Version number, major version 1, minor version 0.
  • [3, 2] , 0x06 , the user memory size of the tag / 8. In this case 6, since 6*8=48
  • [3, 3] , 0x00 , read/write allowed. 0x00 = write allowed, 0x0F = read only
  • [4, 0] , 0x03 , The T in the TLV, the type. In this case, an NDEF Message.
  • [4, 1] , 0x2E , The L in the TLV, the Length. In this case, the rest of the tag, 46 bytes.
  • [4, 2] , 0xD4 , Record MB_ME_CF_SR_IL_TNF. In this case, 11010100 (Is the first record, is the last record, is not a chunk, is a short message, no id in header, is of type "External Type")
  • [4, 3] , 0x03 , Record Type Length. In this case 3 (Type "abc" is 3 long)
  • [5, 0] , 0x28 , Payload Length, the rest of the tag. In this case 40.
  • [5, 1] , 0x61 , First byte of type, 0x61 = 97 = 'a'
  • [5, 2] , 0x61 , Second byte of type, 0x62 = 98 = 'b'
  • [5, 3] , 0x61 , Third byte of type, 0x63 = 99 = 'c'

  • The remaining 40 bytes of the tag, pages 6 to 11, are your payload.

If you just want to Initialise the tag so that the phone can read it and do the rest, just write the CC, and only the TLV with a L of zero and no V. (4,0 = 0x03 and 4,1 =0x00).

Ulster answered 10/4, 2013 at 13:41 Comment(0)
I
1

I had a similar problem, I bought pack of NFC tags on eBay, but they were not NDEF formatted. WP8 can utilize only NDEF formatted tags, so I had to find a way how to format them. I found on Twitter a guy with NFC equipped Android phone and he formatted these tags for me. Too bad that Windows Phone 8 cannot do this as well.

You can vote here on official WPdev Uservoice page for the low-level NFC API feature:
Provide low-level NFC API for NDEF formatting and (un)locking tags

Iowa answered 26/1, 2013 at 8:11 Comment(1)
If I use `SubscribeForMessage("Windows.something", handler); to subscribe every binary data, can I read it? msdn.microsoft.com/en-us/library/windowsphone/develop/…Comprador

© 2022 - 2024 — McMap. All rights reserved.