Exemple #1
0
        private void _InitFlicClient()
        {
            if (_flicClient == null)
            {
                DebugEx.TraceLog("FlicClient is null, Find it!!!!");
                while (true)
                {
                    try
                    {
                        _flicClient = FlicClient.Create(ActiveCfg.FlicDaemonHostname, ActiveCfg.FlicDaemonPort);
                        DebugEx.TraceLog("Connection to FlicClient successful");
                        if (_flicClient != null)
                        {
                            _flicClient.GetInfo(
                                (bluetoothControllerState, myBdAddr, myBdAddrType, maxPendingConnections, maxConcurrentlyConnectedButtons,
                                 currentPendingConnections, currentlyNoSpaceForNewConnection, verifiedButtons) =>
                            {
                                DebugEx.TraceLog("Bluetooth controller status: " + bluetoothControllerState.ToString());
                                foreach (var bdAddr in verifiedButtons)
                                {
                                    GotButton(bdAddr);
                                }
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        DebugEx.TraceErrorException(ex);
                        Thread.Sleep(500);
                        continue;
                    }
                    break;
                }

                _flicClient.BluetoothControllerStateChange += (o, args) =>
                {
                    DebugEx.TraceLog("Bluetooth controller status: " + args.State.ToString());
                };

                _flicClient.NewVerifiedButton += (o, args) =>
                {
                    DebugEx.TraceLog("new NewVerifiedButtonEvent");
                    GotButton(args.BdAddr);
                };

                TaskEx.RunSafe(() =>
                {
                    _flicClient.HandleEvents(); // HandleEvents returns when the socket has disconnected for any reason
                    DebugEx.TraceError("BLE connection lost");
                    OnBLEDisconnected();
                });
            }
        }