public Task <bool> Connect(OWBaseBoard board, CancellationToken cancellationToken) { _requestingDisconnect = false; _reconnecting = false; _connectionCompletionSource = new TaskCompletionSource <bool>(); if (board.NativePeripheral is CBPeripheral peripheral) { _board = board; _peripheral = peripheral; _peripheral.WeakDelegate = this; var options = new PeripheralConnectionOptions() { NotifyOnDisconnection = true, #if __IOS__ NotifyOnConnection = true, NotifyOnNotification = true, #endif }; _centralManager.ConnectPeripheral(peripheral, options); } else { _connectionCompletionSource.SetResult(false); } return(_connectionCompletionSource.Task); }
/// <summary> /// Attempts to connect the connection's remote terminus. /// </summary> /// <returns>The async.</returns> public bool Connect(bool passive = false) { lock (locker) { if (EConnectionState.Disconnected != connectionState) { return(false); } try { connectionState = EConnectionState.Connecting; DateTime start = DateTime.Now; PeripheralConnectionOptions options = new PeripheralConnectionOptions(); options.NotifyOnConnection = true; options.NotifyOnDisconnection = true; options.NotifyOnNotification = true; centralDelegate.centralManager.ConnectPeripheral(device, options); return(true); } catch (Exception e) { Log.E(this, "Failed to connect", e); Disconnect(); return(false); } } }
void Reconnect() { // Disconnect was because board lost connection. BoardReconnecting?.Invoke(); _reconnecting = true; var options = new PeripheralConnectionOptions() { NotifyOnDisconnection = true, #if __IOS__ NotifyOnConnection = true, NotifyOnNotification = true, #endif }; _centralManager.ConnectPeripheral(_peripheral, options); }
public void connect(string deviceAddress) { if (connectedPeripheral != null) { disconnect(); } var dev = devices.FirstOrDefault((item) => item.Identifier.ToString().ToUpper().Equals(deviceAddress.ToUpper())); if (dev == null) { return; } connectedPeripheral = dev; // Make sure we have a reference to this so that it can be canceled while connection is pending var options = new PeripheralConnectionOptions(); options.NotifyOnConnection = true; options.NotifyOnDisconnection = true; options.NotifyOnNotification = true; centralManager.ConnectPeripheral(dev, options); }