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");
     }
 }
Esempio n. 2
0
        protected override async void NewIntent(Cirrious.CrossCore.Core.MvxValueEventArgs <Intent> e)
        {
            WriteResult writeResult = new WriteResult();

            writeResult.ReasonForFailure = FailureReasons.Unkown;


            var id     = GetIdFromTag(e.Value);
            var intent = e.Value;
            Tag tag    = (Tag)intent.GetParcelableExtra(NfcAdapter.ExtraTag);

            Ndef ndef = Ndef.Get(tag);

            if (ndef != null)
            {
                ndef.Connect();
                if (!ndef.IsWritable)
                {
                    writeResult.ReasonForFailure = FailureReasons.TagReadOnly;
                    _taskCompletionSource.TrySetResult(writeResult);
                    return;
                }
                byte[] message = _messageToWrite.ToByteArray();
                if (ndef.MaxSize < message.Length)
                {
                    writeResult.ReasonForFailure = FailureReasons.TagTooSmall;
                    _taskCompletionSource.TrySetResult(writeResult);
                    return;
                }

                try
                {
                    await ndef.WriteNdefMessageAsync(new Android.Nfc.NdefMessage(message));

                    writeResult.ReasonForFailure = FailureReasons.DidNotFail;
                    writeResult.NFCTag           = new NFCTag()
                    {
                        Id = id
                    };
                    _taskCompletionSource.TrySetResult(writeResult);
                    return;
                }
                catch (TagLostException tagLost)
                {
                    writeResult.ReasonForFailure = FailureReasons.TagLostDuringWrite;
                    _taskCompletionSource.TrySetResult(writeResult);
                    return;
                }
                catch (Exception err)
                {
                    Mvx.Error("Error writing Tag: " + err.ToString());
                    writeResult.ReasonForFailure = FailureReasons.ErrorDuringWrite;
                    _taskCompletionSource.TrySetResult(writeResult);
                    return;
                }
            }
            else
            {
                writeResult.ReasonForFailure = FailureReasons.UnableToFormatTag;
                _taskCompletionSource.TrySetResult(writeResult);
                return;
            }
        }
Esempio n. 3
0
 /// <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());
 }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
 /// <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());
 }