private void OnBeaconReceived(object sender, BeaconPacketArgs e)
 {
     if (_invokeAction != null)
     {
         _invokeAction(() => { ReceivedBeacon(e.Data); });
     }
     else
     {
         ReceivedBeacon(e.Data);
     }
 }
Example #2
0
        private void ScanCallback_OnAdvertisementPacketReceived(object sender, BeaconPacketArgs e)
        {
#if DEBUG
            lock (_bluetoothDevicesListLock)
            {
                if (!_bluetoothDevicesWeHaveReceivedBeaconsFrom.Contains(e.Data.BluetoothAddress))
                {
                    SystemDebug.WriteLine($"Beacon received: {e.Data.BluetoothAddress:X} {e.Data.Region.Uuid}", LogTag);
                    _bluetoothDevicesWeHaveReceivedBeaconsFrom.Add(e.Data.BluetoothAddress);
                }
            }
#endif

            if (e.Data.Region.Uuid.Replace("-", String.Empty).ToLower() != _beaconRegion.Uuid.Replace("-", String.Empty).ToLower())
            {
                return;
            }

            // matched UUID, this is the region we are looking for, set name
            e.Data.Region.RegionName = _beaconRegion.RegionName;

            lock (_lock)
            {
                try
                {
                    _regionExitedCancellationTokenSource.Cancel();
                    _regionExitedCancellationTokenSource.Dispose();
                }
                catch (Exception) { }

                _regionExitedCancellationTokenSource = new CancellationTokenSource();
                _regionExitedCancellationToken       = _regionExitedCancellationTokenSource.Token;

                // start task to kick off region exit if no beacon is received for a period of time
                _regionExitedWatchdogTask = Task.Run(WaitDelayAndCheckForRegionExited(_regionExitedCancellationToken), _cancellationToken);

                // send region entered if region was exited and new beacon received,
                // or if this is the first beacon we received
                if (_wasRegionExitTriggered || !_wasFirstRegionEnterTriggered)
                {
                    _wasFirstRegionEnterTriggered = true;
                    BeaconRegionEntered?.Invoke(this, new BeaconPacketArgs(e.Data));
                }

                BeaconReceived?.Invoke(this, new BeaconPacketArgs(e.Data));

                _lastBeaconReceivedDateTime = DateTime.Now;

                _wasRegionExitTriggered = false;
            }
        }