private void AdvertisementWatcherReceivedHandler(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) { foreach (var manufacturerData in args.Advertisement.ManufacturerData) { const int appleCompanyId = 0x4c; if (manufacturerData.CompanyId != appleCompanyId) { continue; } BeaconAdvertisement advertisement; using (var dataReader = DataReader.FromBuffer(manufacturerData.Data)) { var subtype = dataReader.ReadByte(); if (subtype != 2) { return; } var subtypeLength = dataReader.ReadByte(); if (subtypeLength != 0x15) { return; } var a = dataReader.ReadInt32(); var b = dataReader.ReadInt16(); var c = dataReader.ReadInt16(); var d = new byte[8]; dataReader.ReadBytes(d); var uuid = new Guid(a, b, c, d); var major = dataReader.ReadUInt16(); var minor = dataReader.ReadUInt16(); var txPower = (sbyte)dataReader.ReadByte(); advertisement = new BeaconAdvertisement( beaconData: new BeaconData( uuid: uuid, major: major, minor: minor), txPower: txPower, rssi: args.RawSignalStrengthInDBm, timestamp: args.Timestamp); } lock (_syncRoot) { _advertisements.Add(advertisement); } } }
private void ExpirationTimerElapsedHandler(ThreadPoolTimer timer) { lock (_syncRoot) { _advertisements.RemoveAll(x => DateTimeOffset.Now - x.Timestamp > _expirationPeriod); if (_advertisements.Count == 0) { NearestBeacon = null; } else { NearestBeacon = _advertisements.MaxBy(x => x.Rssi); } } }
private void OnNearestBeaconChanged(BeaconAdvertisement e) { NearestBeaconChanged?.Invoke(this, e); }