2017-10-12 120 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