C# 超宽带应用

C# 超宽带应用,c#,uwp,bluetooth-lowenergy,ads,C#,Uwp,Bluetooth Lowenergy,Ads,我想做一个程序,可以扫描的广告。我一直在看Windows universal示例,更确切地说是名为BluetoothAdvertision的示例。我想做一个简单的UWP应用程序,可以扫描BLE广告并在列表框中显示它们。但是我的应用程序找不到任何东西,我完全迷路了 namespace BleDiscAdv2 { public sealed partial class MainPage : Page { // The Bluetooth LE advertisement watcher c

我想做一个程序,可以扫描的广告。我一直在看Windows universal示例,更确切地说是名为BluetoothAdvertision的示例。我想做一个简单的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新手,已经有一段时间没有编写代码了

致意 基督教徒

但是我的应用程序找不到任何东西,我完全迷路了

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"));
        });
    }
}
}
  • 请确保您的应用程序在
    软件包.appxmanifest
    中启用了蓝牙功能。有关详细信息,请参阅
  • 请确保运行设备的蓝牙收音机已打开且可用
  • 有一些设备正在做广告并符合过滤器的要求。您可以在另一台设备上运行的场景2,以确保
通过在我这边进行测试,您的代码片段可以很好地扫描BLE广告。在您的代码段中,您没有侦听watcher的事件句柄,该事件句柄用于通知应用程序Bluetooth LE扫描广告已被应用程序取消或中止,或者由于错误。如果强制停止观察者,它将不会得到任何广告

您可以添加
Stopped
事件句柄来检查是否存在错误


例如,
RadioNotAvailable
可能是由于正在运行的设备未启用蓝牙,
OtherError
可能是由于未启用蓝牙功能造成的。如果观察者没有停止,并且有广告,你的应用程序应该可以工作。

错误
DisabledByUser
是什么意思?当我尝试运行前台观察者示例时,我得到了这一点。为其他人回答我自己的问题:我在设置->隐私->其他设备中关闭了“与未配对设备通信”