Tom
Tom

Reputation: 4069

Using BluetoothLEAdvertisementWatcher in background application?

I try to scan BLE devices in my Windows 10 IOT background (headless) app running on Raspberry PI 3.

I also tried to use BluetoothLEAdvertisementWatcher in a headed app (with UI) on the same RaspBerry PI machine and it worked.

My headless app is the simplest as it can be:

public sealed class StartupTask : IBackgroundTask
{
    private readonly BluetoothLEAdvertisementWatcher _bleWatcher = 
        new BluetoothLEAdvertisementWatcher();

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        _bleWatcher.Received += _bleWatcher_Received;
        _bleWatcher.ScanningMode = BluetoothLEScanningMode.Active;
        _bleWatcher.Start();
    }

    private void _bleWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
    {

    }
}

_bleWatcher_Received is never hit. Capabilities are set (Bluetooth, Internet, Proximity).

What is the problem? What do I miss?

Upvotes: 1

Views: 163

Answers (1)

Rita Han
Rita Han

Reputation: 9720

You app shuts down when the run method completes. That's why _bleWatcher_Received is never hit.

To prevent you app from exiting you need call the “GetDeferral” method like this:

public void Run(IBackgroundTaskInstance taskInstance)
{
    deferral = taskInstance.GetDeferral();

    //YOUR CODE HERE
}

For more information please reference "Developing Background Applications".

Upvotes: 1

Related Questions