I have a very simple RFCOMM bluetooth client application which is trying to maintain a bluetooth stream from a bluetooth device which is a UART for bluetooth converter. My desired behavior is that when you restart the bluetooth transmitter (it can do this in less than 2 seconds), I can return to it very quickly. Currently, thanks to SO_RCVTIMEO, I am disconnecting very quickly, but trying to reconnect the results in a few seconds ...
connect: Device or resource busy
Not connected
connect: Device or resource busy
Not connected
connect: Device or resource busy
... messages. This is regardless of whether I restart my process or not. You can imagine that this is a little impractical if I want to often turn off my Bluetooth transmitter (reprogramming). Are there any socket options or other bluetooth settings that I could change to fix this behavior?
#include <sys/types.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <unistd.h>
#include <errno.h>
int connect_to_bluetooth( char const* btaddr )
{
int sock = socket( AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM );
if(sock < 0) {
perror("socket");
return -1;
}
bdaddr_t ba;
struct sockaddr_rc addr;
str2ba( btaddr, &ba );
memset( &addr, 0, sizeof(addr) );
addr.rc_family = AF_BLUETOOTH;
memcpy( &(addr.rc_bdaddr), &ba, sizeof(ba) );
addr.rc_channel = 1;
int result = connect( sock, (struct sockaddr *)&addr, sizeof(addr ) );
if(result == 0)
{
struct timeval tv;
tv.tv_sec = 2;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
printf("Connected to %s\n", btaddr);
return sock;
}
close(sock);
printf("Not connected\n");
perror("connect");
return -1;
}
int main(int argc, char** argv)
{
printf("Connect to bluetooth device\n");
int sock;
if(argc < 2)
return 1;
while( (sock = connect_to_bluetooth(argv[1])) == -1) usleep(500000);
while(1)
{
char buf[128];
ssize_t r = read(sock,buf, 128);
if(r > 0)
{
write(0, buf, r);
}
else
{
perror("read");
printf("Errno is %i\n", errno);
close(sock);
while( (sock = connect_to_bluetooth(argv[1])) == -1) {
usleep(500000);
}
}
}
return 0;
}
source
share