微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决蓝牙 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 举报,一经查实,本站将立刻删除。