private void ClearTagData() { if (_tag == null) { return; } Logger.Log(" ClearTagData"); try { Logger.Log(" Formatting"); _tag.NDEFFormat(); } catch (Exception ex) { Logger.Log(" " + ex.Message); } var message = new NdefLibrary.Ndef.NdefMessage(); for (int i = 0; i < 5; i++) { var record = new NdefLibrary.Ndef.NdefRecord(); record.Payload = new byte[0]; message.Add(record); } try { _tag.NDEFWrite(message); } catch (Exception ex) { Logger.Log(" ClearTagData: Error writing to tag: " + ex.Message); } }
private bool SaveTagData() { var message = new NdefLibrary.Ndef.NdefMessage(); var nameRecord = new NdefLibrary.Ndef.NdefRecord(NdefLibrary.Ndef.NdefRecord.TypeNameFormatType.Unknown, new byte[0]);//.NdefTextRecord(); nameRecord.Payload = Encoding.ASCII.GetBytes(DeviceName); message.Add(nameRecord); var ipRecord = new NdefLibrary.Ndef.NdefRecord(NdefLibrary.Ndef.NdefRecord.TypeNameFormatType.Unknown, new byte[0]); ipRecord.Payload = _encryptData ? EncDec.Protect(Encoding.ASCII.GetBytes(ipAddress)) : Encoding.ASCII.GetBytes(ipAddress); message.Add(ipRecord); var userNameRecord = new NdefLibrary.Ndef.NdefRecord(NdefLibrary.Ndef.NdefRecord.TypeNameFormatType.Unknown, new byte[0]); userNameRecord.Payload = _encryptData ? EncDec.Protect(Encoding.ASCII.GetBytes(username)) : Encoding.ASCII.GetBytes(username); message.Add(userNameRecord); var passwordRecord = new NdefLibrary.Ndef.NdefRecord(NdefLibrary.Ndef.NdefRecord.TypeNameFormatType.Unknown, new byte[0]); passwordRecord.Payload = _encryptData ? EncDec.Protect(Encoding.ASCII.GetBytes(password)) : Encoding.ASCII.GetBytes(password); message.Add(passwordRecord); // HACKHACK: for some reason the last record seems to get corrupted, so just write some garbage data var tempRecord = new NdefLibrary.Ndef.NdefRecord(NdefLibrary.Ndef.NdefRecord.TypeNameFormatType.Unknown, new byte[0]); tempRecord.Payload = Encoding.ASCII.GetBytes("xxxXXXXxxx"); message.Add(tempRecord); Logger.Log(" Writing: " + ToString()); try { _tag.NDEFWrite(message); Logger.Log(" Wrote Data"); return(Load()); } catch (Exception ex) { Logger.Log(" InitializeTag: Possible error writing data to tag: " + ex.Message); return(false); } }