Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
 public Task DisconnectDeviceAsync(IBTDevice device)
 {
     centralManager.CancelPeripheralConnection(device.NativeDevice as CBPeripheral);
     return(Task.CompletedTask);
 }