BLE advertisement UWP application

时间:2017-10-12 09:46:40

标签: c# uwp bluetooth-lowenergy advertisement

Im trying to make a program that can scan for BLE advertisements. I have been looking at the Windows-universal-samples, more precisely the sample called BluetoothAdvertisement. I want to make a simple UWP application that can scan for BLE advertisements and show them in a listbox. But my application can't find anything at all and I'm totally lost.

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"));
        });
    }
}
}

Can someone tell me what is wrong? I'm new to BLE and I haven't been coding for a while.

Best regards Christian

1 个答案:

答案 0 :(得分:0)

  

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

  • 请确保您的应用已在Package.appxmanifest中启用了蓝牙功能。有关详细信息,请参阅Basic Setup
  • 请确保运行设备的蓝牙无线电已开启且可用。
  • 其他设备正在做广告并符合过滤条件。您可以在另一台设备上运行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可能是由于蓝牙功能未启用而引起的。如果观察者没有停止并且有广告,那么您的应用应该有效。