蓝牙 GattCharacteristic.ValueChanged 从未触发我如何阅读?

问题描述

当我做写操作时,我希望事件被触发并读取,但是每当我写成功时,事件不会被触发。

这是我的写按钮点击:

   private async void btnWrite_Click(object sender,EventArgs e)
    {
        if (bluetoothLEController.currentSelectedService != null && bluetoothLEController.currentSelectedGattCharacteristic != null)
        {
            GattCharacteristicProperties properties = bluetoothLEController.currentSelectedGattCharacteristic.CharacteristicProperties;
            if (properties.HasFlag(GattCharacteristicProperties.Write))
            { 

                var writer = new DataWriter();

                TextBox[] txtBoxes = new TextBox[] {
                    textBox1,textBox2,textBox3,textBox4,textBox5,textBox6,textBox7,textBox8,textBox9,textBox10,textBox11,textBox12,textBox13,textBox14,textBox15,textBox16
                };

                var testBytesArray = new byte[txtBoxes.Length];
                for (int i = 0; i < txtBoxes.Length; i++)
                {
                    try
                    {
                        testBytesArray[i] = Convert.ToByte(txtBoxes[i].Text);
                    }
                    catch (Exception ex)
                    { }
                }

                
                    writer.WriteBytes(testBytesArray);
                
                

                GattCommunicationStatus result = await bluetoothLEController.currentSelectedGattCharacteristic.WriteValueAsync(writer.DetachBuffer());
                if (result == GattCommunicationStatus.Success)
                {
                    // Never trigger this event
                    bluetoothLEController.currentSelectedGattCharacteristic.ValueChanged += CurrentSelectedGattCharacteristic_ValueChanged; 
                    Respond("message sent successfully");
                }
                else
                {
                    Respond("Error encountered on writing to characteristic!");
                }
            }
            else
            {
                Respond("No write property for this characteristic!");
            }
        }
        else
        {
            Respond("Please connect to a device first!");
        }

    }

这是 CurrentSelectedGattCharacteristic_ValueChanged:

private void CurrentSelectedGattCharacteristic_ValueChanged(GattCharacteristic sender,GattValueChangedEventArgs args)
    {
        var reader = DataReader.FromBuffer(args.CharacteristicValue);
        byte[] input = new byte[reader.UnconsumedBufferLength];
        reader.ReadBytes(input);
        Respond(Encoding.ASCII.GetString(input));
    }

总是成功,我在屏幕上看到文本“消息发送成功”,但没有以任何方式触发事件,我看不到输出

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)