Example #1
0
        private void AddNewFlic()
        {
            var scanWizard = new ScanWizard();

            scanWizard.FoundPrivateButton += ScanWizard_FoundPrivateButton;
            scanWizard.FoundPublicButton  += ScanWizard_FoundPublicButton;
            scanWizard.ButtonConnected    += ScanWizard_ButtonConnected;
            scanWizard.Completed          += ScanWizard_Completed;
            _flicClient.AddScanWizard(scanWizard);
        }
Example #2
0
        private void btnAddNewFlic_Click(object sender, EventArgs e)
        {
            if (_currentScanWizard == null)
            {
                lblScanWizardStatus.Text = "Press your Flic button";

                var scanWizard = new ScanWizard();
                scanWizard.FoundPrivateButton += (o, args) => Invoke((MethodInvoker) delegate
                {
                    lblScanWizardStatus.Text = "Hold down your Flic button for 7 seconds";
                });
                scanWizard.FoundPublicButton += (o, args) => Invoke((MethodInvoker) delegate
                {
                    lblScanWizardStatus.Text = "Found button " + args.BdAddr.ToString() + ", now connecting...";
                });
                scanWizard.ButtonConnected += (o, args) => Invoke((MethodInvoker) delegate
                {
                    lblScanWizardStatus.Text = "Connected to " + args.BdAddr.ToString() + ", now pairing...";
                });
                scanWizard.Completed += (o, args) => Invoke((MethodInvoker) delegate
                {
                    lblScanWizardStatus.Text = "Result: " + args.Result;
                    _currentScanWizard       = null;
                    btnAddNewFlic.Text       = "Add new Flic";
                });

                _flicClient.AddScanWizard(scanWizard);

                _currentScanWizard = scanWizard;
                btnAddNewFlic.Text = "Cancel";
            }
            else
            {
                _flicClient.CancelScanWizard(_currentScanWizard);
            }
        }
Example #3
0
        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;
            }
        }