private async void _adapter_DeviceAdvertised(object sender, Plugin.BLE.Abstractions.EventArgs.DeviceEventArgs e) { if (e?.Device == null) { BpLogger.Debug("Null BLE advertisement received: skipping"); return; } var advertName = e.Device.Name; var btAddr = e.Device.Id.ToString(); BpLogger.Trace($"Got BLE Advertisement for device: {advertName} / {btAddr}"); if (_seenAddresses.Contains(btAddr)) { BpLogger.Trace($"Ignoring advertisement for already connecting device: {btAddr}"); //return; } else { _seenAddresses.Add(btAddr); } Debug.WriteLine("BLE device found: " + advertName); // We always need a name to match against. if (string.IsNullOrEmpty(advertName)) { return; } var deviceCriteria = new BluetoothLEProtocolConfiguration(advertName); var deviceFactory = DeviceConfigurationManager.Manager.Find(deviceCriteria); // If we don't have a protocol to match the device, we can't do anything with it. if (deviceFactory == null || !(deviceFactory.Config is BluetoothLEProtocolConfiguration bleConfig)) { BpLogger.Debug($"No usable device factory available for {advertName}."); return; } // If a device is turned on after scanning has started, windows seems to lose the device // handle the first couple of times it tries to deal with the advertisement. Just log the // error and hope it reconnects on a later retry. IButtplugDeviceImpl bleDevice = null; IButtplugDevice btDevice = null; try { await _adapter.ConnectToDeviceAsync(e.Device); bleDevice = await XamarinBluetoothDeviceInterface.Create(LogManager, bleConfig, e.Device).ConfigureAwait(false); btDevice = await deviceFactory.CreateDevice(LogManager, bleDevice).ConfigureAwait(false); InvokeDeviceAdded(new DeviceAddedEventArgs(btDevice)); } catch (Exception ex) { if (btDevice != null) { btDevice.Disconnect(); } else { bleDevice?.Disconnect(); } BpLogger.Error( $"Cannot connect to device {advertName} {btAddr}: {ex.Message}"); } }