BLE 广告 UWP 应用程序
BLE advertisement UWP application
我正在尝试制作一个可以扫描 BLE 广告的程序。我一直在查看 Windows-universal-samples,更准确地说是名为 BluetoothAdvertisement 的示例。我想制作一个简单的 UWP 应用程序,它可以扫描 BLE 广告并将它们显示在列表框中。但是我的应用程序根本找不到任何东西,我完全迷路了。
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 的新手,我已经有一段时间没有编码了。
此致
克里斯蒂安
But my application can't find anything at all and I'm totally lost.
- 请确保您的应用已在
Package.appxmanifest
中启用蓝牙功能。有关详细信息,请参阅 Basic Setup。
- 请确保 运行ning 设备的蓝牙无线电已打开且可用。
- 有设备在做广告,符合过滤条件。您可以在另一台设备上 运行 Bluetooth advertisement official sample 的场景 2 来确保这一点。
通过我这边的测试,你的代码片段可以很好地扫描BLE广告。在您的代码片段中,您没有收听观察者的 Stopped 事件句柄,该事件句柄用于通知应用程序蓝牙 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
可能是蓝牙能力没有开启造成的。如果观察者没有停止并且有广告,您的应用应该可以运行。
我正在尝试制作一个可以扫描 BLE 广告的程序。我一直在查看 Windows-universal-samples,更准确地说是名为 BluetoothAdvertisement 的示例。我想制作一个简单的 UWP 应用程序,它可以扫描 BLE 广告并将它们显示在列表框中。但是我的应用程序根本找不到任何东西,我完全迷路了。
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 的新手,我已经有一段时间没有编码了。
此致 克里斯蒂安
But my application can't find anything at all and I'm totally lost.
- 请确保您的应用已在
Package.appxmanifest
中启用蓝牙功能。有关详细信息,请参阅 Basic Setup。 - 请确保 运行ning 设备的蓝牙无线电已打开且可用。
- 有设备在做广告,符合过滤条件。您可以在另一台设备上 运行 Bluetooth advertisement official sample 的场景 2 来确保这一点。
通过我这边的测试,你的代码片段可以很好地扫描BLE广告。在您的代码片段中,您没有收听观察者的 Stopped 事件句柄,该事件句柄用于通知应用程序蓝牙 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
可能是蓝牙能力没有开启造成的。如果观察者没有停止并且有广告,您的应用应该可以运行。