Пример #1
0
 /// <summary>
 /// Returns the string formatted payload
 /// </summary>
 /// <param name="record">Object <see cref="NFCNdefRecord"/></param>
 /// <returns>String formatted payload</returns>
 public static string GetMessage(NFCNdefRecord record)
 {
     if (record == null)
     {
         return(string.Empty);
     }
     return(GetMessage(record.TypeFormat, record.Payload, record.Uri));
 }
Пример #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);
        }
Пример #3
0
        /// <summary>
        /// Transforms an array of <see cref="NFCNdefPayload"/> into an array of <see cref="NFCNdefRecord"/>
        /// </summary>
        /// <param name="records">Array of <see cref="NFCNdefPayload"/></param>
        /// <returns>Array of <see cref="NFCNdefRecord"/></returns>
        NFCNdefRecord[] GetRecords(NFCNdefPayload[] records)
        {
            var results = new NFCNdefRecord[records.Length];

            for (var i = 0; i < records.Length; i++)
            {
                var record     = records[i];
                var ndefRecord = new NFCNdefRecord
                {
                    TypeFormat = (NFCNdefTypeFormat)record.TypeNameFormat,
                    Payload    = record.Payload.ToByteArray()
                };
                results.SetValue(ndefRecord, i);
            }
            return(results);
        }
Пример #4
0
        /// <summary>
        /// Transforms an array of <see cref="NdefRecord"/> into an array of <see cref="NFCNdefRecord"/>
        /// </summary>
        /// <param name="records">Array of <see cref="NdefRecord"/></param>
        /// <returns>Array of <see cref="NFCNdefRecord"/></returns>
        NFCNdefRecord[] GetRecords(NdefRecord[] records)
        {
            var results = new NFCNdefRecord[records.Length];

            for (var i = 0; i < records.Length; i++)
            {
                var ndefRecord = new NFCNdefRecord
                {
                    TypeFormat = (NFCNdefTypeFormat)records[i].Tnf,
                    Uri        = records[i].ToUri()?.ToString(),
                    MimeType   = records[i].ToMimeType(),
                    Payload    = records[i].GetPayload()
                };
                results.SetValue(ndefRecord, i);
            }
            return(results);
        }
Пример #5
0
        /// <summary>
        /// Returns NDEF payload
        /// </summary>
        /// <param name="record"><see cref="NFCNdefRecord"/></param>
        /// <returns><see cref="NFCNdefPayload"/></returns>
        NFCNdefPayload GetiOSPayload(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NFCNdefPayload payload = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                var lang = record.LanguageCode;
                if (string.IsNullOrWhiteSpace(lang))
                {
                    lang = Configuration.DefaultLanguageCode;
                }
                var langData = Encoding.ASCII.GetBytes(lang.Substring(0, 2));
                var payloadData = new byte[] { 0x02 }.Concat(langData).Concat(record.Payload).ToArray();
                payload = new NFCNdefPayload(NFCTypeNameFormat.NFCWellKnown, NSData.FromString("T"), new NSData(), NSData.FromString(Encoding.UTF8.GetString(payloadData), NSStringEncoding.UTF8));
                break;

            case NFCNdefTypeFormat.Mime:
                payload = new NFCNdefPayload(NFCTypeNameFormat.Media, record.MimeType, new NSData(), NSData.FromArray(record.Payload));
                break;

            case NFCNdefTypeFormat.Uri:
                payload = NFCNdefPayload.CreateWellKnownTypePayload(NSUrl.FromString(Encoding.UTF8.GetString(record.Payload)));
                break;

            case NFCNdefTypeFormat.External:
                payload = new NFCNdefPayload(NFCTypeNameFormat.NFCExternal, record.ExternalType, new NSData(), NSData.FromString(Encoding.UTF8.GetString(record.Payload), NSStringEncoding.UTF8));
                break;

            case NFCNdefTypeFormat.Empty:
                payload = GetEmptyPayload();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(payload);
        }
Пример #6
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);
        }
Пример #7
0
        /// <summary>
        /// Returns NDEF payload
        /// </summary>
        /// <param name="record"><see cref="NFCNdefRecord"/></param>
        /// <returns><see cref="NFCNdefPayload"/></returns>
        NFCNdefPayload GetiOSPayload(NFCNdefRecord record)
        {
            if (record == null)
            {
                return(null);
            }

            NFCNdefPayload payload = null;

            switch (record.TypeFormat)
            {
            case NFCNdefTypeFormat.WellKnown:
                payload = NFCNdefPayload.CreateWellKnownTypePayload(Encoding.UTF8.GetString(record.Payload), NSLocale.CurrentLocale);
                break;

            case NFCNdefTypeFormat.Mime:
                payload = new NFCNdefPayload(NFCTypeNameFormat.Media, record.MimeType, new NSData(), NSData.FromArray(record.Payload));
                break;

            case NFCNdefTypeFormat.Uri:
                payload = NFCNdefPayload.CreateWellKnownTypePayload(NSUrl.FromString(Encoding.UTF8.GetString(record.Payload)));
                break;

            case NFCNdefTypeFormat.External:
                payload = new NFCNdefPayload(NFCTypeNameFormat.NFCExternal, record.ExternalType, new NSData(), NSData.FromString(Encoding.UTF8.GetString(record.Payload), NSStringEncoding.UTF8));
                break;

            case NFCNdefTypeFormat.Empty:
                payload = GetEmptyPayload();
                break;

            case NFCNdefTypeFormat.Unknown:
            case NFCNdefTypeFormat.Unchanged:
            case NFCNdefTypeFormat.Reserved:
            default:
                break;
            }
            return(payload);
        }
Пример #8
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);
        }
Пример #9
0
        /// <summary>
        /// Transforms an array of <see cref="NFCNdefPayload"/> into an array of <see cref="NFCNdefRecord"/>
        /// </summary>
        /// <param name="records">Array of <see cref="NFCNdefPayload"/></param>
        /// <returns>Array of <see cref="NFCNdefRecord"/></returns>
        NFCNdefRecord[] GetRecords(NFCNdefPayload[] records)
        {
            if (records == null)
            {
                return(null);
            }

            var results = new NFCNdefRecord[records.Length];

            for (var i = 0; i < records.Length; i++)
            {
                var record     = records[i];
                var ndefRecord = new NFCNdefRecord
                {
                    TypeFormat = (NFCNdefTypeFormat)record.TypeNameFormat,
                    Uri        = records[i].ToUri()?.ToString(),
                    MimeType   = records[i].ToMimeType(),
                    Payload    = record.Payload.ToByteArray()
                };
                results.SetValue(ndefRecord, i);
            }
            return(results);
        }