| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 | using System;using System.Collections.Generic;using System.Text;using System.Linq;using Windows.Devices.Enumeration;using Windows.Security.Cryptography;using Windows.Storage.Streams;using System.Threading.Tasks;using System.Threading;using System.Text.RegularExpressions;using BluetoothLENet.Classes;namespace BluetoothLENet{    public static class Utilities    {        /// <summary>        ///     Converts from standard 128bit UUID to the assigned 32bit UUIDs. Makes it easy to compare services        ///     that devices expose to the standard list.        /// </summary>        /// <param name="uuid">UUID to convert to 32 bit</param>        /// <returns></returns>        public static ushort ConvertUuidToShortId(Guid uuid)        {            // Get the short Uuid            var bytes = uuid.ToByteArray();            var shortUuid = (ushort)(bytes[0] | bytes[1] << 8);            return shortUuid;        }        /// <summary>        ///     Converts from a buffer to a properly sized byte array        /// </summary>        /// <param name="buffer"></param>        /// <returns></returns>        public static byte[] ReadBufferToBytes(IBuffer buffer)        {            var dataLength = buffer.Length;            var data = new byte[dataLength];            using (var reader = DataReader.FromBuffer(buffer))            {                reader.ReadBytes(data);            }            return data;        }        /// <summary>        /// This function converts IBuffer data to string by specified format        /// </summary>        /// <param name="buffer"></param>        /// <param name="format"></param>        /// <returns></returns>        public static string FormatValue(IBuffer buffer, DataFormat format)        {            byte[] data;            CryptographicBuffer.CopyToByteArray(buffer, out data);            switch (format)            {                case DataFormat.ASCII:                    return Encoding.ASCII.GetString(data);                case DataFormat.UTF8:                    return Encoding.UTF8.GetString(data);                case DataFormat.Dec:                    return string.Join(" ", data.Select(b => b.ToString("00")));                case DataFormat.Hex:                    return BitConverter.ToString(data).Replace("-", " ");                case DataFormat.Bin:                    var s = string.Empty;                    foreach (var b in data) s += Convert.ToString(b, 2).PadLeft(8, '0') + " ";                    return s;                default:                    return Encoding.ASCII.GetString(data);            }        }        /// <summary>        /// Format data for writing by specific format        /// </summary>        /// <param name="data"></param>        /// <param name="format"></param>        /// <returns></returns>        public static IBuffer FormatData(string data, DataFormat format)        {            try            {                if (format == DataFormat.ASCII || format == DataFormat.UTF8)                {                    return CryptographicBuffer.ConvertStringToBinary(Regex.Unescape(data), BinaryStringEncoding.Utf8);                }                else                {                    string[] values = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);                    byte[] bytes = new byte[values.Length];                    for (int i = 0; i < values.Length; i++)                        bytes[i] = Convert.ToByte(values[i], format == DataFormat.Dec ? 10 : format == DataFormat.Hex ? 16 : 2);                    var writer = new DataWriter();                    writer.ByteOrder = ByteOrder.LittleEndian;                    writer.WriteBytes(bytes);                    return writer.DetachBuffer();                }            }            catch (Exception error)            {                Console.WriteLine(error.Message);                return null;            }        }        /// <summary>        /// This function is trying to find device or service or attribute by name or number        /// </summary>        /// <param name="collection">source collection</param>        /// <param name="name">name or number to find</param>        /// <returns>ID for device, Name for services or attributes</returns>        public static string GetIdByNameOrNumber(object collection, string name)        {            string result = string.Empty;            // for devices            if (collection is List<DeviceInformation>)            {                var foundDevices = (collection as List<DeviceInformation>).Where(d => d.Name.ToLower().StartsWith(name.ToLower())).ToList();                if (foundDevices.Count == 0)                {                    if (!Console.IsOutputRedirected)                        Console.WriteLine("Can't connect to {0}.", name);                }                else if (foundDevices.Count == 1)                {                    result = foundDevices.First().Id;                }                else                {                    if (!Console.IsOutputRedirected)                        Console.WriteLine("Found multiple devices with names started from {0}. Please provide an exact name.", name);                }            }            // for services or attributes            else            {                var foundDispAttrs = (collection as List<BluetoothLEAttributeDisplay>).Where(d => d.Name.Split(':').Last().Trim().ToLower().StartsWith(name.ToLower())).ToList();                if (foundDispAttrs.Count == 0)                {                    if (Console.IsOutputRedirected)                        Console.WriteLine("No service/characteristic found by name {0}.", name);                }                else if (foundDispAttrs.Count == 1)                {                    result = foundDispAttrs.First().Name;                }                else                {                    if (Console.IsOutputRedirected)                        Console.WriteLine("Found multiple services/characteristic with names started from {0}. Please provide an exact name.", name);                }            }            return result;        }    }}
 |