简体   繁体   中英

bluetooth read thread will not exit when bluetooth connection lost in c

I am writing a "race box" in C that runs on an Intel Edison. It can send or receive bluetooth data with an Android app based on Bluetooth Chat. Everything works fine. I can send and receive. I can lose the connection and reacquire and the connection comes back and data flows from the C program to the Android app. All is good except once I reconnect I can no longer send data from C to Android. I have traced this to the read statement and I assume it never returns a value because it never exits the while loop. Specifically, this line:

while(bluetooth_up == 1 && (read(client, &aa, 1) == -1) ){

Will not exit even when I know bluetooth_up == 0 (another thread fprints bluetooth_up and it is 0 when bluetooth is down). My conclusion was that read was blocking so I attempted to fix that with the line

fcntl(s, F_SETFL,sock_flags|O_NONBLOCK);

The bluetooth connection is in the bluetooth write thread. But like I said, everything works except that this while loop will not exit when bluetooth_up is 0.

All I can figure out is that the read is blocking and what I cannot figure out is how to make it not blocking so it will return the -1 and the while loop can see that bluetooth_up == 0 and exit.

Here is the global definition of bluetooth_up

volatile int bluetooth_up = 0;

I would appreciate help on this as it needs to be robust and I don't want to require people to power cycle the race box to get bluetooth working again, although that does work.

    void *blueRead(void *arg){
    blue_read_up = 1;
    char aa;
    char buffer[500];
    int idx = 0;
    printf("\nBlue Read started\n");
    fcntl(s, F_SETFL,sock_flags|O_NONBLOCK);
    while(bluetooth_up == 1){
        if (new_blue_read_sentence == 0 && bluetooth_up == 1){
            while(bluetooth_up == 1 && (read(client, &aa, 1) == -1) ){
                    if(bluetooth_up == 0){
                        printf("\nExiting blue read\n");
                        blue_read_up = 0;
                        return NULL;
                    }
            }
            printf("%i",aa);
            if(aa != '\n')
            {
                buffer[idx++] = aa;
            }
            else
            {
                buffer[idx] = '\n';
                buffer[idx + 1] = '\0';
                idx = 0;
                strcpy( blue_read_buffer, buffer);
                new_blue_read_sentence = 1;
            }
        }
    }
    printf("\nExiting blue read 2\n");
    blue_read_up = 0;
    return NULL;
}

I think the bluetooth connection code is pretty standard but here it is

        // allocate socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    printf("\nsocket = %i\n", s);

    // bind socket to port 1 of the first available local bluetooth adapter
    loc_addr.rc_family = AF_BLUETOOTH;
    loc_addr.rc_bdaddr = *BDADDR_ANY;
    loc_addr.rc_channel = (uint8_t)1;
    retval = bind(s, (struct sockaddr*)&loc_addr, sizeof(loc_addr));
    printf("\nbind = %i\n", retval);

    // put socket into listening mode
    //listen(s, 1);
    retval = listen(s, 1);
    printf("\nlisten = %i\n", retval);

    // accept one connection
    client = accept(s, (struct sockaddr*)&rem_addr, &opt);

    sock_flags = fcntl(s,F_GETFL,0);
    fcntl(s, F_SETFL,sock_flags|O_NONBLOCK);

    printf("\n1 - client connect = %i socket %i\n", client, s);

You are setting O_NONBLOCK on the listening socket (s). Try instead on the accept socket (client) which is the one that is actually being read from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM