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 void Llcp_ConnectionChanged(object sender, LLCPLinkActivatedEventArgs e)
 {
     if (e.Connected)
     {
         lock (Pinning.SpiLock)
         {
             Console.WriteLine("LLCP open");
             LLCP llcp = (LLCP)sender;
             SnepServiceManager snepServiceManager = llcp.SnepServiceManager;
             if (snepServiceManager != null)
             {
                 Console.WriteLine("Snep service found");
                 NdefLibrary.Ndef.NdefMessage   message   = new NdefLibrary.Ndef.NdefMessage();
                 NdefLibrary.Ndef.NdefUriRecord uriRecord = new NdefLibrary.Ndef.NdefUriRecord();
                 uriRecord.Uri = "http://www.koolecontrols.nl";
                 //NdefLibrary.Ndef.NdefTextRecord textRecord = new NdefLibrary.Ndef.NdefTextRecord();
                 //textRecord.LanguageCode = "en";
                 //textRecord.Text = "Wat een geweldige test is dit";
                 message.Add(uriRecord);
                 byte[] ndef = message.ToByteArray();
                 snepServiceManager.SendNdefMessage(ndef);
             }
         }
     }
     else
     {
         Console.WriteLine("LLCP closed");
     }
 }
        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);
            }
        }