如何通过蓝牙套接字(RFCOMM、SPP)仅发送一个字节或如何刷新蓝牙套接字(RFCOMM、SPP)?
我已经使用基于 RFCOMM 和串行端口配置文件 (SPP) 的 MS 蓝牙套接字在我的 PC 和蓝牙设备之间建立了连接。
连续接收数据没有问题。但是,如果要发送一个字节(充当命令),设备不会接收它。
以下是代码片段(错误处理省略):
SOCKET s = ::socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); //successful
...
SOCKADDR_BTH btSockAddr;
ZeroMemory(&btSockAddr, sizeof(SOCKADDR_BTH));
btSockAddr.addressFamily = AF_BTH;
btSockAddr.btAddr = DEVICE_ADDRESS;
btSockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID; //SerialPortServiceClass_UUID
btSockAddr.port = BT_PORT_ANY;
...
err = ::connect(s, reinterpret_cast<SOCKADDR*>(&btSockAddr), sizeof(SOCKADDR_BTH)); //successful
char cmd = 'm'; //switch mode
int sentSize = ::send(s, &cmd, 1, 0); //successful, sentSize = 1
...
for(;;)
{
char out;
int recvSize = ::recv(s, &out, 1, 0); //successful, receive byte by byte
...
}
虽然 ::send
返回成功,但设备的模式并没有改变。因此,显然,给定的字节尚未传输,仍然驻留在发送缓冲区中(这是我的假设)。有没有一种方法可以强制只发送一个字节,即不缓冲发送?
我可以排除设备方面的错误。手动设置蓝牙链接(Windows 通知栏/蓝牙/添加设备)并使用 Putty 建立串行连接,接收和发送均按预期工作。
操作系统:Windows 7 集成开发环境:Visual Studio 2010
I have set up a connection between my PC and a Bluetooth device using a MS Bluetooth socket based on RFCOMM and Serial Port Profile (SPP).
There is no problem to receive data continuously. However, if want to send one byte (which acts as a command), the device does not receive it.
Here are the code snippets (error handling omitted):
SOCKET s = ::socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); //successful
...
SOCKADDR_BTH btSockAddr;
ZeroMemory(&btSockAddr, sizeof(SOCKADDR_BTH));
btSockAddr.addressFamily = AF_BTH;
btSockAddr.btAddr = DEVICE_ADDRESS;
btSockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID; //SerialPortServiceClass_UUID
btSockAddr.port = BT_PORT_ANY;
...
err = ::connect(s, reinterpret_cast<SOCKADDR*>(&btSockAddr), sizeof(SOCKADDR_BTH)); //successful
char cmd = 'm'; //switch mode
int sentSize = ::send(s, &cmd, 1, 0); //successful, sentSize = 1
...
for(;;)
{
char out;
int recvSize = ::recv(s, &out, 1, 0); //successful, receive byte by byte
...
}
Though ::send
returns successfully, the device's mode has not changed. So, apparently, the byte given has not been transmitted and still resides in the send buffer (that is my assumption). Is there a way to force sending only one byte, i.e. to send unbuffered?
I can rule out a mistake on device side. Setting up the Bluetooth link manually (Windows Notification Bar / Bluetooth / Add device) und using Putty to establish a serial connection, both receiving and sending are working as expected.
OS: Windows 7
IDE: Visual Studio 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将 Putty 与虚拟 COM 端口一起使用时,您是否在输入单个命令字符后按回车键?
我怀疑是这样,因此您需要在命令字节之后发送 CR+LF。因此传递一个大小为 3 的缓冲区来发送(即 {, 0x0D, 0x0A })。您的设备可能是基于线路的并且正在等待“线路”。
When you use Putty with the virtual COM port do you hit return after entering your single command character?
I suspect so, and thus you need to send CR+LF after the command byte. So pass a buffer of size three to send (i.e. { <command>, 0x0D, 0x0A }). Your device is presumably line based and it waiting for a "line".
好的,试试第二个! :-)
这行可能是问题所在:
Why did you change from SerialPortServiceClass_UUID? RFCOMM_PROTOCOL_UUID 告诉 RFCOMM 套接字连接到使用 RFCOMM 协议的 SDP(服务数据库)中的任何服务。您想要的是:(
当然,如果设备在 SD 中只有一个 RFCOMM 服务,那么实际上不会导致您所看到的问题)。
OK, try number 2! :-)
This line is probably the problem:
Why did you change from SerialPortServiceClass_UUID? With RFCOMM_PROTOCOL_UUID that tells the RFCOMM socket to connect to any service in the SDP (service database) that uses protocol RFCOMM. You want instead to have:
(Of course that wouldn't actaully cause the problem you are seeing if the device has only one RFCOMM service in the SD).