private void OnBLEDisconnected() { try { foreach (var chan in ButtonChannels.Values) { _flicClient.RemoveConnectionChannel(chan); } _flicClient.Disconnect(); _flicClient = null; DebugEx.TraceLog("FLIC: InitFlicClient from OnBLEDisconnected"); _InitFlicClient(); } catch (Exception ex) { DebugEx.Assert(ex); } }
private async void btnConnectDisconnect_Click(object sender, EventArgs e) { if (_flicClient == null) { btnConnectDisconnect.Enabled = false; try { _flicClient = await FlicClient.CreateAsync(txtServer.Text); } catch (Exception ex) { MessageBox.Show("Connect failed: " + ex.Message); btnConnectDisconnect.Enabled = true; return; } btnConnectDisconnect.Text = "Disconnect"; btnConnectDisconnect.Enabled = true; btnAddNewFlic.Text = "Add new Flic"; btnAddNewFlic.Enabled = true; _flicClient.BluetoothControllerStateChange += (o, args) => Invoke((MethodInvoker) delegate { lblBluetoothStatus.Text = "Bluetooth controller status: " + args.State.ToString(); }); _flicClient.NewVerifiedButton += (o, args) => Invoke((MethodInvoker) delegate { GotButton(args.BdAddr); }); _flicClient.GetInfo((bluetoothControllerState, myBdAddr, myBdAddrType, maxPendingConnections, maxConcurrentlyConnectedButtons, currentPendingConnections, currentlyNoSpaceForNewConnection, verifiedButtons) => Invoke((MethodInvoker) delegate { lblBluetoothStatus.Text = "Bluetooth controller status: " + bluetoothControllerState.ToString(); foreach (var bdAddr in verifiedButtons) { GotButton(bdAddr); } })); await Task.Run(() => _flicClient.HandleEvents()); // HandleEvents returns when the socket has disconnected for any reason buttonsList.Controls.Clear(); btnAddNewFlic.Enabled = false; _flicClient = null; lblConnectionStatus.Text = "Connection status: Disconnected"; lblBluetoothStatus.Text = "Bluetooth controller status:"; btnConnectDisconnect.Text = "Connect"; btnConnectDisconnect.Enabled = true; _currentScanWizard = null; lblScanWizardStatus.Text = ""; } else { _flicClient.Disconnect(); btnConnectDisconnect.Enabled = false; } }