2017-10-12 160 views
0

我試圖做一個程序,可以掃描BLE廣告。我一直在尋找Windows通用樣本,更確切地說是名爲BluetoothAdvertisement的樣本。我想製作一個簡單的UWP應用程序,它可以掃描BLE廣告並將它們顯示在列表框中。但是我的應用程序找不到任何東西,我完全迷失了。BLE廣告UWP應用程序

namespace BleDiscAdv2 
{ 

public sealed partial class MainPage : Page 
{ 
    // The Bluetooth LE advertisement watcher class is used to control and customize Bluetooth LE scanning. 
    private BluetoothLEAdvertisementWatcher watcher; 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     // Create and initialize a new watcher instance. 
     watcher = new BluetoothLEAdvertisementWatcher(); 

     //Set the in-range threshold to -70dBm. This means advertisements with RSSI >= -70dBm 
     //will start to be considered "in-range" 
     watcher.SignalStrengthFilter.InRangeThresholdInDBm = -70; 

     // Set the out-of-range threshold to -75dBm (give some buffer). Used in conjunction with OutOfRangeTimeout 
     // to determine when an advertisement is no longer considered "in-range" 
     watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -75; 

     // Set the out-of-range timeout to be 2 seconds. Used in conjunction with OutOfRangeThresholdInDBm 
     // to determine when an advertisement is no longer considered "in-range" 
     watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000); 

    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     // Attach a handler to process the received advertisement. 
     // The watcher cannot be started without a Received handler attached 
     watcher.Received += OnAdvertisementReceived; 
    } 

     private void btStart_Click(object sender, RoutedEventArgs e) 
    { 
     watcher.Start(); 
    } 

    private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) 
    { 
     DateTimeOffset timestamp = eventArgs.Timestamp; 
     string localName = eventArgs.Advertisement.LocalName; 

     await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => 
     { 
      lbModtaget.Items.Add("Name of device: " + localName + "\t" + "Time for advertisement: " + timestamp.ToString("hh\\:mm\\:ss\\.fff")); 
     }); 
    } 
} 
} 

有人能告訴我什麼是錯的嗎? 我是BLE的新手,我一直沒有編寫代碼。

問候 基督教

回答

0

但我的應用程序無法找到任何東西都和我完全失去了。

  • 請確保您的應用程序有能夠在Package.appxmanifest藍牙功能。有關詳細信息,請參閱Basic Setup
  • 請確保運行設備的藍牙無線電已打開且可用。
  • 有設備是廣告和符合過濾器。您可以在另一臺設備上運行Bluetooth advertisement official sample的方案2以確保。

通過在我身邊測試,您的代碼片段可以很好地掃描BLE廣告。在您的代碼片段中,您沒有收聽觀察者的事件句柄,該事件句柄用於通知應用程序Bluetooth LE掃描廣告已被應用程序取消或由於錯誤而被取消或中止。如果觀察者被強制停止,它將不會得到任何廣告。

您可以添加Stopped事件句柄來檢查是否有BluetoothError

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    // Attach a handler to process the received advertisement. 
    // The watcher cannot be started without a Received handler attached 
    watcher.Received += OnAdvertisementReceived; 
    watcher.Stopped += OnAdvertisementWatcherStopped; 
} 

private async void OnAdvertisementWatcherStopped(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementWatcherStoppedEventArgs args) 
{ 
    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => 
    { 
     txtresult.Text = string.Format("Watcher stopped or aborted: {0}", args.Error.ToString()); 
    }); 
} 

例如,RadioNotAvailable可能由運行裝置不啓用藍牙引起,OtherError可以通過藍牙能力引起的不啓用。如果觀察者沒有停下來,並且有廣告,你的應用程序應該工作。

+0

錯誤'DisabledByUser'是什麼意思?當我嘗試運行前景觀察器示例時,我得到了這個結果。 – TedMilker

+0

爲他人回答我自己的問題:我在「設置」 - >「隱私」 - >「其他設備」中關閉了「與未配對設備進行通信」 – TedMilker