BluetoothLE在通用Windows中

时间:2017-11-09 04:53:30

标签: c# winforms bluetooth

大家!!!

我正在学习BluetoothLE。如果你不能帮助我,你能给我一些建议吗?

在Windows 10 Universal Windows中研究BluetoothLE的示例源:Character_ValueChanged()不响应特征功能。在示例源中,值更改称为函数调用,但它不响应WinForm。 请...。

私人GattCharacteristic注册特色; 你好。 我正在学习BluetoothLE。然后我查看了数据,然后我看到了博客。如果你不能帮助我,你能给我一些建议吗?

在Windows 10 Universal Windows中研究BluetoothLE的示例源:Character_ValueChanged()不响应特征功能。在示例源中,值更改称为函数调用,但它不响应WinForm。 请...。

private GattCharacteristic registeredCharacteristic;
.
.



private async void BTN_Change_SubscribeToggle_Click(object sender, EventArgs e)
{
            if (!subscribedForNotifications)
            {
                // initialize status
                GattCommunicationStatus status = GattCommunicationStatus.Unreachable;
                var cccdValue = GattClientCharacteristicConfigurationDescriptorValue.None;

                if (selectedCharacteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Indicate))
                {
                    cccdValue = GattClientCharacteristicConfigurationDescriptorValue.Indicate;
                }
                else if (selectedCharacteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
                {
                    cccdValue = GattClientCharacteristicConfigurationDescriptorValue.Notify;
                }

                Debug.WriteLine("[ cccdValue = {0} ]", cccdValue);
                try
                {
                    // BT_Code: Must write the CCCD in order for server to send indications.
                    // We receive them in the ValueChanged event handler.
                    status = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(cccdValue);
                    Debug.WriteLine("[ status = {0} ]", status);
                    if (status == GattCommunicationStatus.Success)
                    {
                        AddValueChangedHandler();
                        Notify_User("Successfully subscribed for value changes", NotifyType.StatusMessage);
                    }
                    else
                    {
                        Notify_User("Error registering for value changes : " + status.ToString(), NotifyType.ErrorMessage);
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    // This usually happens when a device reports that it support indicate, but it actually doesn't.
                    Notify_User(ex.Message, NotifyType.ErrorMessage);
                }
            } // if(!subscribedForNotifications)
            else
            {
                try
                {
                    // BT_Code: Must write the CCCD in order for server to send notifications.
                    // We receive them in the ValueChanged event handler.
                    // Note that this sample configures either Indicate or Notify, but not both.
                    var result = await
                            selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
                                GattClientCharacteristicConfigurationDescriptorValue.None);
                    Debug.WriteLine("[ result = {0} ]", result);
                    if (result == GattCommunicationStatus.Success)
                    {
                        subscribedForNotifications = false;
                        RemoveValueChangedHandler();
                        Notify_User("Successfully un-registered for notifications", NotifyType.StatusMessage);
                    }
                    else
                    {
                        Notify_User("Error un-registered for notifications : " + result, NotifyType.ErrorMessage);
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    Notify_User(ex.Message, NotifyType.ErrorMessage);
                }
            } // else
}

private void AddValueChangedHandler()
{
            Debug.WriteLine("[ AddValueChangedHandler() ]");
            Debug.WriteLine("[ subscribedForNotifications = {0} ]", subscribedForNotifications);
            BTN_Change_SubscribeToggle.Text = "Unsubscribe from value changes";

            if (!subscribedForNotifications)
            {
                registeredCharacteristic = selectedCharacteristic;
                registeredCharacteristic.ValueChanged += Characteristic_ValueChanged;
                subscribedForNotifications = true;
                CTR_Update_Visible(LB_Value, true);
                CTR_Update_Msg(LB_Value, "test");
            }
}

 private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
            // BT_Code: An Indicate or Notify reported that the value has changed.
            // Display the new value with a timestamp.
            //var reader = DataReader.FromBuffer(args.CharacteristicValue);
            Debug.WriteLine("[ Characteristic_ValueChanged ]");
            var newValue = FormatValueByPresentation(args.CharacteristicValue, presentationFormat);
            var message = string.Format("Value at {0} : \r\n\t {1}", DateTime.Now.ToString("HH:mm:ss.FFF"), newValue);
}

1 个答案:

答案 0 :(得分:0)

我能理解的是:

  

值更改称为函数调用,但它不响应WinForm。

要解决此问题,请尝试从按钮单击中删除Async:

private async void BTN_Change_SubscribeToggle_Click(object sender,EventArgs e) {