Exemplo n.º 1
0
        private void DiscoverThread()
        {
            while (IsStarted)
            {
                var devices = BluetoothAdapter.DiscoverDevices();

                if (!IsStarted)
                {
                    return;
                }

                //Remove lost devices.
                foreach (var device in _discoveredServices.ToList())
                {
                    if (!devices.Exists(x => x.Address == device.DiscoveryInfo.Address && x.Name == device.DiscoveryInfo.Name))
                    {
                        _discoveredServices.Remove(device);
                        ServiceLost?.Invoke(this, new ResonanceDiscoveredServiceEventArgs <ResonanceBluetoothDiscoveredDevice, BluetoothDevice>(device));
                    }
                }

                //Add new devices.
                foreach (var device in devices)
                {
                    if (!_discoveredServices.Exists(x => x.DiscoveryInfo.Address == device.Address && x.DiscoveryInfo.Name == device.Name))
                    {
                        var newDevice = new ResonanceBluetoothDiscoveredDevice(device);
                        _discoveredServices.Add(newDevice);
                        ServiceDiscovered?.Invoke(this, new ResonanceDiscoveredServiceEventArgs <ResonanceBluetoothDiscoveredDevice, BluetoothDevice>(newDevice));
                    }
                }
            }
        }