private void Write(object sender, EventArgs e)
        {
            lock (this)
            {
                var label = FindViewById <TextView>(Resource.Id.DataLabel);

                try
                {
                    if (nfcTag == null)
                    {
                        label.Text = "nfc tag is null";
                        return;
                    }

                    var data = DateTime.Now.ToString();

                    /*
                     * var ndefRecord = new NdefRecord(NdefRecord.TnfMimeMedia,
                     *  null,
                     *  new byte[] { },
                     *  Encoding.UTF8.GetBytes(data));
                     */

                    var ndefRecord = NdefRecord.CreateExternal(
                        "Smartisan.Nfc.Smartisan.Nfc", // your domain name
                        "myapp",                       // your type name
                        Encoding.UTF8.GetBytes(data)); // payload


                    var ndef = Ndef.Get(nfcTag);
                    ndef.Connect();
                    ndef.WriteNdefMessage(new NdefMessage(ndefRecord));
                    ndef.Close();

                    label.Text = $"Data:{newLine}{data}";
                }
                catch (Exception ex)
                {
                    label.Text += $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}";
                }
                finally
                {
                    if (nfcTag != null)
                    {
                        if (Ndef.Get(nfcTag).IsConnected)
                        {
                            Ndef.Get(nfcTag).Close();
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Transforms a <see cref="NFCNdefRecord"/> into an Android <see cref="NdefRecord"/>
        /// </summary>
        /// <param name="record">Object <see cref="NFCNdefRecord"/></param>
        /// <returns>Android <see cref="NdefRecord"/></returns>
        NdefRecord GetAndroidNdefRecord(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NdefRecord ndefRecord = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                var languageCode = record.LanguageCode;
                if (string.IsNullOrWhiteSpace(languageCode))
                {
                    languageCode = Configuration.DefaultLanguageCode;
                }
                if (languageCode.Length > 5)
                {
                    languageCode = languageCode.Substring(0, 5);                                                //max support 5 chars like en-US or de-AT
                }
                ndefRecord = NdefRecord.CreateTextRecord(languageCode, Encoding.UTF8.GetString(record.Payload));
                //no need to force it to 2 letters only
                //ndefRecord = NdefRecord.CreateTextRecord(languageCode.Substring(0, 2), Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.Mime:
                ndefRecord = NdefRecord.CreateMime(record.MimeType, record.Payload);
                break;

            case NFCNdefTypeFormat.Uri:
                ndefRecord = NdefRecord.CreateUri(Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.External:
                ndefRecord = NdefRecord.CreateExternal(record.ExternalDomain, record.ExternalType, record.Payload);
                break;

            case NFCNdefTypeFormat.Empty:
                ndefRecord = GetEmptyNdefRecord();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(ndefRecord);
        }
Example #3
0
        /// <summary>
        /// Transforms a <see cref="NFCNdefRecord"/> into an Android <see cref="NdefRecord"/>
        /// </summary>
        /// <param name="record">Object <see cref="NFCNdefRecord"/></param>
        /// <returns>Android <see cref="NdefRecord"/></returns>
        NdefRecord GetAndroidNdefRecord(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NdefRecord ndefRecord = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                var languageCode = record.LanguageCode;
                if (string.IsNullOrWhiteSpace(languageCode))
                {
                    languageCode = Configuration.DefaultLanguageCode;
                }
                ndefRecord = NdefRecord.CreateTextRecord(languageCode.Substring(0, 2), Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.Mime:
                ndefRecord = NdefRecord.CreateMime(record.MimeType, record.Payload);
                break;

            case NFCNdefTypeFormat.Uri:
                ndefRecord = NdefRecord.CreateUri(Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.External:
                ndefRecord = NdefRecord.CreateExternal(record.ExternalDomain, record.ExternalType, record.Payload);
                break;

            case NFCNdefTypeFormat.Empty:
                ndefRecord = GetEmptyNdefRecord();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(ndefRecord);
        }
Example #4
0
        /// <summary>
        /// Transforms a <see cref="NFCNdefRecord"/> into an Android <see cref="NdefRecord"/>
        /// </summary>
        /// <param name="record">Object <see cref="NFCNdefRecord"/></param>
        /// <returns>Android <see cref="NdefRecord"/></returns>
        NdefRecord GetAndroidNdefRecord(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NdefRecord ndefRecord = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                ndefRecord = NdefRecord.CreateTextRecord(Locale.Default.ToLanguageTag(), Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.Mime:
                ndefRecord = NdefRecord.CreateMime(record.MimeType, record.Payload);
                break;

            case NFCNdefTypeFormat.Uri:
                ndefRecord = NdefRecord.CreateUri(Encoding.UTF8.GetString(record.Payload));
                break;

            case NFCNdefTypeFormat.External:
                ndefRecord = NdefRecord.CreateExternal(record.ExternalDomain, record.ExternalType, record.Payload);
                break;

            case NFCNdefTypeFormat.Empty:
                ndefRecord = GetEmptyNdefRecord();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(ndefRecord);
        }
Example #5
0
        public bool WriteExternalType(string domain, string type, string payload, object tag)
        {
            if (!(tag is Tag))
            {
                return(false);
            }
            if (string.IsNullOrEmpty(domain) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(payload))
            {
                return(false);
            }

            WriteResult writeResult = WriteResult.FAILED;

            try
            {
                NdefRecord  extRecord   = NdefRecord.CreateExternal(domain, type, Encoding.UTF8.GetBytes(payload));
                NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] { extRecord });
                writeResult = WriteTag(ndefMessage, (Tag)tag);
            }
            catch { }
            return(writeResult == WriteResult.OK);
        }