Linked Questions

Popular Questions

I am using RFCOMM Server application on linux side so any mobile application can get connect with it and does the communication. I have used rfcomm-server.c example to test and have modified it little

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int s, client, bytes_read, status;
socklen_t opt = sizeof(rem_addr);

void vCreateConnection()
{
    // allocate socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

    // 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;
    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

    // put socket into listening mode
    listen(s, 1);

    // accept one connection
    client = accept(s, (struct sockaddr *)&rem_addr, &opt);
    ba2str( &rem_addr.rc_bdaddr, buf );
    fprintf(stderr, "accepted connection from %s\n", buf);

}

void vCloseConnection()
{
    close(client);
    close(s);
}

int main(int argc, char **argv)
{

    vCreateConnection();
    while(1)
    {
        printf("\r\nClear buffer");
        memset(buf, 0, sizeof(buf));

        // read data from the client
        bytes_read = read(client, buf, sizeof(buf));
        if( bytes_read > 0 ) {
            printf("received [%s]\n", buf);
            //if(status == 0)
            {
                printf("send [%s]\n", buf);
                status = write(s, &buf, 1);
                //status is not making any difference.
                //if(status < 0)
                    //break;
            }
        }
        if(buf[0] == 'q')
        {
            vCloseConnection();
            vCreateConnection();
        }
        else if(buf[0] == 'x')
            break;
    }
    // close connection
    close(client);
    close(s);
    return 0;
}
  1. Here I am able to connect fine and sending data from BlueTerm Android App. But when I disconnect from device from android then I am not sure if my linux application can detect that client is disconnected so I can put it back into accepting new connection mode by closing current active session?
  2. I am also not sure which is correct way to response back client is this function to write back client?

status = write(s, &buf, 1);

Related Questions