private void CurrentOnOnMessageReceived(ITagInfo tagInfo) { AppendText("MessageReceived"); AppendText($"SerialNumber = {tagInfo.SerialNumber}"); var id = NFCUtils.ByteArrayToHexString(tagInfo.Identifier); AppendText($"Identifier = {id}"); AppendText($"tagInfo.IsEmpty = {tagInfo.IsEmpty}"); AppendText($"tagInfo.IsWritable = {tagInfo.IsWritable}"); if (tagInfo.Records != null && tagInfo.Records.Any()) { AppendText($"tagInfo.Records.Length = {tagInfo.Records.Length}"); foreach (var record in tagInfo.Records) { if (record.Payload != null && record.Payload.Any()) { var payload = NFCUtils.ByteArrayToHexString(record.Payload); AppendText($"record.Payload = {payload}"); } if (record.Message != null) { AppendText($"record.Message = {record.Message}"); } } } Device.BeginInvokeOnMainThread(CrossNFC.Current.StartListening); Analytics.TrackEvent("NfcTag", new Dictionary <string, string> { { "Identifier", id } }); }
async void Current_OnMessageReceived(ITagInfo tagInfo) { if (tagInfo == null) { await ShowAlert("No tag found"); return; } // Customized serial number var identifier = tagInfo.Identifier; var serialNumber = NFCUtils.ByteArrayToHexString(identifier, ":"); var title = !string.IsNullOrWhiteSpace(serialNumber) ? $"Tag [{serialNumber}]" : "Tag Info"; if (!tagInfo.IsSupported) { await ShowAlert("Unsupported tag (app)", title); } else if (tagInfo.IsEmpty) { await ShowAlert("Empty tag", title); } else { var first = tagInfo.Records[0]; await ShowAlert(GetMessage(first), title); } }
public static List <string> GetRecords(NdefRecord[] records) { List <string> textMessages = new List <string>(); for (var i = 0; i < records.Length; i++) { var record = records[i]; if (record != null) { var typeFormat = (NFCNdefTypeFormat)record.Tnf; var uri = record.ToUri()?.ToString(); var mimeType = record.ToMimeType(); var payload = record.GetPayload(); string textmessage = NFCUtils.GetMessage(typeFormat, payload, uri); if (string.IsNullOrEmpty(textmessage) == false) { textMessages.Add(textmessage); } } } return(textMessages); }
async void Current_OnTagDiscovered(ITagInfo tagInfo, bool format) { if (!CrossNFC.Current.IsWritingTagSupported) { await ShowAlert("Writing tag is not supported on this device"); return; } try { var record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Mime, MimeType = MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("Hi Buddy!") //TypeFormat = NFCNdefTypeFormat.Uri, //Payload = NFCUtils.EncodeToByteArray("http://google.fr") }; tagInfo.Records = new[] { record }; if (format) { CrossNFC.Current.ClearMessage(tagInfo); } else { CrossNFC.Current.PublishMessage(tagInfo); } } catch (System.Exception ex) { await ShowAlert(ex.Message); } }
void Current_OnTagDiscovered(ITagInfo tagInfo, bool format) { try { if (!CrossNFC.Current.IsWritingTagSupported) { return; } try { NFCNdefRecord record = null; switch (_type) { case NFCNdefTypeFormat.WellKnown: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.WellKnown, MimeType = MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("Plugin.NFC is awesome!") }; break; case NFCNdefTypeFormat.Uri: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Uri, Payload = NFCUtils.EncodeToByteArray("https://github.com/franckbour/Plugin.NFC") }; break; case NFCNdefTypeFormat.Mime: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Mime, MimeType = MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("Plugin.NFC is awesome!") }; break; default: break; } if (!format && record == null) { throw new Exception("Record can't be null."); } tagInfo.Records = new[] { record }; if (format) { CrossNFC.Current.ClearMessage(tagInfo); } else { CrossNFC.Current.PublishMessage(tagInfo, _makeReadOnly); } } catch (System.Exception ex) { } } catch (Exception ex) { } }
/// <summary> /// Custom contructor /// </summary> /// <param name="identifier">Tag Identifier as <see cref="byte[]"/></param> /// <param name="isNdef">Is Ndef tag</param> public TagInfo(byte[] identifier, bool isNdef = true) { Identifier = identifier; SerialNumber = NFCUtils.ByteArrayToHexString(identifier); IsSupported = isNdef; }
async void Current_OnTagDiscovered(ITagInfo tagInfo, bool format) { try { if (!CrossNFC.Current.IsWritingTagSupported) { await DisplayAlert("Alert", "Writing tag not supported on this device", "Ok"); return; } try { NFCNdefRecord record = null; switch (_type) { case NFCNdefTypeFormat.WellKnown: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.WellKnown, MimeType = MonthlyPassCashPaymentPage.MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("Plugin.NFC is awesome!") }; break; case NFCNdefTypeFormat.Uri: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Uri, Payload = NFCUtils.EncodeToByteArray("https://github.com/franckbour/Plugin.NFC") }; break; case NFCNdefTypeFormat.Mime: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Mime, MimeType = MonthlyPassCashPaymentPage.MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("Plugin.NFC is awesome!") }; break; default: break; } if (!format && record == null) { throw new Exception("Record can't be null."); } tagInfo.Records = new[] { record }; if (format) { CrossNFC.Current.ClearMessage(tagInfo); } else { CrossNFC.Current.PublishMessage(tagInfo, _makeReadOnly); } } catch (System.Exception ex) { await DisplayAlert("Alert", ex.Message, "Ok"); } } catch (Exception ex) { await DisplayAlert("Alert", "Unable to proceed,Please contact Admin" + ex.Message, "Ok"); } }
async void Current_OnTagDiscovered(ITagInfo tagInfo, bool format) { if (!CrossNFC.Current.IsWritingTagSupported) { await ShowAlert("Writing tag is not supported on this device"); return; } try { NFCNdefRecord record = null; switch (_type) { case NFCNdefTypeFormat.WellKnown: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.WellKnown, MimeType = MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("This is a text message!") }; break; case NFCNdefTypeFormat.Uri: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Uri, Payload = NFCUtils.EncodeToByteArray("https://google.fr") }; break; case NFCNdefTypeFormat.Mime: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Mime, MimeType = MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("This is a custom record!") }; break; default: break; } if (!format && record == null) { throw new Exception("Record can't be null."); } tagInfo.Records = new[] { record }; if (format) { CrossNFC.Current.ClearMessage(tagInfo); } else { CrossNFC.Current.PublishMessage(tagInfo); } } catch (System.Exception ex) { await ShowAlert(ex.Message); } }
void Current_OnTagDiscovered(ITagInfo tagInfo, bool format) { try { if (!CrossNFC.Current.IsWritingTagSupported) { DisplayAlert("Alert", "Writing tag not supported on this device", "Ok"); return; } try { NFCNdefRecord record = null; switch (_type) { case NFCNdefTypeFormat.WellKnown: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.WellKnown, MimeType = MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("Plugin.NFC is awesome!") }; break; case NFCNdefTypeFormat.Uri: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Uri, Payload = NFCUtils.EncodeToByteArray("https://github.com/franckbour/Plugin.NFC") }; break; case NFCNdefTypeFormat.Mime: record = new NFCNdefRecord { TypeFormat = NFCNdefTypeFormat.Mime, MimeType = MIME_TYPE, Payload = NFCUtils.EncodeToByteArray("Plugin.NFC is awesome!") }; break; default: break; } if (!format && record == null) { throw new Exception("Record can't be null."); } tagInfo.Records = new[] { record }; if (format) { CrossNFC.Current.ClearMessage(tagInfo); } else { CrossNFC.Current.PublishMessage(tagInfo, _makeReadOnly); } } catch (System.Exception ex) { DisplayAlert("Alert", ex.Message, "Ok"); } } catch (Exception ex) { DisplayAlert("Alert", "Unable to proceed,Please contact Admin" + ex.Message, "Ok"); dal_Exceptionlog.InsertException(Convert.ToString(App.Current.Properties["apitoken"]), "Operator App", ex.Message, "MasterDetailHomePage.xaml.cs", "", "Current_OnTagDiscovered"); } }
protected override void OnNewIntent(Intent intent) { //base.OnNewIntent(intent); // CrossNFC.OnNewIntent(intent); string manuf = ""; string data = ""; string serialNumber = ""; TagRead tagRead = new TagRead(); if (intent.Action == NfcAdapter.ActionTagDiscovered) { List <string> tags = new List <string>(); NdefMessage ndefMessage = null; var id = intent.GetByteArrayExtra(NfcAdapter.ExtraId); if (id != null) { serialNumber = NFCUtils.ByteArrayToHexString(id, ":"); /*for (int ii = 0; ii < id.Length; ii++) * { * if (!string.IsNullOrEmpty(data)) * serialNumber += "-"; * serialNumber += id[ii].ToString("X2"); * }*/ tagRead.SerialNumber = serialNumber; tags.Add(serialNumber); } else { tags.Add(null); } var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag != null) { manuf = NFCHelpers.GetManufacturer(tag); tags.Add(manuf); tagRead.Model = manuf; if (NFCHelpers.IsNDEFTag(tag)) { var ndef = Ndef.Get(tag); if (ndef != null) { if (ndefMessage == null) { ndefMessage = ndef.CachedNdefMessage; } if (ndefMessage != null) { NdefRecord[] records = ndefMessage.GetRecords(); var ndefrecords = NFCHelpers.GetRecords(records); for (var i = 0; i < ndefrecords.Count; i++) { if (data != "") { data += "\n"; } data += ndefrecords[i]; } tags.Add(data); tagRead.Data = data; } } } /* * var rawTagMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraTag); * * // First get all the NdefMessage * var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages); * if (rawMessages != null) * { * * // https://medium.com/@ssaurel/create-a-nfc-reader-application-for-android-74cf24f38a6f * * foreach (var message in rawMessages) * { * * foreach (var r in NdefMessageParser.GetInstance().Parse((NdefMessage)message)) * { * System.Diagnostics.Debug.WriteLine("TAG: " + r.Str()); * tags.Add(r.Str()); * } * * } * }*/ } MessagingCenter.Send <App, TagRead>((App)Xamarin.Forms.Application.Current, "Tag", tagRead); } else if (intent.Action == NfcAdapter.ActionNdefDiscovered) { System.Diagnostics.Debug.WriteLine("ActionNdefDiscovered"); } }