0

I'm trying to get 2 laptops with bluetooth to connect. This is the code I'm using for making a server:

WSADATA wsd;
WSAStartup (MAKEWORD(1,0), &wsd);

SOCKET server_socket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);

SOCKADDR_BTH sa;
memset(&sa, 0, sizeof(sa));
sa.addressFamily = AF_BTH;
sa.port = 0 & 0xFF;

if(0 != bind(server_socket, (SOCKADDR *)&sa, sizeof(sa)))
{
    std::cout << "BIND ERROR: " << WSAGetLastError () << std::endl;
    closesocket(server_socket);
    return false;
}

if(0 != listen (server_socket, 5))
{
    std::cout << "LISTEN ERROR: " << WSAGetLastError () << std::endl;
    closesocket(server_socket);
    return false;
}

SOCKADDR_BTH sa2;
int size = sizeof(sa2);
SOCKET s2 = accept(server_socket, (SOCKADDR*)&sa2, &size);
if(s2 == INVALID_SOCKET)
{
    std::cout << "ACCEPT ERROR: " << WSAGetLastError () << std::endl;
    return false;
}
else
{
    std::cout << "Connected? " << WSAGetLastError () << std::endl;
}

and the following as a client:

WSADATA wsd;
WSAStartup (MAKEWORD(1,0), &wsd);

SOCKET client_socket = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);

SOCKADDR_BTH sa;
memset (&sa, 0, sizeof(sa));
sa.addressFamily = AF_BTH;
sa.btAddr = (BTH_ADDR)d.getDeviceAddress();
sa.port = 0;
sa.serviceClassId = *d.getLpServiceClassId();

if(client_socket == INVALID_SOCKET)
{
    std::cout << "SOCKET ERROR: " << WSAGetLastError () << std::endl;
    return false;
}
if (0 != connect (client_socket, (SOCKADDR *)&sa, sizeof(sa))) 
{
    std::cout << "CONNECT ERROR: " << WSAGetLastError () << std::endl;
    return false;
}
else
{
    //std::cout << "CONNECT ERROR: " << WSAGetLastError () << std::endl;
    //Perform error handling.s
    send(client_socket, "ABC",3,0);
    closesocket (client_socket);
    return true;
}

Where "b" is the bluetooth device I give as argument the bluetoothdevice class has the following fields:

LPSOCKADDR local_address;
LPSOCKADDR device_address;
LPGUID lp_service_class_id;

This SHOULD work. But when I try to connect to the server, the server gets nothing and the client gives me error 10060.

This error means:

WSAETIMEDOUT 10060

The I/O timed out at the Bluetooth radio level (PAGE_TIMEOUT).

Is there anyone out here who has worked with the bluetooth stack on windows and knows where this error comes from?

4

1 回答 1

0

您是否使用蓝牙 SDP 注册了您的服务?如果不是,我猜,您将无法在客户端代码中使用当前方法进行连接。来自 MSDN:

The bind function does not automatically advertise the server application using the Bluetooth SDP; applications must call the WSASetService function to be found by remote Bluetooth applications.

还可以查看蓝牙文档bind和功能connect

http://msdn.microsoft.com/en-us/library/windows/desktop/aa362901%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/windows/desktop /aa362901%28v=vs.85%29.aspx

据我了解,另一种方法是您可以尝试port在服务器端和客户端显式指定。但这仅适用于游乐场应用程序就足够了。

有关蓝牙使用的更多信息,您可以参考http://msdn.microsoft.com/en-us/library/windows/desktop/aa363058%28v=vs.85%29.aspx

于 2012-11-29T15:08:56.847 回答