private void SetValue()

in BluetoothLEExplorer/BluetoothLEExplorer/Models/ObservableGattCharacteristics.cs [741:905]


        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)
            {
                try
                {
                    Value = GattConvert.ToHexString(rawData);
                }
                catch(Exception)
                {
                    Value = "Error: Invalid hex value";
                }
            }
            else if (DisplayType == DisplayTypes.Decimal)
            {
                try
                {
                    Value = GattConvert.ToInt64(rawData).ToString();
                }
                catch(Exception)
                {
                    Value = "Error: Invalid Int64 Value";
                }
            }
            else if (DisplayType == DisplayTypes.UTF8)
            {
                try
                {
                    Value = GattConvert.ToUTF8String(rawData);
                }
                catch(Exception)
                {
                    Value = "Error: Invalid UTF8 String";
                }
            }
            else if (DisplayType == DisplayTypes.UTF16)
            {
                try
                {
                    Value = GattConvert.ToUTF16String(rawData);
                }
                catch(Exception)
                {
                    Value = "Error: Invalid UTF16 String";
                }
            }
        }