我有一个非常简单的蓝牙 RFCOMM 客户端应用程序,它试图维护来自蓝牙设备的蓝牙流,蓝牙设备是 UART 到蓝牙转换器。我想要的行为是,当蓝牙发射器重新启动时(它可以在不到 2 秒的时间内完成),我可以很快恢复。目前由于 SO_RCVTIMEO 我很快断开连接但尝试重新连接会导致几秒钟......
connect: Device or resource busy
Not connected
connect: Device or resource busy
Not connected
connect: Device or resource busy
...消息。这与我是否重新启动进程无关。您可以想象,如果我想频繁关闭我的蓝牙发射器(重新编程),这有点不切实际。我可以更改任何套接字选项或其他蓝牙设置来解决此问题吗?
#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); // write to stdout
}
else
{
perror("read");
printf("Errno is %i\n", errno);
close(sock);
while( (sock = connect_to_bluetooth(argv[1])) == -1) {
usleep(500000);
}
}
}
return 0;
}