// Callback invoked once the connection with a PLUX device was established.
        public void ConnectionDone()
        {
            // Change the color and text of "Connect" button.
            if (ConnectText.text == "Connect")
            {
                Debug.Log("Connection with device " + this.SelectedDevice + " established with success!");

                ConnectText.text = "Disconnect";
                GreenFlag.SetActive(true);
                RedFlag.SetActive(false);

                // Enable "Device Configuration" panel options.
                SamplingRateInput.interactable  = true;
                ResolutionInput.interactable    = true;
                ResolutionDropdown.interactable = true;

                // Enable channel selection buttons accordingly to the type of device.
                string devType = PluxDevManager.GetDeviceTypeUnity();
                Debug.Log("Product ID: " + PluxDevManager.GetProductIdUnity());
                if (devType == "MuscleBAN BE Plux")
                {
                    CH1Toggle.interactable = true;

                    //Clear the old options of the Dropdown menu
                    ResolutionDropdown.ClearOptions();

                    //Add the options created in the List above
                    ResolutionDropdown.AddOptions(new List <string>()
                    {
                        "8", "16"
                    });
                }
                else if (devType == "BITalino")
                {
                    CH1Toggle.interactable = true;
                    CH2Toggle.interactable = true;
                    CH3Toggle.interactable = true;
                    CH4Toggle.interactable = true;
                    CH5Toggle.interactable = true;
                    CH6Toggle.interactable = true;

                    //Clear the old options of the Dropdown menu
                    ResolutionDropdown.ClearOptions();

                    //Add the options created in the List above
                    ResolutionDropdown.AddOptions(new List <string>()
                    {
                        "10"
                    });
                }
                else if (devType == "biosignalsplux" || devType == "BioPlux")
                {
                    CH1Toggle.interactable = true;
                    CH2Toggle.interactable = true;
                    CH3Toggle.interactable = true;
                    CH4Toggle.interactable = true;
                    CH5Toggle.interactable = true;
                    CH6Toggle.interactable = true;
                    CH7Toggle.interactable = true;
                    CH8Toggle.interactable = true;

                    //Clear the old options of the Dropdown menu
                    ResolutionDropdown.ClearOptions();

                    //Add the options created in the List above
                    if (devType == "biosignalsplux")
                    {
                        ResolutionDropdown.AddOptions(new List <string>()
                        {
                            "8", "16"
                        });
                        ResolutionDropDownOptions = new List <string>()
                        {
                            "8", "16"
                        };
                    }
                    else
                    {
                        ResolutionDropdown.AddOptions(new List <string>()
                        {
                            "8", "12"
                        });
                        ResolutionDropDownOptions = new List <string>()
                        {
                            "8", "12"
                        };
                    }
                }
                else if (devType == "OpenBANPlux")
                {
                    CH1Toggle.interactable = true;
                    CH2Toggle.interactable = true;

                    //Clear the old options of the Dropdown menu
                    ResolutionDropdown.ClearOptions();

                    //Add the options created in the List above
                    ResolutionDropdown.AddOptions(new List <string>()
                    {
                        "8", "16"
                    });
                }
                else if (PluxDevManager.GetProductIdUnity() != 542) // If the device is not a fNIRS Explorer then we are dealing with a not supported system.
                {
                    throw new NotSupportedException();
                }

                // Enable Start and Device Configuration buttons.
                StartButton.interactable = true;

                // Disable "Start by Sources" button if the device is a BITalino.
                if (PluxDevManager.GetDeviceTypeUnity() != "BITalino")
                {
                    StartBySrcButton.interactable = true;
                }

                // Disable Connect Button.
                //ConnectButton.interactable = false;

                // Hide show Info message if it is active.
                ConnectInfoPanel.SetActive(false);

                // Update Battery Level.
                int batteryLevel = -1;
                if (devType != "BioPlux")
                {
                    batteryLevel = PluxDevManager.GetBatteryUnity();
                }

                // Battery icon accordingly to the battery level.
                List <GameObject> ListBatteryIcons = new List <GameObject>()
                {
                    BatteryIcon0, BatteryIcon10, BatteryIcon50, BatteryIcon100, BatteryIconUnknown
                };
                GameObject currImage;
                if (batteryLevel > 50)
                {
                    BatteryIcon100.SetActive(true);
                    currImage = BatteryIcon100;
                }
                else if (batteryLevel <= 50 && batteryLevel > 10)
                {
                    BatteryIcon50.SetActive(true);
                    currImage = BatteryIcon50;
                }
                else if (batteryLevel <= 10 && batteryLevel > 1)
                {
                    BatteryIcon10.SetActive(true);
                    currImage = BatteryIcon10;
                }
                else if (batteryLevel == 0)
                {
                    BatteryIcon0.SetActive(true);
                    currImage = BatteryIcon0;
                }
                else
                {
                    BatteryIconUnknown.SetActive(true);
                    currImage = BatteryIconUnknown;
                }

                // Disable the remaining images.
                foreach (var batImg in ListBatteryIcons)
                {
                    if (batImg != currImage)
                    {
                        batImg.SetActive(false);
                    }
                }

                // Show the quantitative battery value.
                if (batteryLevel != -1)
                {
                    BatteryLevel.text = batteryLevel.ToString() + "%";
                }
                else
                {
                    BatteryLevel.text = "N.A.";
                }
            }
        }
        // Function invoked during the onClick event of "ConnectButton".
        public void ConnectButtonFunction(bool typeOfStop)
        {
            try
            {
                // Change the color and text of "Connect" button.
                if (ConnectText.text == "Connect")
                {
                    // Specification of the callback function (defined on this/the user Unity script) which will receive the acquired data
                    // samples as inputs.
                    PluxDevManager.SetCallbackHandler(CallbackHandler);

                    // Get the selected device.
                    string selectedDevice = this.ListDevices[DeviceDropdown.value];

                    // Connection with the device.
                    Debug.Log("Trying to establish a connection with device " + selectedDevice);
                    PluxDevManager.PluxDev(selectedDevice);
                    Debug.Log("Connection with device " + selectedDevice + " established with success!");

                    ConnectText.text = "Disconnect";
                    GreenFlag.SetActive(true);
                    RedFlag.SetActive(false);

                    // Enable "Device Configuration" panel options.
                    SamplingRateInput.interactable  = true;
                    ResolutionInput.interactable    = true;
                    ResolutionDropdown.interactable = true;

                    // Enable channel selection buttons accordingly to the type of device.
                    string devType = PluxDevManager.GetDeviceTypeUnity();
                    if (devType == "MuscleBAN BE Plux")
                    {
                        CH1Toggle.interactable = true;
                    }
                    else if (devType == "BITalino")
                    {
                        CH1Toggle.interactable = true;
                        CH2Toggle.interactable = true;
                        CH3Toggle.interactable = true;
                        CH4Toggle.interactable = true;
                        CH5Toggle.interactable = true;
                        CH6Toggle.interactable = true;

                        //Clear the old options of the Dropdown menu
                        ResolutionDropdown.ClearOptions();

                        //Add the options created in the List above
                        ResolutionDropdown.AddOptions(new List <string>()
                        {
                            "8"
                        });
                    }
                    else if (devType == "biosignalsplux")
                    {
                        CH1Toggle.interactable = true;
                        CH2Toggle.interactable = true;
                        CH3Toggle.interactable = true;
                        CH4Toggle.interactable = true;
                        CH5Toggle.interactable = true;
                        CH6Toggle.interactable = true;
                        CH7Toggle.interactable = true;
                        CH8Toggle.interactable = true;
                    }
                    else if (devType == "OpenBANPlux")
                    {
                        CH1Toggle.interactable = true;
                        CH2Toggle.interactable = true;
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }

                    // Enable Start and Device Configuration buttons.
                    StartButton.interactable = true;

                    // Disable Connect Button.
                    //ConnectButton.interactable = false;

                    // Hide show Info message if it is active.
                    ConnectInfoPanel.SetActive(false);

                    // Update Battery Level.
                    int batteryLevel;
                    batteryLevel = PluxDevManager.GetBatteryUnity();

                    // Battery icon accordingly to the battery level.
                    List <GameObject> ListBatteryIcons = new List <GameObject>()
                    {
                        BatteryIcon0, BatteryIcon10, BatteryIcon50, BatteryIcon100, BatteryIconUnknown
                    };
                    GameObject currImage;
                    if (batteryLevel > 50)
                    {
                        BatteryIcon100.SetActive(true);
                        currImage = BatteryIcon100;
                    }
                    else if (batteryLevel <= 50 && batteryLevel > 10)
                    {
                        BatteryIcon50.SetActive(true);
                        currImage = BatteryIcon50;
                    }
                    else if (batteryLevel <= 10 && batteryLevel > 1)
                    {
                        BatteryIcon10.SetActive(true);
                        currImage = BatteryIcon10;
                    }
                    else
                    {
                        BatteryIcon0.SetActive(true);
                        currImage = BatteryIcon0;
                    }

                    // Disable the remaining images.
                    foreach (var batImg in ListBatteryIcons)
                    {
                        if (batImg != currImage)
                        {
                            batImg.SetActive(false);
                        }
                    }

                    // Show the quantitative battery value.
                    BatteryLevel.text = batteryLevel.ToString() + "%";
                }
                else if (ConnectText.text == "Disconnect")
                {
                    // Disconnect device if the stop was forced by an event or timeout exception.
                    if (typeOfStop == false)
                    {
                        PluxDevManager.DisconnectPluxDev();
                    }

                    ConnectText.text = "Connect";
                    GreenFlag.SetActive(false);
                    RedFlag.SetActive(true);

                    // Disable "Device Configuration" panel options.
                    SamplingRateInput.interactable  = false;
                    SamplingRateInput.text          = "1000";
                    ResolutionInput.interactable    = false;
                    ResolutionDropdown.interactable = false;

                    // Disable channel selection buttons.
                    CH1Toggle.interactable = false;
                    CH2Toggle.interactable = false;
                    CH3Toggle.interactable = false;
                    CH4Toggle.interactable = false;
                    CH5Toggle.interactable = false;
                    CH6Toggle.interactable = false;
                    CH7Toggle.interactable = false;
                    CH8Toggle.interactable = false;

                    // Disable Start and Device Configuration buttons.
                    StartButton.interactable = false;

                    // Disable the battery icons.
                    List <GameObject> ListBatteryIcons = new List <GameObject>()
                    {
                        BatteryIcon0, BatteryIcon10, BatteryIcon50, BatteryIcon100
                    };
                    foreach (var batImg in ListBatteryIcons)
                    {
                        batImg.SetActive(false);
                    }
                    BatteryIconUnknown.SetActive(true);

                    // Show the quantitative battery value.
                    BatteryLevel.text = "";

                    // Disable Drop-down options.
                    DeviceDropdown.ClearOptions();

                    //Add the options created in the List above
                    DeviceDropdown.AddOptions(new List <string>()
                    {
                        "Select Device"
                    });

                    // Disable drop-down and Connect button if a PLUX Device was disconnected.
                    DeviceDropdown.interactable = false;
                    ConnectButton.interactable  = false;

                    // Show PlotIcon.
                    PlotIcon.SetActive(true);
                    TransparencyLevel.SetActive(true);

                    // Reboot of global variables.
                    RebootVariables();
                }
            }
            catch (Exception e)
            {
                // Print information about the exception.
                Debug.Log(e);

                // Show info message.
                ConnectInfoPanel.SetActive(true);

                // Hide object after 5 seconds.
                StartCoroutine(RemoveAfterSeconds(5, ConnectInfoPanel));
            }
        }