当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Bluetooth LE device as proximity sensor, BluetoothLEAdvertisementWatcher cyclically stops listening for advertisements

问题描述:

In the way of learning BLE programming using C#, I'm trying to write a winform application that should detec BLE devices and use them as proximity sensors. For this purpose I've applied the BluetoothAdvertisementWatcher class, filtering the advertisements with a provided signal strength. The problem is the detection seems to be quite crappy... Even with the device at no distance from my PC, the listener often returns a RSSI value of -127, wich stands for "no detection" as I've understood. I've tried setting the OutOfRangeTimeout to 10 seconds and, even if things get better, problems are still heavy. The major issue is the detection can flows without interruption just for about a bunch of seconds, then the debugger signals me a thread exiting with code 0, and the listener can't receive new advertisements (with the device ever aside the pc) for even 30-40 seconds, then it restarts and so it loops.

Do you know if is this how it is intended to work or if am I missing something? Is there a way, eventually, to intercept this interruption and immediately restart the listener?

This is how I initialize the watcher:

public BLEScanner(short maxDBRange, TimeSpan outOfRangeTimeout) {

this.InRange = maxDBRange;

this.OutOfRange = (short)(this.InRange + BLEScanner.BUFFER_RANGE);

this.OutOfRangeTimeout = outOfRangeTimeout;

this.watcher = new BluetoothLEAdvertisementWatcher();

this.watcher.SignalStrengthFilter.InRangeThresholdInDBm = this.InRange;

this.watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = this.OutOfRange;

this.watcher.SignalStrengthFilter.OutOfRangeTimeout = this.OutOfRangeTimeout;

this.watcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromSeconds(1);

this.watcher.ScanningMode = BluetoothLEScanningMode.Active;

}

While this is the event handler:

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) {

var deviceAddress = eventArgs.BluetoothAddress;

BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(deviceAddress);

UpdateUiDelegate update = new UpdateUiDelegate(

(dev, args) => {

if (eventArgs.RawSignalStrengthInDBm == -127) {

this.form.spyLabel.BackColor = System.Drawing.Color.Red;

this.form.nameLabel.Text = "(none)";

this.form.addressLabel.Text = "(none)";

this.form.rssiLabel.Text = "(none)";

this.form.connectedLabel.Text = device.ConnectionStatus.ToString();

} else {

this.form.spyLabel.BackColor = System.Drawing.Color.Green;

this.form.nameLabel.Text = device.Name;

this.form.addressLabel.Text = device.BluetoothAddress.ToString();

this.form.rssiLabel.Text = eventArgs.RawSignalStrengthInDBm.ToString();

this.form.connectedLabel.Text = device.ConnectionStatus.ToString();

}

});

this.form.Invoke(update, device, eventArgs);

Console.Write(eventArgs.RawSignalStrengthInDBm + " ");

}

Any help is appreciated!

分享给朋友:
您可能感兴趣的文章:
随机阅读: