private void BtDeviceDiscovered(object sender, EventArgs.BTDeviceEventArgs e) { if (e.Device != null) { IBTDevice device = e.Device; Console.WriteLine($"Discovered a device : {device.Id.ToString()}"); _bluetoothCentral.DeviceConnected += BtDeviceConnected; _bluetoothCentral.ConnectToDeviceAsync(device); } }
public Task ConnectToDeviceAsync(IBTDevice device, CancellationToken cancellationToken = default) { if (device != null) { centralManager.ConnectPeripheral(device.NativeDevice as CBPeripheral, new PeripheralConnectionOptions()); cancellationToken.Register(() => { Console.WriteLine("Canceling the connect attempt"); centralManager.CancelPeripheralConnection(device.NativeDevice as CBPeripheral); }); } return(Task.FromResult(true)); }
/// <summary> /// Checks the list of discovered devices and connects to the first /// </summary> /// <returns>True if a device was found</returns> private bool ConnectDiscoveredDevice() { this.StopDiscovery(); if (this.discoveredDevices.Count > 0) { this.connectedDevice = this.discoveredDevices.First().Value; this.connectedDevice.NotifyCanSendImages += () => { this.NotifyCanSendImages?.Invoke(); }; this.connectedDevice.NotifyDisconnected += () => { this.NotifyDisconnected?.Invoke(); }; this.connectedDevice.OnConnected(); return(true); } return(false); }
public Task DisconnectDeviceAsync(IBTDevice device) { centralManager.CancelPeripheralConnection(device.NativeDevice as CBPeripheral); return(Task.CompletedTask); }