public void ReadValues(NFCNdefMessage message, NSError error) { try { if (MainPage.currentPage == "Read From Node") //If current page is read page { NFCNdefPayload messageRecord = message.Records[0]; ReadNFC.DisplayValues(messageRecord.Payload.ToArray()); //Encrypt nonce and create reply Session.InvalidateSession(); } else // If current page is write page { string test = "sjkdsakdjdksad"; //byte[] bytes = Encoding.ASCII.GetBytes(test); byte[] bytes = WriteNFC.CreateRequest(); NFCNdefPayload writePayload = new NFCNdefPayload(NFCTypeNameFormat.Unknown, NSData.FromArray(new byte[0]), NSData.FromArray(new byte[0]), NSData.FromArray(bytes)); NFCNdefMessage writeMessage = new NFCNdefMessage(new NFCNdefPayload[] { writePayload }); tag.WriteNdef(writeMessage, delegate { Console.WriteLine("Write"); }); System.Threading.Thread.Sleep(300); Session.InvalidateSession(); } } catch { Console.WriteLine("Did not contain a message"); } }
protected override void OnNewIntent(Intent intent) //When a new NFC tag is discovered { try { var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; IParcelable[] rawMsgs = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages); if (rawMsgs != null) { NdefMessage message = (NdefMessage)rawMsgs[0]; NdefRecord[] records = message.GetRecords(); if (records != null) { if (MainPage.currentPage == "Read From Node") //If current page is read page { ReadNFC.DisplayValues(records[0].GetPayload()); //Encrypt nonce and create reply } else //If current page is write page { Ndef ndef = Ndef.Get(tag); ndef.Connect(); byte[] bytes = WriteNFC.CreateRequest(); NdefRecord newRecord = new NdefRecord(NdefRecord.TnfUnknown, new byte[0], new byte[0], bytes); NdefMessage newMessage = new NdefMessage(new NdefRecord[] { newRecord }); ndef.WriteNdefMessage(newMessage); ndef.Close(); Toast.MakeText(ApplicationContext, "Write Succesful", ToastLength.Long).Show(); } } } else { Toast.MakeText(ApplicationContext, "The Tag did not contain a message.", ToastLength.Long).Show(); } } catch { Toast.MakeText(ApplicationContext, "Something went wrong, please try again.", ToastLength.Long).Show(); } }