コード例 #1
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();

                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();
            });
        }
コード例 #2
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);
                }
            });
        }