Example #1
0
        private void ReadCharacteristics_ValueChanged(ObservableGattCharacteristics sender, CharacteristicsValueChangedArgs args)
        {
            foreach (var t in args.Data)
            {
                if (_parser.PushData(t))
                {
                    var result = CodeyShareableFactory.Generate(_parser.GetPacket());

                    if (result is SharedVariable variable)
                    {
                        SaveVariable(variable);

                        OnVariableValueChanged(new VariableValueChangedArgs()
                        {
                            Variable = variable
                        });
                    }
                    else if (result is BroadcastMessage message)
                    {
                        OnMessageReceived(new MessageReceivedArgs()
                        {
                            Message = message
                        });
                    }
                    else if (result is Heartbeat heartbeat)
                    {
                        // 机器端心跳包接收
                        if (heartbeat.Code[0] == 0x01)
                        {
                            Send(new Heartbeat());
                        }
                    }
                }
            }
        }
        private async void ReadCharValue_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            button.IsEnabled = false;

            ObservableGattCharacteristics characteristic = CBCharacteristic.SelectedItem as ObservableGattCharacteristics;

            if (characteristic != null)
            {
                TBCharValue.Text = await characteristic.ReadValueAsync();
            }

            button.IsEnabled = true;
        }
        private void CBCharacteristic_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                ObservableGattCharacteristics characteristic = e.AddedItems[0] as ObservableGattCharacteristics;

                if (characteristic.Characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Read))
                {
                    BtReadCharValue.Visibility = Visibility.Visible;
                    TBCharValue.Text           = string.Empty;
                }
                else
                {
                    TBCharValue.Text = "This characteristic can not be read because the read property is not set";
                }
            }
            else
            {
                BtReadCharValue.Visibility = Visibility.Collapsed;
                TBCharValue.Text           = string.Empty;
            }
        }