如何将蓝牙地址格式化为 btaddr?
How do I format an bluetooth address as a btaddr?
我有一个蓝牙地址 (7C9EBD4CBFB2),需要使用 winsock 连接。这是我的代码,returns 错误为 -1,无法连接到我的设备。
#include <winsock2.h>
#include <ws2bth.h>
#pragma comment(lib, "Ws2_32.lib")
#include <Windows.h>
#include <iostream>
using namespace std;
SOCKADDR_BTH sockAddr;
SOCKET btSocket;
int error;
int main() {
btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.addressFamily = AF_BTH;
sockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;
sockAddr.port = BT_PORT_ANY;
sockAddr.btAddr = 0x7C9EBD4CBFB2;
error = connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));
cout << error;
}
如何格式化它以用作 btaddr?谢谢
您没有在 socket()
之前致电 WSAStartup()
。如果您一直在进行更好的错误检查,您会更快地意识到这一点。参见 Handling Winsock Errors。
socket()
会返回 INVALID_SOCKET
(-1),然后 WSAGetLastError()
会返回 WSANOTINITIALISED
(10093)。
Successful WSAStartup
not yet performed.
Either the application has not called WSAStartup
or WSAStartup
failed. The application may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), or WSACleanup
has been called too many times.
然后 connect()
会失败 SOCKET_ERROR
(-1),WSAGetLastError()
会返回 WSAENOTSOCK
(10038)。
Socket operation on nonsocket.
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select
, a member of an fd_set
was not valid.
根据 Bluetooth Programming with Windows Sockets 文档:
As with all Windows Sockets application programming, the WSAStartup function must be called to initiate Windows Sockets functionality and enable Bluetooth.
始终检查系统调用的错误代码!
试试像这样的东西:
#include <winsock2.h>
#include <ws2bth.h>
#include <Windows.h>
#include <iostream>
#include <cstdio>
#pragma comment(lib, "Ws2_32.lib")
int str2ba(const char *straddr, BTH_ADDR *btaddr)
{
int i;
unsigned int aaddr[6];
BTH_ADDR tmpaddr = 0;
if (std::sscanf(straddr, "%02x:%02x:%02x:%02x:%02x:%02x",
&aaddr[0], &aaddr[1], &aaddr[2],
&aaddr[3], &aaddr[4], &aaddr[5]) != 6)
return 1;
*btaddr = 0;
for (i = 0; i < 6; i++) {
tmpaddr = (BTH_ADDR) (aaddr[i] & 0xff);
*btaddr = ((*btaddr) << 8) + tmpaddr;
}
return 0;
}
int main() {
WSADATA wsa;
memset(&wsa, 0, sizeof(wsa));
int error = WSAStartup(MAKEWORD(2,2), &wsa);
if (error != 0) {
std::cerr << "WSAStartup() failed, error: " << error;
return -1;
}
SOCKET btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (btSocket == INVALID_SOCKET) {
error = WSAGetLastError();
std::cerr << "socket() failed, error: " << error;
WSACleanup();
return -1;
}
SOCKADDR_BTH sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.addressFamily = AF_BTH;
sockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;
sockAddr.port = BT_PORT_ANY;
str2ba("7C:9E:BD:4C:BF:B2", &sockAddr.btAddr);
if (connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR) {
error = WSAGetLastError();
std::cerr << "connect() failed, error: " << error;
closesocket(btSocket);
WSACleanup();
return -1;
}
// use btSocket as needed...
closesocket(btSocket);
return 0;
}
我有一个蓝牙地址 (7C9EBD4CBFB2),需要使用 winsock 连接。这是我的代码,returns 错误为 -1,无法连接到我的设备。
#include <winsock2.h>
#include <ws2bth.h>
#pragma comment(lib, "Ws2_32.lib")
#include <Windows.h>
#include <iostream>
using namespace std;
SOCKADDR_BTH sockAddr;
SOCKET btSocket;
int error;
int main() {
btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.addressFamily = AF_BTH;
sockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;
sockAddr.port = BT_PORT_ANY;
sockAddr.btAddr = 0x7C9EBD4CBFB2;
error = connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));
cout << error;
}
如何格式化它以用作 btaddr?谢谢
您没有在 socket()
之前致电 WSAStartup()
。如果您一直在进行更好的错误检查,您会更快地意识到这一点。参见 Handling Winsock Errors。
socket()
会返回 INVALID_SOCKET
(-1),然后 WSAGetLastError()
会返回 WSANOTINITIALISED
(10093)。
Successful
WSAStartup
not yet performed.Either the application has not called
WSAStartup
orWSAStartup
failed. The application may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), orWSACleanup
has been called too many times.
然后 connect()
会失败 SOCKET_ERROR
(-1),WSAGetLastError()
会返回 WSAENOTSOCK
(10038)。
Socket operation on nonsocket.
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for
select
, a member of anfd_set
was not valid.
根据 Bluetooth Programming with Windows Sockets 文档:
As with all Windows Sockets application programming, the WSAStartup function must be called to initiate Windows Sockets functionality and enable Bluetooth.
始终检查系统调用的错误代码!
试试像这样的东西:
#include <winsock2.h>
#include <ws2bth.h>
#include <Windows.h>
#include <iostream>
#include <cstdio>
#pragma comment(lib, "Ws2_32.lib")
int str2ba(const char *straddr, BTH_ADDR *btaddr)
{
int i;
unsigned int aaddr[6];
BTH_ADDR tmpaddr = 0;
if (std::sscanf(straddr, "%02x:%02x:%02x:%02x:%02x:%02x",
&aaddr[0], &aaddr[1], &aaddr[2],
&aaddr[3], &aaddr[4], &aaddr[5]) != 6)
return 1;
*btaddr = 0;
for (i = 0; i < 6; i++) {
tmpaddr = (BTH_ADDR) (aaddr[i] & 0xff);
*btaddr = ((*btaddr) << 8) + tmpaddr;
}
return 0;
}
int main() {
WSADATA wsa;
memset(&wsa, 0, sizeof(wsa));
int error = WSAStartup(MAKEWORD(2,2), &wsa);
if (error != 0) {
std::cerr << "WSAStartup() failed, error: " << error;
return -1;
}
SOCKET btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (btSocket == INVALID_SOCKET) {
error = WSAGetLastError();
std::cerr << "socket() failed, error: " << error;
WSACleanup();
return -1;
}
SOCKADDR_BTH sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.addressFamily = AF_BTH;
sockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;
sockAddr.port = BT_PORT_ANY;
str2ba("7C:9E:BD:4C:BF:B2", &sockAddr.btAddr);
if (connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR) {
error = WSAGetLastError();
std::cerr << "connect() failed, error: " << error;
closesocket(btSocket);
WSACleanup();
return -1;
}
// use btSocket as needed...
closesocket(btSocket);
return 0;
}