Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, Im writing application which is using bluetooth (bluez stack - l2cap protocol). Sending data to others devices is very easy, because bluez bluetooth stack is using BSD sockets. So, ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    2

    Bluez and socket programing - how to disable buffering

    Hi,


    Im writing application which is using bluetooth (bluez stack - l2cap protocol). Sending data to others devices is very easy, because bluez bluetooth stack is using BSD sockets. So, when I want send something then I write:

    Code:
    sockId = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    
    while( "msg is ready")
    {
    status = write( sockId, &msgBuf, sizeof(msgBuf));
    }
    And now I have the problem. If im writing to socket too often my data, then data is buffered and I have very big latencies (up to 3s). Now my question is, how can I know, that my previous packet was sent ??? I want to send next packet only if buffer is empty (socket is empty), because then I'm not buffering my bluetooth data and latencies are correct ( 6 ms instead of 3 s).

    Code is working correctly (without buffering) when I implement:
    Code:
    sockId = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    
    while( "msg is ready")
    {
    status = write( sockId, &msgBuf, sizeof(msgBuf));
    usleep( 2700);
    }
    ... but I think, that this delay is "not professional". Is there any blocking function which can I use to determinate if socket is empty ? Is there any possibility to check how many bytes is in the socket buffer ? Does bluez bluetooth stack inform application if packet was sent ???

    I tried reduce socket buffer size, but it didn't work with bluetooth socket. I tried:
    Code:
    setsockopt(skt,BTPROTO_L2CAP, SO_SNDBUF,(char *)&sndsize, (int)sizeof(sndsize));
    But i got error:
    Code:
    "Protocol not available"
    Any ideas ? Suggestions ? Pleeeease help me

    Sorry for my English - I'm not native English speaker.
    Best regards,
    Lukasz

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Have you tried to use fcntl() to make the socket non-blocking? If its buffer is full, the write will return EAGAIN. Also, you can use select() to find out when a blocking socket is ready to take more data. You can specify a timeout to select() if there is an upper bound where you can wait before you do something else. In any case, select() is the most common method to do what you want, and using non-blocking sockets is prefereable where you need to do a lot of other stuff between writes.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...