簡體   English   中英

BLE廣告UWP應用

[英]BLE advertisement UWP application

我正在嘗試制作一個可以掃描BLE廣告的程序。 我一直在看Windows通用示例,更確切地說是名為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的新手,已經有一段時間沒有編碼了。

問候克里斯蒂安

但是我的應用程序根本找不到任何東西,我完全迷路了。

  • 請確保您的應用在Package.appxmanifest啟用了藍牙功能。 有關詳細信息,請參見基本設置
  • 請確保正在運行的設備的藍牙無線電已打開並且可用。
  • 有設備在做廣告並符合過濾條件。 您可以在另一台設備上運行Bluetooth廣告官方示例的方案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可能是由於未啟用藍牙功能引起的。 如果觀察者沒有停止並且有廣告,則您的應用應該可以運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM