【问题标题】:UWP BLE advertising status is abortedUWP BLE 广播状态已中止
【发布时间】:2021-09-24 23:04:34
【问题描述】:

每当我从 UWP 应用程序启动 BluetoothLEAdvertisementWatcher 时,它的状态都会中止。在控制台应用程序中使用相同的功能是没有问题的(包括所需的库)。当我想与 BLE 设备配对时,我使用 UWP 应用程序中的 DeviceWatcher 没有问题。操作系统为Win10,使用VS2015 Community。

为了说明这个问题,我制作了一个 UWP 项目,其中包含蓝牙功能:

   <Capabilities>
   <Capability Name="internetClient" />
   <DeviceCapability Name="bluetooth" />
   </Capabilities>

MainPage 上有用于显示BluetoothLEAdvertisementWatcher 状态的Start、Stop 和View 按钮和TextBlock。代码呈现:

public sealed partial class MainPage : Page
    {
        private BluetoothLEAdvertisementWatcher watcher = null;

        public MainPage()
        {
            this.InitializeComponent();

            watcher = new BluetoothLEAdvertisementWatcher();
            watcher.ScanningMode = BluetoothLEScanningMode.Active;

            textBlock.Text = watcher.Status.ToString();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            watcher.Received += OnAdvertisementReceived;

            watcher.Stopped += OnAdvertisementWatcherStopped;
        }


        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            watcher.Stop();
        }

        private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {

            await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                textBlock.Text = "rcvd" + watcher.Status.ToString();
            });
        }

        private async void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
        {
            // Notify the user that the watcher was stopped
            await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                textBlock.Text = "stopped:" + watcher.Status.ToString();
            });
        }

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            watcher.Start();
            textBlock.Text = watcher.Status.ToString();
        }
        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            watcher.Stop();
            textBlock.Text = watcher.Status.ToString();
        }

        private void buttonView_Click(object sender, RoutedEventArgs e)
        {
            textBlock.Text = watcher.Status.ToString();
        }
    }

程序启动时,BluetoothLEAdvertisementWatcher 状态为 Created。按下 Start 按钮后,watcher 启动,但状态变为 Aborted,并触发事件 OnAdvertisementWatcherStopped(状态仍为 Aborted)。

对于克服这个问题有什么建议吗?或者可以做些什么来澄清问题?

更新

应用程序在不同的笔记本电脑上执行。结果是一样的,所以不是硬件问题。

网上有两个建议:

  1. 启用蓝牙(Dmitry 在第一个回答中建议)

  2. 检查功能( https://keyoti.com/blog/bluetooth-low-energy-in-windows-10-troubleshooting-capabilities/)

没有提供结果。

补充说明:当 Stopped 的事件注册被移除时, (// watcher.Stopped += OnAdvertisementWatcherStopped;) 第一个结果是 Started。下一次单击按钮 View 将显示 Aborted。在很短的时间内,结果成功有效。

任何配置设置建议?

【问题讨论】:

    标签: c# bluetooth-lowenergy


    【解决方案1】:

    这个帖子帮助了我。

    https://social.msdn.microsoft.com/Forums/windowshardware/en-US/5351a1f0-92f3-498b-a0c1-805d568cb55c/when-uwp-ble-advertising-watcher-is-started-its-status-is-aborted?forum=wdk

    解决方案是为 uwp 应用启用“与设备同步”权限(设置->隐私->其他设备)。

    【讨论】:

    • 这是我的问题的解决方案。我希望更新文档,因为我没有在其他任何地方找到此建议。
    【解决方案2】:

    我遇到了与 OP 类似的问题。在GattServiceProvider.StartAdvertising() 之后,属性GattServiceProvider.AdvertisementStatus 返回Aborted。我尝试了上述所有建议,但均未成功。

    接下来,我从 https://github.com/microsoft/Windows-universal-samples/tree/main/Samples/BluetoothLE 下载了一个官方的 Microsoft 示例,构建并启动它并在那里体验了 Aborted 状态。

    听起来很简单——我的开发笔记本电脑的简单重启解决了这个问题。下次我启动 Microsoft 示例时,它就可以工作了。

    【讨论】:

      【解决方案3】:

      我认为您需要尝试在设备上启用蓝牙,同时获得“已中止”状态。

      我为此添加了 LaunchBluetoothSettingsAsync() 方法。当 OnAdvertisementWatcherStopped 被触发且状态为 aborted 时调用它。

      public sealed partial class MainPage : Page
      {
      
          private BluetoothLEAdvertisementWatcher watcher = null;
          private IAsyncOperation<IUICommand> _bluetoothNotOnDialogOperation;
      
          public MainPage()
          {
              this.InitializeComponent();
      
              watcher = new BluetoothLEAdvertisementWatcher();
              watcher.ScanningMode = BluetoothLEScanningMode.Active;
      
              textBlock.Text = watcher.Status.ToString();
          }
      
          protected override void OnNavigatedTo(NavigationEventArgs e)
          {
              watcher.Received += OnAdvertisementReceived;
      
              watcher.Stopped += OnAdvertisementWatcherStopped;
          }
      
      
          private void StopButton_Click(object sender, RoutedEventArgs e)
          {
              watcher.Stop();
          }
      
          private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
          {
      
              await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
              {
                  textBlock.Text = "rcvd" + watcher.Status.ToString();
              });
          }
      
          private async void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
          {
              if (watcher .Status == BluetoothLEAdvertisementWatcherStatus.Aborted && _bluetoothNotOnDialogOperation == null)
              {
                  MessageDialog messageDialog = new MessageDialog(
                      "Do you wish to enable Bluetooth on this device?",
                      "Failed to start Bluetooth LE advertisement watcher");
      
                  messageDialog.Commands.Add(new UICommand("Yes",
                      async command => { await LaunchBluetoothSettingsAsync(); }));
      
                  messageDialog.Commands.Add(new UICommand("No",
                      command => { watcher.Stop(); }));
      
                  _bluetoothNotOnDialogOperation = messageDialog.ShowAsync();
              }
          }
      
          private void buttonStart_Click(object sender, RoutedEventArgs e)
          {
              watcher.Start();
              textBlock.Text = watcher.Status.ToString();
          }
          private void buttonStop_Click(object sender, RoutedEventArgs e)
          {
              watcher.Stop();
              textBlock.Text = watcher.Status.ToString();
          }
      
          private void buttonView_Click(object sender, RoutedEventArgs e)
          {
              textBlock.Text = watcher.Status.ToString();
          }
      
          private async Task LaunchBluetoothSettingsAsync()
          {
              await Launcher.LaunchUriAsync(new Uri("ms-settings-bluetooth:"));
          }
      }
      

      【讨论】:

      • 感谢德米特里的努力。
      • 我在用于开发的戴尔笔记本电脑(笔记本电脑支持蓝牙)上遇到了这个问题。启动修改后的应用程序后,蓝牙设置打开,我有蓝牙启动的信息。我提到我使用 BluetoothLEAdvertisementWatcher 创建了一个类,该类现在是另一个控制台应用程序的一部分,该应用程序正在成功扫描 BLE 设备(在同一台笔记本电脑上)。如果提供的程序在您的环境中正常运行,那么我在某处犯了一个明显的错误......或者这是一个硬件问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多