簡體   English   中英

UWP BLE 廣告狀態中止

[英]UWP BLE advertising status is aborted

每當我從 UWP 應用程序啟動 BluetoothLEAdvertisementWatcher 時,它的狀態都是中止的。 在控制台應用程序中使用相同的功能是沒有問題的(包括所需的庫)。 當我想與 BLE 設備配對時,我使用 UWP 應用程序中的 DeviceWatcher 沒有問題。 操作系統為Win10,使用VS2015 Community。

為了說明這個問題,我做了一個 UWP 項目,其中包含藍牙功能:

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

有按鈕 Start、Stop 和 View,以及用於在 MainPage 上顯示 BluetoothLEAdvertisementWatcher 狀態的 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)。

有什么克服這個問題的建議嗎? 或者可以做些什么來澄清問題?

更新

該應用程序在不同的筆記本電腦上執行。 結果是一樣的,所以不是硬件問題。

web上有兩個推薦:

  1. 啟用藍牙(德米特里在第一個答案中建議)

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

沒有提供結果。

附加說明:當 Stopped 的事件注冊被刪除時,(// watcher.Stopped += OnAdvertisementWatcherStopped;) 第一個結果是 Started。 下一次單擊按鈕 View 將顯示 Aborted。 在很短的時間內,結果成功地有效。

任何配置設置建議?

這個線程幫助了我。

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

解決方案是為 uwp 應用啟用“與設備同步”權限(設置->隱私->其他設備)。

我遇到了與 OP 類似的問題。 GattServiceProvider.StartAdvertising()之后,屬性GattServiceProvider.AdvertisementStatus返回Aborted 我嘗試了以上所有建議,但均未成功。

接下來,我從https://github.com/microsoft/Windows-universal-samples/tree/main/Samples/BluetoothLE下載了一個官方的 Microsoft 示例,構建並啟動它並在那里也經歷了Aborted state。

聽起來微不足道——簡單地重啟我的開發筆記本電腦就解決了這個問題。 下次我啟動 Microsoft 示例時它起作用了。

我認為您需要嘗試在設備上啟用藍牙,同時獲得“中止”狀態。

我為此添加了方法 LaunchBluetoothSettingsAsync() 。 當 OnAdvertisementWatcherStopped 被觸發且狀態中止時調用它。

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:"));
    }
}

我也有同樣的問題。 軟件是如何控制的? 我現在必須重新啟動計算機才能解決問題

  1. 我的電腦很容易重現這個問題,並快速打開/關閉系統藍牙。

  2. 然后啟動我的程序。獲取觀察者狀態。 代碼:

     BluetoothLEAdvertisementWatcher watcher; watcher.Start(); while (true) { BluetoothLEAdvertisementWatcherStatus status= watcher.Status(); // std::wcout << status << std::endl; }
  3. Output 結果:

    Created Stopping Started Aborted Aborted Aborted Aborted......

這是參考圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM