Search code examples
uwptimeoutconnectionbluetooth-lowenergyads

Difference between DeviceWatcher and BluetoothLEAdvertisementWatcher class and Timeout property


I had built a windows 10 application that watch for a specific advertisement and connect with it.

I used the example "BluetoothLE" from "Windows-universal-samples" with a little bit edits. In this example they used "DeviceWatcher " Class.

But I'm now trying to add timeout to disconnect after a specific period. I found this property: OutOfRangeTimeout which belongs to BluetoothLEAdvertisementWatcher Class.

Now I'm confused what are the differences between "DeviceWatcher" AND "BluetoothLEAdvertisementWatcher"?

And is there a way to add "timeout" using DeviceWatcher Class?


Solution

  • There is a big difference between the "DeviceWatcher " Class and the BluetoothLEAdvertisementWatcher Class. The deviceWacher is on system-level; meaning You have to add the device first in Windows Settings before it can be found. This also means that the device is always present in the Windows.Devices.Enumeration, unless you remove it from Windows Settings. It takes a long time for the device watcher to complete. Also to make a well-working application you will have to implement all of the added, removed, and updated events to be notified when there are device additions, removals or updates. If an app handles only the added event, it will not receive an update if a device is added to the system after the initial device enumeration completes.

    The BluetoothLEAdvertisementWatcher only listens to BLE-devices that send advertisements and that are in reach. If you set the scanMode to active, the watcher tries to get all information on the device, like name, capabilities, ble-Address, Services advertisement payload, etc. Setting it for scanMode passive, the watcher listens only for the device using name and Ble-address, which is faster.

    The scanning time depends only on the number of devices in reach and their advertising interval. The OutOfRangeTimeout refers to the BluetoothSignalStrengthFilter that decides if a device is out of reach.

    As for the timeOut to disconnect:

    Bluetooth LE devices APIs do not provide direct control over the connection to the device. Instead, the Bluetooth-stack will disconnect the device after a one-second timeout if there are no outstanding references to it. So you have to use a timer to set your timeOut and close and dispose of the device!