SamuelNLP
SamuelNLP

Reputation: 4136

Using bluetooth with qt in linux

I've wrote a program in C to connect the pc with a device by bluetooth. The program runs from terminal and the data received is shown in terminal as well. So far so good.

Now I've created a gui in qt, in which the main aim is to present the information which was before shown in terminal, now in qwtplots.

Well, I can so far connect the device with pc with the gui, but when I request the information form the device, it is shown in the terminal but the gui starts non responding.

here's the slot that requests the information from the device:

// Main Bluetooth
void gui::main_b()
{
    // BLUETOOTH STUFF
    int status, bytes_read;
    int conta = 0;
    FILE *data = NULL;

    fd_set readmask;
    struct timeval tv;
    char buf[101];
    int v, v1, v2;

    tv.tv_sec = 0;
    tv.tv_usec = 100000;

    // Standard messages
    char *startstr = "@START,0060,FF,12;";

    write (sock, startstr, strlen (startstr));

    data = fopen ("data.txt", "w");
    while (conta < 100)
    {
        int i;
        memset (buf, 0, 100);
        FD_ZERO (&readmask);
        FD_SET (sock, &readmask);
        if (select (255, &readmask, NULL, NULL, &tv) > 0)
        {
            if (FD_ISSET (sock, &readmask))
            {
                int numb;
                numb = read (sock, buf, 100);

                // 12 bits
                if (ui->comboBox->currentIndex() == 1)
                {
                    if (numb == 14)
                    {
                        conta++;
                        //printf ("received %d bytes:\n", numb);
                        // print of counter
                        //printf ("%d,", buf[0]);
                        fprintf (data, "%d,", buf[0]);
                        for (i = 1; i < numb-1; i += 3)
                        {
                            v1 = buf[i] | ((buf[i + 1] & 0x0F) << 8);
                            v2 = buf[i + 2];
                            v2 = (v2 << 4) | ((buf[i + 1] & 0xf0) >> 4);
                            printf ("%d,%d,", v1, v2);
                            //fprintf (data, "%d,%d,", v1, v2);
                        }

                        printf ("\n");
                        //fprintf (data, "\n");
                    }
                }
            }
        }
    }
    fclose (data);
}

so, when i click the button which calls this slot, it will never let me use the gui again.

This works in terminal.

thanks in advance.

Upvotes: 0

Views: 695

Answers (2)

hyde
hyde

Reputation: 62817

Instead of your own select, you should use QSocketNotifier class and give your own file handles for Qt event loop.

You can also use this overload of QFile::open to turn your socket into a QIODevice instance.

Third choice is to put your own select loop into a different thread, so it does not block the Qt main event loop. But that is going to bring quite a lot of extra complexity, so I'd do that only as a last resort.

Upvotes: 2

cmannett85
cmannett85

Reputation: 22356

You are running the while loop in the same thread as the GUI so the event queue is blocked. You have two choices:

  • During the loop, call QCoreApplication::processEvents(). This forces the event queue to be processed.
  • Separate the while loop logic into it's own thread.

The first one is much simpler, but is generally considered inefficient, as just all about all computers have multiple cores.

Upvotes: 1

Related Questions