2017-01-24 115 views
1

每當我從UWP應用程序啓動BluetoothLEAdvertisementWatcher時,其狀態將中止。在控制檯應用程序中使用相同的功能是沒有問題的(包括必需的庫)。當我想與BLE設備配對時,我使用UWP應用程序中的DeviceWatcher而沒有問題。 OS是Win10,並且使用VS2015社區。UWP BLE廣告狀態中止

要ilustrate的問題,我做了包括在功能與藍牙的UWP項目:

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

有按鈕啓動,停止和視圖,TextBlock的用於上顯示的MainPage的BluetoothLEAdvertisementWatcher狀態。代碼提供:

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。按下開始按鈕後,觀察者被啓動,但狀態變爲異常終止,事件OnAdvertisementWatcherStopped被觸發(狀態仍然中止)。

有沒有解決這個問題的建議?或者可以做些額外的事情來澄清問題?

UPDATE

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

有在網絡上兩條建議:

  1. 啓用藍牙(建議由梅德第一個答案)

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

沒有提供結果。

補充說明:當Stopped的事件註冊被移除時, (// watcher.Stopped + = OnAdvertisementWatcherStopped;)的第一個結果是Started。下一個點擊按鈕View將顯示Aborted。對於一小段時間來說,結果會成功。

任何配置設置建議?

回答

1

我認爲你需要嘗試啓用設備上的藍牙,同時得到狀態「中止」。

我已經爲此添加了方法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:")); 
    } 
} 
+0

謝謝德米特里的努力。 – procrustes

+0

我在用於開發的dell筆記本電腦(筆記本電腦支持藍牙)上有此問題。啓動修改後的應用程序後,藍牙設置將打開,並且我有藍牙啓動信息。 我提到我創建的類與BluetoothLEAdvertisementWatcher這是現在另一個控制檯應用程序,其被成功地掃描(在同一膝上型)BLE設備的一部分。 如果提供的程序在你environement正常工作,那麼我犯了一個明顯的錯誤的地方......或者這是一個硬件問題。 – procrustes