我想知道是否有人可以帮助我解决 C 蓝牙编程 (Linux Bluez) 的问题。 我使用的是 Ubuntu 10.04、BlueZ 4.60。 我的目标是拥有一个 L2CAP 套接字,其中两台计算机之间发送数据的延迟最小。 到目前为止,我设法打开一个 L2CAP 套接字,但该套接字有无休止的重传,我正在尝试更改它。我根本不想重传,因为我需要数据以最小的延迟快速传输,并且数据的可靠性并不重要。
我在网上找到了一个示例,该示例涉及更改套接字的刷新超时,从而导致如果数据包在一定时间后未得到确认,则会被丢弃并发送缓冲区中的下一个数据。 问题是这个例子不起作用:-(
这是我的代码,该方法在绑定命令之后调用:
int set_flush_timeout(bdaddr_t *ba, int timeout)
{
int err = 0, dd, dev_id;
struct hci_conn_info_req *cr = 0;
struct hci_request rq = { 0 };
struct {
uint16_t handle;
uint16_t flush_timeout;
} cmd_param;
struct {
uint8_t status;
uint16_t handle;
} cmd_response;
// find the connection handle to the specified bluetooth device
cr = (struct hci_conn_info_req*) malloc(
sizeof(struct hci_conn_info_req) +
sizeof(struct hci_conn_info));
bacpy( &cr->bdaddr, ba );
cr->type = ACL_LINK;
dev_id = hci_get_route( NULL);
dd = hci_open_dev( dev_id );
if( dd < 0 ) {
err = dd;
goto cleanup;
}
err = ioctl(dd, HCIGETCONNINFO, (unsigned long) cr );
if( err ) goto cleanup;
// build a command packet to send to the bluetooth microcontroller
cmd_param.handle = cr->conn_info->handle;
cmd_param.flush_timeout = htobs(timeout);
rq.ogf = OGF_HOST_CTL;
rq.ocf = 0x28;
rq.cparam = &cmd_param;
rq.clen = sizeof(cmd_param);
rq.rparam = &cmd_response;
rq.rlen = sizeof(cmd_response);
rq.event = EVT_CMD_COMPLETE;
// send the command and wait for the response
err = hci_send_req( dd, &rq, 1 );
if( err ) goto cleanup;
if( cmd_response.status ) {
err = -1;
errno = bt_error(cmd_response.status);
}
cleanup:
free(cr);
if( dd >= 0) close(dd);
return err;
}
我的错误是什么? 有谁知道另一个可以解决我的问题的选择。 代码示例也很棒!!
设置自动刷新超时的代码似乎是正确的。 您可以通过确保您收到“成功”来响应此命令的命令完成事件来确保。
我怀疑问题可能出在您的数据包发送代码中,请注意,为了使自动刷新超时生效,各个数据包应标记为可自动刷新,HCI 数据包具有 Packet_Boundary_Flag,您可以发送该标志来指示各个数据包是否可冲洗。
还要注意,刷新超时必须足够大,以便有足够的时间,以便数据包获得传输尝试,刷新超时的定义方式可能会导致数据包被刷新,即使数据包没有被传输一次,所以你需要调整它。根据定义,当数据包排队等待传输时,刷新超时开始。