WORD wVersionRequested;
WSADATA wsaData;
int err;

/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
    /* Tell the user that we could not find a usable */
    /* Winsock DLL.                                  */
    printf("WSAStartup failed with error: %d\n", err);
    return 1;
}

最佳答案:

如果您有来自发现或配对设备列表的蓝牙地址,则可以这样连接(需要添加错误检查):

#include <winsock2.h>
#include <ws2bth.h>
SOCKADDR_BTH sockAddr;
SOCKET btSocket;
int error;

   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 = <your bluetooth address>
   error = connect(btSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));

一些设备公布serialportserviceclass_uuid而不是rfcomm_protocol_uid。您可能还需要多次重试连接。某些蓝牙设备(cough powera moga-cough)的实现很差,需要多次尝试连接。
这可能不是官方的方法,但我从如下配对列表中获得了感兴趣设备的6字节bt地址:
unsigned char *p;
ULONGLONG ullAddr;

   p = (unsigned char *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr; // point to addr
   memcpy(&ullAddr, &p[2], 8); // copy Bluetooth address of device we found