Example #1
0
        private async Task DoIncrementWriteTapped(GattWriteOption WriteOption)
        {
            var oldValue       = uiValueShow.Text;
            var characteristic = Characteristic;

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

                    if (buffer.Status == GattCommunicationStatus.Success)
                    {
                        var data = buffer.Value.ToArray();
                        for (int i = data.Length - 1; i >= 0; i--)
                        {
                            if (data[i] < 0xFF)
                            {
                                data[i]++;
                                break;
                            }
                            else
                            {
                                data[i] = 0;
                                // And carry the one to the next byte.
                            }
                        }
                        var dataBuffer  = data.AsBuffer();
                        var newAsString = ValueParser.ConvertToStringHex(dataBuffer);
                        uiValueShow.Text = newAsString;
                        var status = await Characteristic.WriteValueWithResultAsync(dataBuffer, WriteOption);
                    }
                }
            }
            catch (Exception)
            {
                ; //TODO: show exception to user!
            }
        }
Example #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}");
            }
        }
Example #3
0
        public async Task InitAsync(GattDeviceService service, GattCharacteristic characteristic)
        {
            Service        = service;
            Characteristic = characteristic;

            string str;

            AddData("Service", characteristic.Service.Uuid.ToString());
            AddData("ID", characteristic.Uuid.ToString());
            str = characteristic.UserDescription;
            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)
                    {
                        //NOTE: also get the data as the nice version?
                        var data = ValueParser.ConvertToStringHex(buffer.Value);
                        AddData("Data", data);
                        uiEditBox.Text = data; // NOTE: what if I don't want hex?
                    }
                    else
                    {
                        AddData("Data", $"Error: {buffer.Status}");
                    }
                }
            }
            catch (Exception e)
            {
                AddData("Data", $"Exception: {e.Message}");
            }

            AddButtons(); // e.g. for the Skootbot robot controls
        }