Exemple #1
0
        public NameService(GattDeviceService service, NameService defaultService, int count = -1)
        {
            var reg = BluetoothServiceRegistration.FindRegistration(service.Uuid);

            UUID = service.Uuid.ToString("D"); // documented at https://docs.microsoft.com/en-us/dotnet/api/system.guid.tostring?view=netframework-4.8#System_Guid_ToString_System_String_
            if (defaultService != null)
            {
                Name        = defaultService.Name;
                Suppress    = defaultService.Suppress;
                Description = defaultService.Description;
                Priority    = defaultService.Priority;
                if (!string.IsNullOrEmpty(defaultService.RegistrationOwner))
                {
                    RegistrationOwner = defaultService.RegistrationOwner;
                }
                else if (reg != null)
                {
                    RegistrationOwner = reg.RegistrationOwner;
                }
            }
            else
            {
                Name = count >= 0 ? $"Unknown{count}" : "Unknown";
                if (reg != null)
                {
                    RegistrationOwner = reg.RegistrationOwner;
                }
            }
        }
Exemple #2
0
        public async Task InitAsync(GattDeviceService service, GattCharacteristic characteristic, string preferredFormat)
        {
            uiPreferredFormat.Text = preferredFormat;

            string str = null;

            try
            {
                AddData("Service", characteristic.Service.Uuid.ToString());
                var reg = BluetoothServiceRegistration.FindRegistration(characteristic.Service.Uuid);
                if (reg != null)
                {
                    AddData("RegistrationOwner", reg.RegistrationOwner);
                }
                AddData("ID", characteristic.Uuid.ToString());
            }
            catch (Exception e1)
            {
                System.Diagnostics.Debug.WriteLine($"Exception: CharacteristicDetailViewer: Adding data {e1.Message}");
            }
            try
            {
                str = characteristic.UserDescription;
            }
            catch (Exception e2)
            {
                System.Diagnostics.Debug.WriteLine($"Exception: CharacteristicDetailViewer: Getting user description {e2.Message}");
            }
            if (!String.IsNullOrWhiteSpace(str))
            {
                AddData("Description", str);
            }
            try
            {
                str = "";
                foreach (var format in characteristic.PresentationFormats)
                {
                    str = $"{format.Description} type={format.FormatType} namespace={format.Namespace} unit={format.Unit} exponent={format.Exponent}";
                    AddData("Format", str);
                }
            }
            catch (Exception e)
            {
                AddData("Format", $"Exception: {e.Message}");
            }
            str = "";

            // Don't bother with descriptors
            try
            {
                var dc = DeviceCharacteristic.Create(characteristic);
                AddData("Methods", dc.PropertiesAsString); // e.g. Read Write etc.
            }
            catch (Exception e)
            {
                AddData("Methods", $"Exception: {e.Message}");
            }

            try
            {
                AddData("Protection Level", characteristic.ProtectionLevel.ToString());
            }
            catch (Exception e)
            {
                AddData("Protection Level", $"Exception: {e.Message}");
            }

            try
            {
                if (characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Read))
                {
                    var buffer = await characteristic.ReadValueAsync();

                    if (buffer.Status == GattCommunicationStatus.Success)
                    {
                        var data = ValueParser.ConvertToStringHex(buffer.Value);
                        AddData("Data", data);
                    }
                    else
                    {
                        AddData("Data", $"Error: {buffer.Status}");
                    }
                }
            }
            catch (Exception e)
            {
                AddData("Data", $"Exception: {e.Message}");
            }
        }