Home > Enterprise >  How do I format an bluetooth address as a btaddr?
How do I format an bluetooth address as a btaddr?

Time:10-07

I have a bluetooth address (7C9EBD4CBFB2) that I need to connect to using winsock. This is my code, which returns error as -1, and won't connect to my device.

#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;
}

How do I format this to use as a btaddr? Thanks.

CodePudding user response:

You are not calling WSAStartup() before socket(). You would have realized this sooner if you had been doing better error checking.

socket() would have returned INVALID_SOCKET (-1), and then WSAGetLastError() would have returned WSANOTINITIALISED (10093).

And then connect() would have failed with SOCKET_ERROR (-1), and WSAGetLastError() would have returned WSAENOTSOCK (10038).

Per the Bluetooth Programming with Windows Sockets documentation:

As with all Windows Sockets application programming, the WSAStartup function must be called to initiate Windows Sockets functionality and enable Bluetooth.

ALWAYS check error codes on system calls!

Try something more like this:

#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, "x:x:x:x:x:x",
                    &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;
}
  • Related