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 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); } }
protected void UpdateDisplay(NdefLibrary.Ndef.NdefMessage messages) { Messages.Clear(); foreach (var message in messages) { Messages.Add(new MessageViewModel(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); } }
public MessageReceived(string tagId, NdefLibrary.Ndef.NdefMessage message, object sender) : base(sender) { TagId = tagId; Message = message; }
public Task<WriteResult> WriteTag(NdefMessage message) { return WriteTag(message, CancellationToken.None); }
public async Task<WriteResult> WriteTag(NdefMessage message, CancellationToken cancellationToken, TimeSpan timeout) { { if (!IsSupported) { if (_dontThrowExpceptionWhenNotSupported) { return null; } throw new NotSupportedException("This device does not support NFC (or perhaps it's disabled)"); } if (_taskCompletionSource != null) { //Mark it as cancelled if (!_taskCompletionSource.Task.IsCompleted) { _taskCompletionSource.TrySetCanceled(); } } _taskCompletionSource = new TaskCompletionSource<WriteResult>(); Task timeoutTask = null; if (timeout != default(TimeSpan)) { timeoutTask = Task.Delay(timeout); } using (cancellationToken.Register((s => ((TaskCompletionSource<WriteResult>)s).TrySetCanceled()), _taskCompletionSource)) { _messageToWrite = message; AttachEvents(); StartForegroundMonitoring(); if (timeoutTask != null) { await Task.WhenAny(timeoutTask, _taskCompletionSource.Task); if (timeoutTask.IsCompleted) { throw new TimeoutException("NFC message not recieved in time"); } } if (_taskCompletionSource.Task.IsCanceled) { StopForegroundDispatch(); DetachEvents(); return null; } var result = await _taskCompletionSource.Task; //We don't need to stop the foreground dispatch. Prior to the message been sent the application calls //OnPause which removes the foreground dispatch. By removing the events we prevent it from being added back. //StopForegroundDispatch(); DetachEvents(); return result; } } }
protected abstract void NewMessage(string tagID, NdefLibrary.Ndef.NdefMessage ndefMessage);
protected override void NewMessage(string tagId, NdefLibrary.Ndef.NdefMessage message) { var nfcMessage = new MessageReceived(tagId, message, this); _messenger.Publish(nfcMessage); }
public Task <WriteResult> WriteTag(NdefMessage message) { return(WriteTag(message, CancellationToken.None)); }
public async Task <WriteResult> WriteTag(NdefMessage message, CancellationToken cancellationToken, TimeSpan timeout) { { if (!IsSupported) { if (_dontThrowExpceptionWhenNotSupported) { return(null); } throw new NotSupportedException("This device does not support NFC (or perhaps it's disabled)"); } if (_taskCompletionSource != null) { //Mark it as cancelled if (!_taskCompletionSource.Task.IsCompleted) { _taskCompletionSource.TrySetCanceled(); } } _taskCompletionSource = new TaskCompletionSource <WriteResult>(); Task timeoutTask = null; if (timeout != default(TimeSpan)) { timeoutTask = Task.Delay(timeout); } using (cancellationToken.Register((s => ((TaskCompletionSource <WriteResult>)s).TrySetCanceled()), _taskCompletionSource)) { _messageToWrite = message; AttachEvents(); StartForegroundMonitoring(); if (timeoutTask != null) { await Task.WhenAny(timeoutTask, _taskCompletionSource.Task); if (timeoutTask.IsCompleted) { throw new TimeoutException("NFC message not recieved in time"); } } if (_taskCompletionSource.Task.IsCanceled) { StopForegroundDispatch(); DetachEvents(); return(null); } var result = await _taskCompletionSource.Task; //We don't need to stop the foreground dispatch. Prior to the message been sent the application calls //OnPause which removes the foreground dispatch. By removing the events we prevent it from being added back. //StopForegroundDispatch(); DetachEvents(); return(result); } } }
public Task <WriteResult> WriteTag(NdefLibrary.Ndef.NdefMessage message, System.Threading.CancellationToken cancellationToken, TimeSpan timeout) { throw new NotImplementedException(); }
public Task <WriteResult> WriteTag(NdefLibrary.Ndef.NdefMessage message, TimeSpan timeout) { throw new NotImplementedException(); }
public void WriteTag(NdefLibrary.Ndef.NdefMessage message) { if (droidTag == null) { throw new Exception("Tag Error: No Tag to write, register to NewTag event before calling WriteTag()"); } Ndef ndef = GetNdef(droidTag); if (ndef == null) { throw new Exception("Tag Error: NDEF not supported"); } try { ndef.Connect(); RaiseTagConnected(nfcTag); } catch { throw new Exception("Tag Error: No Tag nearby"); } if (!ndef.IsWritable) { ndef.Close(); throw new Exception("Tag Error: Tag is write locked"); } int size = message.ToByteArray().Length; if (ndef.MaxSize < size) { ndef.Close(); throw new Exception("Tag Error: Tag is too small"); } try { List <Android.Nfc.NdefRecord> records = new List <Android.Nfc.NdefRecord>(); for (int i = 0; i < message.Count; i++) { if (message[i].CheckIfValid()) { records.Add(new Android.Nfc.NdefRecord(Android.Nfc.NdefRecord.TnfWellKnown, message[i].Type, message[i].Id, message[i].Payload)); } else { throw new Exception("NDEFRecord number " + i + "is not valid"); } } ; Android.Nfc.NdefMessage msg = new Android.Nfc.NdefMessage(records.ToArray()); ndef.WriteNdefMessage(msg); } catch (TagLostException tle) { throw new Exception("Tag Lost Error: " + tle.Message); } catch (IOException ioe) { throw new Exception("Tag IO Error: " + ioe.ToString()); } catch (Android.Nfc.FormatException fe) { throw new Exception("Tag Format Error: " + fe.Message); } catch (Exception e) { throw new Exception("Tag Error: " + e.ToString()); } finally { ndef.Close(); RaiseTagTagDisconnected(nfcTag); } }
/// <summary> /// Publish a NDEF formatted message to another NFC device /// </summary> /// <param name="type"></param> /// <param name="message"></param> public void PublishNdefMessage(NdefLibrary.Ndef.NdefMessage message) { DeliverBinaryMessage("NDEF", message.ToByteArray()); }
/// <summary> /// Write a NDEF formatted message to a tag /// </summary> /// <param name="type"></param> /// <param name="message"></param> public void WriteNdefMessageToTag(NdefLibrary.Ndef.NdefMessage message) { DeliverBinaryMessage("NDEF:WriteTag", message.ToByteArray()); }