コード例 #1
0
        /// <summary>
        /// Analyze the received Bluetooth LE advertisement, and either add a new unique
        /// beacon to the list of known beacons, or update a previously known beacon
        /// with the new information.
        /// </summary>
        /// <param name="btAdv">Bluetooth advertisement to parse, as received from
        /// the Windows Bluetooth LE API.</param>
        private void ReceivedBeacon(BeaconPacket btAdv)
        {
            if (btAdv == null)
            {
                return;
            }

            // Beacon was not yet known - add it to the list.
            var newBeacon = new Beacon(btAdv);

            BeaconReceived?.Invoke(this, newBeacon);
        }
コード例 #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;
            }
        }