public ObservableBluetoothLEAdvertisementSection(BluetoothLEAdvertisementDataSection section) { TypeAsString = section.DataType.ToString("X2"); TypeAsDisplayString = AdvertisementDataTypeHelper.ConvertSectionTypeToString(section.DataType); DataAsString = GattConvert.ToHexString(section.Data); if (section.DataType == BluetoothLEAdvertisementDataTypes.Flags) { var flagsInt = GattConvert.ToInt32(section.Data); DataAsDisplayString = ((BluetoothLEAdvertisementFlags)Enum.ToObject(typeof(BluetoothLEAdvertisementFlags), flagsInt)).ToString(); } else if (section.DataType == BluetoothLEAdvertisementDataTypes.CompleteLocalName || section.DataType == BluetoothLEAdvertisementDataTypes.ShortenedLocalName) { DataAsDisplayString = GattConvert.ToUTF8String(section.Data); } else if (section.DataType == BluetoothLEAdvertisementDataTypes.TxPowerLevel) { var txPowerLevel = GattConvert.ToInt16(section.Data); DataAsDisplayString = txPowerLevel.ToString(); } else { DataAsDisplayString = "<Unknown>"; } }
public void Int32Test() { Int32 data = 42; Assert.AreEqual(data, GattConvert.ToInt32(GattConvert.ToIBuffer(data))); Assert.AreNotEqual(data - 1, GattConvert.ToInt32(GattConvert.ToIBuffer(data))); }
public void IntTest1() { int data = 42; IBuffer buf = GattConvert.ToIBuffer(data); Assert.AreEqual(data, GattConvert.ToInt32(buf)); Assert.AreNotEqual(43, GattConvert.ToInt32(buf)); }
public void Int16Test() { Int16 data = 42; Int64 wrong = Int64.MaxValue; Assert.AreEqual(data, GattConvert.ToInt32(GattConvert.ToIBuffer(data))); Assert.AreNotEqual(data - 1, GattConvert.ToInt16(GattConvert.ToIBuffer(data))); Assert.AreNotEqual(wrong, GattConvert.ToInt16(GattConvert.ToIBuffer(wrong))); }
public void IntTest3() { Int16 data = 42; DataWriter writer = new DataWriter(); writer.ByteOrder = ByteOrder.LittleEndian; writer.WriteInt16(data); IBuffer result = writer.DetachBuffer(); Assert.AreEqual(42, GattConvert.ToInt32(result)); Assert.AreNotEqual(43, GattConvert.ToInt32(result)); }
public void IntTest2() { byte[] data = { 42, 0 }; DataWriter writer = new DataWriter(); writer.ByteOrder = ByteOrder.LittleEndian; writer.WriteBytes(data); IBuffer result = writer.DetachBuffer(); Assert.AreEqual(42, GattConvert.ToInt32(result)); Assert.AreNotEqual(43, GattConvert.ToInt32(result)); }
/// <summary> /// Sets the value of this characteristic based on the display type /// </summary> private void SetValue() { if (data == null) { Value = "NULL"; return; } GattPresentationFormat format = null; if (Characteristic.PresentationFormats.Count > 0) { format = Characteristic.PresentationFormats[0]; } // Determine what to set our DisplayType to if (format == null && DisplayType == DisplayTypes.NotSet) { if (Name == "DeviceName") { // All devices have DeviceName so this is a special case. DisplayType = DisplayTypes.UTF8; } else { string buffer = string.Empty; bool isString = true; try { buffer = GattConvert.ToUTF8String(rawData); } catch (Exception) { isString = false; } if (isString == true) { // if buffer is only 1 char or 2 char with 0 at end then let's assume it's hex if (buffer.Length == 1) { isString = false; } else if (buffer.Length == 2 && buffer[1] == 0) { isString = false; } else { foreach (char b in buffer) { // if within the reasonable range of used characters and not null, let's assume it's a UTF8 string by default, else hex if ((b < ' ' || b > '~') && b != 0) { isString = false; break; } } } } if (isString) { DisplayType = DisplayTypes.UTF8; } else { // By default, display as Hex DisplayType = DisplayTypes.Hex; } } } else if (format != null && DisplayType == DisplayTypes.NotSet) { if (format.FormatType == GattPresentationFormatTypes.Boolean || format.FormatType == GattPresentationFormatTypes.Bit2 || format.FormatType == GattPresentationFormatTypes.Nibble || format.FormatType == GattPresentationFormatTypes.UInt8 || format.FormatType == GattPresentationFormatTypes.UInt12 || format.FormatType == GattPresentationFormatTypes.UInt16 || format.FormatType == GattPresentationFormatTypes.UInt24 || format.FormatType == GattPresentationFormatTypes.UInt32 || format.FormatType == GattPresentationFormatTypes.UInt48 || format.FormatType == GattPresentationFormatTypes.UInt64 || format.FormatType == GattPresentationFormatTypes.SInt8 || format.FormatType == GattPresentationFormatTypes.SInt12 || format.FormatType == GattPresentationFormatTypes.SInt16 || format.FormatType == GattPresentationFormatTypes.SInt24 || format.FormatType == GattPresentationFormatTypes.SInt32) { DisplayType = DisplayTypes.Decimal; } else if (format.FormatType == GattPresentationFormatTypes.Utf8) { DisplayType = DisplayTypes.UTF8; } else if (format.FormatType == GattPresentationFormatTypes.Utf16) { DisplayType = DisplayTypes.UTF16; } else if (format.FormatType == GattPresentationFormatTypes.UInt128 || format.FormatType == GattPresentationFormatTypes.SInt128 || format.FormatType == GattPresentationFormatTypes.DUInt16 || format.FormatType == GattPresentationFormatTypes.SInt64 || format.FormatType == GattPresentationFormatTypes.Struct || format.FormatType == GattPresentationFormatTypes.Float || format.FormatType == GattPresentationFormatTypes.Float32 || format.FormatType == GattPresentationFormatTypes.Float64) { DisplayType = DisplayTypes.Unsupported; } else { DisplayType = DisplayTypes.Unsupported; } } // Decode the value into the right display type if (DisplayType == DisplayTypes.Hex || DisplayType == DisplayTypes.Unsupported) { Value = GattConvert.ToHexString(rawData); } else if (DisplayType == DisplayTypes.Decimal) { //TODO: if data is larger then int32 this will overflow. Need to fix. Value = GattConvert.ToInt32(rawData).ToString(); } else if (DisplayType == DisplayTypes.UTF8) { Value = GattConvert.ToUTF8String(rawData); } else if (DisplayType == DisplayTypes.UTF16) { Value = GattConvert.ToUTF16String(rawData); } }
/// <summary> /// Converts GenericGattCharacteristic.Value to a string based on the presentation format /// </summary> /// <param name="value">value to convert</param> /// <param name="format">presentation format to use</param> /// <returns>value as string</returns> public static string ConvertValueBufferToString(IBuffer value, GattPresentationFormat format = null) { // no format, return bytes if (format == null) { return(GattConvert.ToHexString(value)); } // Bool if (format.FormatType == GattPresentationFormatTypes.Boolean) { // Previous implementation was incorrect. Need to implement in GattHelper. throw new NotImplementedException(); } else if (format.FormatType == GattPresentationFormatTypes.Bit2 || format.FormatType == GattPresentationFormatTypes.Nibble) { // 2bit or nibble - no exponent // Previous implementation was incorrect. Need to implement in GattHelper. return(GattConvert.ToHexString(value)); } else if (format.FormatType == GattPresentationFormatTypes.UInt8 || format.FormatType == GattPresentationFormatTypes.UInt12 || format.FormatType == GattPresentationFormatTypes.UInt16) { // Previous implementation was incorrect. Need to implement in GattHelper. return(GattConvert.ToHexString(value)); } else if (format.FormatType == GattPresentationFormatTypes.UInt24 || format.FormatType == GattPresentationFormatTypes.UInt32) { // Previous implementation was incorrect. Need to implement in GattHelper. return(GattConvert.ToHexString(value)); } else if (format.FormatType == GattPresentationFormatTypes.UInt48 || format.FormatType == GattPresentationFormatTypes.UInt64) { // Previous implementation was incorrect. Need to implement in GattHelper. return(GattConvert.ToHexString(value)); } else if (format.FormatType == GattPresentationFormatTypes.SInt8 || format.FormatType == GattPresentationFormatTypes.SInt12 || format.FormatType == GattPresentationFormatTypes.SInt16) { // Previous implementation was incorrect. Need to implement in GattHelper. return(GattConvert.ToHexString(value)); } else if (format.FormatType == GattPresentationFormatTypes.SInt24 || format.FormatType == GattPresentationFormatTypes.SInt32) { return(GattConvert.ToInt32(value).ToString()); } else if (format.FormatType == GattPresentationFormatTypes.Utf8) { return(GattConvert.ToUTF8String(value)); } else if (format.FormatType == GattPresentationFormatTypes.Utf16) { return(GattConvert.ToUTF16String(value)); } else { // format.FormatType == GattPresentationFormatTypes.UInt128 || // format.FormatType == GattPresentationFormatTypes.SInt128 || // format.FormatType == GattPresentationFormatTypes.DUInt16 || // format.FormatType == GattPresentationFormatTypes.SInt64 || // format.FormatType == GattPresentationFormatTypes.Struct || // format.FormatType == GattPresentationFormatTypes.Float || // format.FormatType == GattPresentationFormatTypes.Float32 || // format.FormatType == GattPresentationFormatTypes.Float64 return(GattConvert.ToHexString(value)); } }