我想定期扫描信标,例如每两秒钟扫描一次。 我试图设置SignalStrengthFilter频率的SamplingInterval,但它不起作用。
所以,我尝试使用Timer自己实现这种行为。我实现了一个例程,收集信标信息两秒钟,然后返回在此期间看到的信标列表。但它无法正常工作。
我使用每隔0.5秒传输一次的信标,两秒钟后扫描器没有看到正确数量的信标。每次看到的信标数量都是可变的。
我认为问题是Windows 10的扫描间隔和扫描窗口。 有没有办法调整它们?
这是主要代码:
public void startDecoding()
{
watcher.Received += OnAdvertisementReceived;
watcher.Stopped += OnAdvertisementWatcherStopped;
watcher.Start();
t = ThreadPoolTimer.CreatePeriodicTimer((t) =>
{
timerCallback();
}, TimeSpan.FromSeconds(2)); //every two seconds for example
}
private void timerCallback()
{
List<Beacon> tmp;
lock(_sync)
{
tmp = new List<iBeaconData>(beacons);
beacons.Clear();
}
bc.onBeaconDetected(tmp);
}
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
// The timestamp of the event
DateTimeOffset timestamp = eventArgs.Timestamp;
// The type of advertisement
BluetoothLEAdvertisementType advertisementType = eventArgs.AdvertisementType;
// The received signal strength indicator (RSSI)
Int16 rssi = eventArgs.RawSignalStrengthInDBm;
// The local name of the advertising device contained within the payload, if any
string localName = eventArgs.Advertisement.LocalName;
// Get iBeacon specific data
var beaconData = eventArgs.Advertisement.iBeaconParseAdvertisement(eventArgs.RawSignalStrengthInDBm);
if (beaconData == null)
return;
lock(_sync)
{
var existing = beacons.Where(b => b.UUID.ToString() == beaconData.UUID.ToString() && b.Major == beaconData.Major && b.Minor == beaconData.Minor).FirstOrDefault();
if (existing != null)
{
var idx = beacons.IndexOf(existing);
beacons.RemoveAt(idx);
}
beacons.AddSorted(beaconData);
}
}