Christian
Christian

Reputation: 75

BLE advertisement UWP application

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

Upvotes: 0

Views: 2327

Answers (1)

Sunteen Wu
Sunteen Wu

Reputation: 10627

But my application can't find anything at all and I'm totally lost.

  • Please ensure that your app has enable Bluetooth capability in the Package.appxmanifest. See Basic Setup for details.
  • Please ensure the Bluetooth radio of running device was turn on and available.
  • There're devices are advertising and meet the filter. You can run the Scenario 2 of the Bluetooth advertisement official sample on another device to ensure that.

By testing on my side, your code snippet can scan the BLE advertisements well. In your code snippet, you didn't listen to the Stopped event handle of the watcher which is for notification to the app that the Bluetooth LE scanning for advertisements has been cancelled or aborted either by the app or due to an error. If the watcher is force stopped it will not get any advertisements.

You can add the Stopped event handle to check if there is a 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());
    });
}

For example, RadioNotAvailable may be caused by the running device is not enable the Bluetooth, OtherError may be caused by Bluetooth capability doesn't enabled. If the watcher is not stopped and there're advertisements, your app should work.

Upvotes: 1

Related Questions