public void Start()
        {
            if (_watcher == null)
            {
                var watcher = new BluetoothLEAdvertisementWatcher()
                {
                    ScanningMode = BluetoothLEScanningMode.Active
                };
                watcher.SignalStrengthFilter = new BluetoothSignalStrengthFilter()
                {
                    SamplingInterval = TimeSpan.Zero
                };

                GlobalCounters.IncrementWatchersCreated();

                watcher.Received += WatcherReceived;
                watcher.Stopped  += WatcherStopped;

                _watcher = watcher;

                Log("Watcher created, has status " + _watcher.Status);
            }

            _watcher.Start();
            IsStopped.Reset();
            GlobalCounters.IncrementWatchersStarted();
            Console.WriteLine("Watcher started, has status " + _watcher.Status);
        }
        private async void WatcherReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            //  Noticed a build warning here, WatcherReceived isn't waiting on this async operation to finish.
            ExceptionLogger.Run(async() =>
            {
                //AssertSameThreadAndContext();
                GlobalCounters.IncrementAdvertisementsSeen();

                var address = args.BluetoothAddress;

                if (_devices.ContainsKey(args.BluetoothAddress))
                {
                    return;
                }

                var device = new Device(address,
                                        GlobalCounters.IncrementDevicesConnected,
                                        () =>
                {
                    GlobalCounters.IncrementDevicesClosed();
                    _devices.Remove(address);
                });

                _devices[address] = device;

                await device.Start();
            });
        }
Exemple #3
0
        private async void WatcherReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            //  Noticed a build warning here, WatcherReceived isn't waiting on this async operation to finish.
            ExceptionLogger.Run(async() =>
            {
                //AssertSameThreadAndContext();
                GlobalCounters.IncrementAdvertisementsSeen();

                if (_devices.ContainsKey(args.BluetoothAddress))
                {
                    return;
                }

                _devices[args.BluetoothAddress] = null;

                var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

                if (device == null)
                {
                    GlobalCounters.IncrementFailedCreate();
                    return;
                }
                GlobalDeviceNameTracking.ReportName(device.Name, device.BluetoothAddress);

                device.ConnectionStatusChanged += ConnectionStatusChanged;

                _devices[args.BluetoothAddress] = device;

                GlobalCounters.IncrementDevicesCreated();
            });
        }
Exemple #4
0
        private void WatcherStopped(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementWatcherStoppedEventArgs args)
        {
            BluetoothError a = args.Error;

            //AssertSameThreadAndContext();
            GlobalCounters.IncrementWatchersClosed();
        }
Exemple #5
0
        public void Start()
        {
            if (_watcher != null)
            {
                throw new Exception("Should only be started once");
            }

            var watcher = new BluetoothLEAdvertisementWatcher()
            {
                ScanningMode = BluetoothLEScanningMode.Active
            };

            watcher.SignalStrengthFilter = new BluetoothSignalStrengthFilter()
            {
                SamplingInterval = TimeSpan.Zero
            };

            GlobalCounters.IncrementWatchersCreated();

            var received = new Subject <Tuple <BluetoothLEAdvertisementReceivedEventArgs, BluetoothError?> >();

            watcher.Received += (sender, args) => received.OnNext(new Tuple <BluetoothLEAdvertisementReceivedEventArgs, BluetoothError?>(args, null));
            watcher.Stopped  += (sender, args) => received.OnNext(new Tuple <BluetoothLEAdvertisementReceivedEventArgs, BluetoothError?>(null, args.Error));

            watcher.Start();

            _watcher = watcher;
        }
Exemple #6
0
        private void ConnectionStatusChanged(BluetoothLEDevice sender, object args)
        {
            ExceptionLogger.Run(async() =>
            {
                if (!_devices.ContainsKey(sender.BluetoothAddress))
                {
                    throw new Exception("\nReceived connection status change for non-created device.");
                }

                var connectionStatus = sender.ConnectionStatus;

                if (connectionStatus == BluetoothConnectionStatus.Connected)
                {
                    GlobalCounters.IncrementDevicesConnected();
                }
                else if (connectionStatus == BluetoothConnectionStatus.Disconnected)
                {
                    GlobalCounters.IncrementDevicesClosed();
                    var device = _devices[sender.BluetoothAddress];
                    _devices.Remove(sender.BluetoothAddress);
                    device.Dispose();
                }
                else
                {
                    throw new Exception("unrecognized bluetooth connection status: " + connectionStatus);
                }
            });
        }
        private void WatcherStopped(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementWatcherStoppedEventArgs args)
        {
            //AssertSameThreadAndContext();
            GlobalCounters.IncrementWatchersClosed();

            Log($"Watcher stopped for reason {args.Error}, has status {_watcher.Status}.");
            IsStopped.Set();
        }