locked
PublishBinaryMessage() not working for a valid NDEF record with NDEF:WriteTag option.

    Question

  • Hi,
    I am writing a metro app that writes a NDEF record to a static tag. 
    I need to write a NDEF:Unknown and NDEF:ext type.

    I understand that I need to use "NDEF:WriteTag" to publish my message to a static tag.

    How do I write the data as? Below is my code snippet for "NDEF Uri" which also doesnt work.The data that I have given in byteArr is a valid NDEF record in hex format. Please note that I understand that "WindowsUri:Writetag" will actually write as a NDEF record. But i want to use NDEF:WriteTag only to write a NDEF record. When I could write this data to a tag, i will extend my app to NDEF:wkt, NDEF:Unknown, NDEF:ext.

    Code:

    var byteArray = (0x03, 0x0e, 0xd1, 0x01,0x0a,0x55, 0x03,0x6e,0x6f,0x6b,0x69, 0x61, 0x2e, 0x63,0x6f,0x6d, 0xfe); //valid NDEF record for NDEF:Uri.
    
    dataWriter.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
    dataWriter.writeBytes(byteArray);
    
    selectedMsgType = "NDEF:WriteTag";
    
    publishedMessageId = proximityDevice.publishBinaryMessage(selectedMsgType, dataWriter.detachBuffer(), WrittentoTag);
    

    Kindly help me with a correct way of using this PublishBinaryMessage API for NDEF:WriteTag.

    Thanks.

    Thursday, August 23, 2012 1:59 PM

Answers

  • As David mentioned, please describe what the problem you are seeing is. You've said it doesn't work, but you haven't described what is not working or how you know it isn't working. The code you've included above appears to construct the array incorrectly. The documentation for WriteBytes suggests you pass a Uint8Array object.

    I would also note that WindowsUri does not use NDEF:Uri. It uses NDEF:wkt.U (corresponding to 'urn:nfc:wkt:U'). In order to ensure your NDEF content is correct, I would recommend writing the content using WindowsUri, then reading that content by subscribing to NDEF.

    -Mike [MSFT] 

    • Marked as answer by IMetroUI Monday, August 27, 2012 9:46 AM
    Saturday, August 25, 2012 1:26 AM

All replies

  • Are you receiving any error?

    I'm looking to locate someone to speak to this for you.

    Thanks!


    David Lamb

    Saturday, August 25, 2012 12:44 AM
    Moderator
  • As David mentioned, please describe what the problem you are seeing is. You've said it doesn't work, but you haven't described what is not working or how you know it isn't working. The code you've included above appears to construct the array incorrectly. The documentation for WriteBytes suggests you pass a Uint8Array object.

    I would also note that WindowsUri does not use NDEF:Uri. It uses NDEF:wkt.U (corresponding to 'urn:nfc:wkt:U'). In order to ensure your NDEF content is correct, I would recommend writing the content using WindowsUri, then reading that content by subscribing to NDEF.

    -Mike [MSFT] 

    • Marked as answer by IMetroUI Monday, August 27, 2012 9:46 AM
    Saturday, August 25, 2012 1:26 AM
  • Hi David/Mike,

    Here are the details about my problem.

    First I wrote a Metro app to read from tags. I use some other application to write to tags in different formats and read through my app. All the options like, WindowsUri, WindowsMime, NDEF:ext, NDEF:MIME, NDEF:URI, NDEF:wkt work fine when reading is concerned.

    I extended my app to write to the tag using "Windowsxxx:WriteTag". All of them like, Windows:WriteTag, WindowsUri:WriteTag, WindowsMime:WriteTag work fine i.e I could write to the tag and read from it through my App. I pass a string (containing the data that I want to write to) to the PublishBinaryMessage API.  I just pass "hello string for WindowsMIME type. the datawriter buffer holds  "hello" only. Message is published.

    I want to extend it further to enable my app to write NDEF records. My issue here is: How should I pass the data to PublishBinaryMessage API. i.e what is the datatype of the data that datawriter's buffer should have, say for a message "hello" similar to WindowsMIME as given above.

    1) is it a string? or a byte array.

    2)If it is a byte array, should it hold the binary value or a hex value.

    3) Is it sufficient if i pass only the hex value of the data? If so, how will the type been identified?

    No matter how I try, my app crashes at PublishBinaryMessage API call with "Parameter is invalid". i.e Message is not published any way. 

    Please let me know. I hope I have explained my problem clearly. 

    Thanks.

    Monday, August 27, 2012 12:27 PM
  • It sounds like the question you have here is how to use DataWriter to write binary data in JavaScript? NDEF will definitely require writing binary data using an array of bytes. The sample code you provided seems to attempt to do this, though it is not using the appropriate byte array type. The DataWriter documentation recommends using Uint8Array as your array type. There's some info on Uint8Array here: http://msdn.microsoft.com/en-us/library/br212477(v=vs.94)

    Another option, though likely less performant, is to simply use DataWriter.WriteByte to write each byte rather than passing an array.

    -Mike [MSFT]

    • Proposed as answer by Mike L [MSFT] Tuesday, September 4, 2012 10:10 PM
    Monday, August 27, 2012 5:28 PM
  • Hi Mike,

    My app crashes when I call PublishBinaryMessage() with "Parameter is incorrect" So I am guessing that the data that I am passing is wrong. Though the code snippet does not have the UInt8Array value for dataWriter.writeBytes(), I did pass a correct Uint8Array. Here is the code where I publish. Please correct me if I am wrong.

     

    //Data that I want to write to.

    var data = "test data"; var ab = new ArrayBuffer(data.length); var uint8 = new Uint8Array(ab); for (var i = 0; i < data.length; i++) { uint8[i] = data.charCodeAt(i); } dataWriter.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8; dataWriter.writeBytes(uint8); var selectedMsgType = "NDEF:WriteTag"; //Is this message type correct??

    publishedMessageId = proximityDevice.publishBinaryMessage(selectedMsgType, dataWriter.detachBuffer(), WrittentoTag);

    I did the same thing in C# as well. here is the snippet.

                    DataWriter dataWriter = new DataWriter();
                    dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    String mystring = "Testdata"; 
                    System.Text.UTF8Encoding  encoding = new System.Text.UTF8Encoding();
                    byte[] byteArray = encoding.GetBytes(mystring);
                    dataWriter.WriteBytes(byteArray);
    
                    _proximity.PublishBinaryMessage("NDEF:WriteTag", dataWriter.DetachBuffer(), MessagePublished);

    So what is the way to use "NDEF:WriteTag" option in PublishBinaryMessage() to write a NDEF record. Kindly help. Documentation does not give any information on this.

    Thanks.

    Monday, September 3, 2012 12:28 PM
  • It appears you are using an appropriate way of creating the Uint8Array and writing that to the DataWriter. However, the data in your sample code is not NDEF. Your code snippet shows you writing 'var data = "test data";', which is clearly not NDEF.

    I believe all you should have left remaining to get this working is using actual NDEF binary data.

    -Mike [MSFT]

    • Proposed as answer by Mike L [MSFT] Tuesday, September 4, 2012 10:10 PM
    Tuesday, September 4, 2012 10:10 PM
  • Thanks Mike,

    After some struggle, I managed to get a proper NDEF record... from the output research that I have done so far and from the output, I think my problem was that I wasn't able to get a properly formatted NDEF record. An NDEF Tech Specification document NFC Forum site, helped in getting my stuff work.

    Wednesday, September 5, 2012 12:25 PM
  • Glad to hear you got it working.

    -Mike [MSFT]

    Wednesday, September 5, 2012 3:34 PM
  • I've just released an NDEF library for the Windows Proximity APIs - it helps to create the byte arrays for NDEF messages, as well as parsing the arrays back to individual NDEF record and extracting their information.

    Based on C# for Windows 8 platform apps. But even if you're using JavaScript, it's still easier to port to JavaScript compared to writing everything from scratch based on the specs :)

    http://ndef.codeplex.com/

    Wednesday, September 12, 2012 7:37 AM
  • Glad to hear it. I will be sure to suggest people check this out if they are working with NDEF.

    -Mike [MSFT]

    Saturday, September 15, 2012 7:44 PM