using System.Linq; using System.Text; #if __IOS__ using UIKit; #endif namespace Plugin.NFC { /// /// NFC tools /// public static class NFCUtils { /// /// Returns the content size of an array of /// /// array of /// Content size internal static int GetSize(NFCNdefRecord[] records) { var size = 0; if (records != null && records.Length > 0) { for (var i = 0; i < records.Length; i++) { if (records[i] != null) size += records[i].Payload.Length; } } return size; } /// /// Returns the string formatted payload /// /// type of /// record payload /// record uri /// String formatted payload internal static string GetMessage(NFCNdefTypeFormat type, byte[] payload, string uri) { string message; if (!string.IsNullOrWhiteSpace(uri)) message = uri; else { if (type == NFCNdefTypeFormat.WellKnown) { // NDEF_WELLKNOWN Text record var status = payload[0]; var enc = status & 0x80; var languageCodeLength = status & 0x3F; if (enc == 0) message = Encoding.UTF8.GetString(payload, languageCodeLength + 1, payload.Length - languageCodeLength - 1); else message = Encoding.Unicode.GetString(payload, languageCodeLength + 1, payload.Length - languageCodeLength - 1); } else { // Other NDEF types message = Encoding.UTF8.GetString(payload, 0, payload.Length); } } return message; } /// /// Transforms a string message into an array of bytes /// /// text message /// Array of bytes public static byte[] EncodeToByteArray(string text) => Encoding.UTF8.GetBytes(text); /// /// Returns the string formatted payload /// /// Object /// String formatted payload public static string GetMessage(NFCNdefRecord record) { if (record == null) return string.Empty; return GetMessage(record.TypeFormat, record.Payload, record.Uri); } /// /// Convert bytes array into hexadecimal string /// /// Bytes Array /// Separator /// Hexadecimal string public static string ByteArrayToHexString(byte[] bytes, string separator = null) { return bytes == null ? string.Empty : string.Join(separator ?? string.Empty, bytes.Select(b => b.ToString("X2"))); } /// /// Checks if writing tags is supported /// /// boolean public static bool IsWritingSupported() { #if __IOS__ var splitted = UIDevice.CurrentDevice.SystemVersion?.Split('.'); if (splitted != null && splitted.Length > 0 && int.TryParse(splitted[0], out var majorVersion)) return majorVersion >= 13; return false; #else return true; #endif } } }