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 { /// /// Converts from standard 128bit UUID to the assigned 32bit UUIDs. Makes it easy to compare services /// that devices expose to the standard list. /// /// UUID to convert to 32 bit /// public static ushort ConvertUuidToShortId(Guid uuid) { // Get the short Uuid var bytes = uuid.ToByteArray(); var shortUuid = (ushort)(bytes[0] | bytes[1] << 8); return shortUuid; } /// /// Converts from a buffer to a properly sized byte array /// /// /// 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; } /// /// This function converts IBuffer data to string by specified format /// /// /// /// 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); } } /// /// Format data for writing by specific format /// /// /// /// 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; } } /// /// This function is trying to find device or service or attribute by name or number /// /// source collection /// name or number to find /// ID for device, Name for services or attributes public static string GetIdByNameOrNumber(object collection, string name) { string result = string.Empty; // for devices if (collection is List) { var foundDevices = (collection as List).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).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; } } }