public static async Task <IButtplugDeviceImpl> Create(IButtplugLogManager aLogManager,
                                                              BluetoothLEProtocolConfiguration aConfig,
                                                              IDevice aDevice)
        {
            var device = new XamarinBluetoothDeviceInterface(aLogManager, aDevice);
            await device.InitializeDevice(aConfig).ConfigureAwait(false);

            return(device);
        }
Ejemplo n.º 2
0
        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;
            }
            string advertName = e.Device.GetPropValue <string>("BluetoothDevice.Name");

            var advertGUIDs = new List <Guid>();

            advertGUIDs.Add(e.Device.Id);
            var btAddr = e.Device.GetPropValue <string>("BluetoothDevice.Address");

            BpLogger.Trace($"Got BLE Advertisement for device: {advertName} / {btAddr}");
            if (_seenAddresses.Contains(btAddr))
            {
                BpLogger.Trace($"Ignoring advertisement for already connecting device: {btAddr}");
                return;
            }
            _seenAddresses.Add(btAddr);
            BpLogger.Trace("BLE device found: " + advertName);

            // We always need a name to match against.
            if (String.IsNullOrEmpty(advertName))
            {
                return;
            }

            // todo Add advertGUIDs back in. Not sure that ever really gets used though.
            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}");
            }
        }