public static extern UInt32 WriteNDEF(ref TNFCAddress Sender, ref TMessageInfo MessageInfo, ref byte NDEF, ref UInt32 NDEFSize, bool DontOverwrite, bool FormatCard, UInt32 TimeOut);
public static extern UInt32 ReadNDEF(ref TNFCAddress Sender, ref TMessageInfo MessageInfo, ref byte NDEF, ref UInt32 NDEFSize);
private void WriteNFCMessage() { // Validate NFCWrapper. if (NFCWrapper == null) { return; } UInt32 Result; //Create Smart Poster NDEF Message string URI = "http://www.anthonybaker.co"; string Comment = "Anthony Baker's Blog"; string Language = "en-US"; string TargetType = ""; UInt32 Size = 0; byte Action = 0; UInt32 NDEFSize = 1000; byte[] NDEF = new byte[NDEFSize]; Result = TNFCWrapper.CreateNDEFSp(URI, Comment, Language, ref Action, ref Size, TargetType, ref NDEF[0], ref NDEFSize); Debug.WriteLine("CreateNDEFSp: " + NFCWrapper.NFCWrapperErrorToString(Result)); if (Result != TNFCWrapper.ERR_SUCCESS) { return; } // print NDEF as Hex dump PrintHexDump(NDEF, NDEFSize); // convert NDEF into XML string XML = ""; Result = NFCWrapper.NDEF2XML(ref NDEF[0], NDEFSize, ref XML); Debug.WriteLine("NDEF2XML: " + NFCWrapper.NFCWrapperErrorToString(Result)); if (Result != TNFCWrapper.ERR_SUCCESS) { return; } // print NDEF as XML Debug.WriteLine(XML); Debug.WriteLine(" "); // now write the newly created NDEF message on an NFC tag Debug.WriteLine("Put an NFC Tag on the reader within the next 5 seconds ..."); TNFCAddress NFCAddress = new TNFCAddress(); TMessageInfo MessageInfo = new TMessageInfo(); Result = TNFCWrapper.WriteNDEF(ref NFCAddress, ref MessageInfo, ref NDEF[0], ref NDEFSize, false, true, 5); Debug.WriteLine("NDEF2XML: " + NFCWrapper.NFCWrapperErrorToString(Result)); if (Result == TNFCWrapper.ERR_SUCCESS) { //Display the message details string sAddress = ""; for (int i = 0; i < 12; i++) { sAddress = sAddress + NFCAddress.Address[i].ToString("X2") + " "; } Debug.WriteLine(" Receiver ID = " + sAddress); Debug.WriteLine(" Receiver Type = " + NFCWrapper.NFCTagTypeToString(MessageInfo.CardType)); Debug.WriteLine(" Device Name = " + MessageInfo.ReaderName); Debug.WriteLine(" Sent At = " + SystemTimeToString(MessageInfo.TimeStamp)); Debug.WriteLine(" NDEF Size = " + NDEFSize.ToString()); } else if (Result == TNFCWrapper.ERR_NDEF_TOO_LARGE) { // the NDEF message does not fit on the NFC tag Debug.WriteLine(" Max. NDEF Size = " + NDEFSize.ToString()); } Debug.WriteLine(""); }
public static extern UInt32 ClearNDEFQueue(ref TNFCAddress Sender);
/// <summary> /// Reads the NFC message data /// </summary> /// <returns></returns> private string ReadNDEF() { UInt32 DeviceCount = 0; UInt32 MessageCount = 0; UInt32 NextMessageSize = 0; UInt32 Result; // check to make sure the NCFWrappr is valid. if (NFCWrapper == null) { return(null); } // Get information about the message queue Result = TNFCWrapper.GetNDEFQueueInfo(ref DeviceCount, ref MessageCount, ref NextMessageSize); Debug.WriteLine("GetNDEFQueueInfo: " + NFCWrapper.NFCWrapperErrorToString(Result)); // Quit if the queue's information could not be retrieved if (Result != TNFCWrapper.ERR_SUCCESS) { return(null); } Debug.WriteLine(" DeviceCount = " + DeviceCount.ToString()); Debug.WriteLine(" MessageCount = " + MessageCount.ToString()); Debug.WriteLine(" NextMessageSize = " + NextMessageSize.ToString()); //Resize the NDEF buffer accordingly to the site of the next message in the queue byte[] NDEF = new byte[NextMessageSize]; UInt32 NDEFSize = NextMessageSize; TNFCAddress NFCAddress = new TNFCAddress(); TMessageInfo MessageInfo = new TMessageInfo(); //Read the NDEF message from the message queue Result = TNFCWrapper.ReadNDEF(ref NFCAddress, ref MessageInfo, ref NDEF[0], ref NDEFSize); Debug.WriteLine("ReadNDEF: " + NFCWrapper.NFCWrapperErrorToString(Result)); // Quit if the message queue reading was not successful. if (Result != TNFCWrapper.ERR_SUCCESS) { return(null); } //Display the message details string sAddress = ""; for (int i = 0; i < 12; i++) { sAddress = sAddress + NFCAddress.Address[i].ToString("X2") + " "; } Debug.WriteLine(" Sender ID = " + sAddress); Debug.WriteLine(" Sender Type = " + NFCWrapper.NFCTagTypeToString(MessageInfo.CardType)); Debug.WriteLine(" Device Name = " + MessageInfo.ReaderName); Debug.WriteLine(" Received At = " + SystemTimeToString(MessageInfo.TimeStamp)); Debug.WriteLine(" NDEF Size = " + NDEFSize.ToString()); Debug.WriteLine(" "); // print NDEF as Hex dump PrintHexDump(NDEF, NDEFSize); // convert NDEF into XML string XML = ""; Result = NFCWrapper.NDEF2XML(ref NDEF[0], NDEFSize, ref XML); Debug.WriteLine("NDEF2XML: " + NFCWrapper.NFCWrapperErrorToString(Result)); // Quit if the message could not be converted to XML if (Result != TNFCWrapper.ERR_SUCCESS) { return(null); } // print NDEF as XML Debug.WriteLine(XML); Debug.WriteLine(" "); // Parse XML Message XDocument xml = XDocument.Parse(XML); var typeElement = xml.Descendants("Type"); if (typeElement != null) { var type = typeElement.First().Value; XNamespace NDEF_Text = ""; string payload = "No NFC Message Data"; switch (type) { case "urn:nfc:wkt:U": //NDEF_Text = "urn:nfc:wkt:U"; //payload = xml.Descendants(NDEF_Text + "Text").First().Value; payload = xml.ToString(); return(payload); case "text/x-vCard": //NDEF_Text = "text/x-vCard"; //payload = xml.Descendants(NDEF_Text + "Text").First().Value; payload = xml.ToString(); return(payload); case "urn:nfc:wkt:T": //NDEF_Text = "urn:nfc:wkt:T"; //payload = xml.Descendants(NDEF_Text + "Text").First().Value; payload = xml.ToString(); return(payload); default: payload = xml.ToString(); return(payload); } } return(null); }