win10 Using winsock2, ws2bth.h ignored by compiler: BTHPROTO_RFCOMM not found

45 views Asked by At

I get several C2065 errors: BTHPROTO_RFCOMM/SOCKADDR_BTH is not defined when I use the Winsock Bluetooth API to connect with another device. I have included winsock2.h, ws2bth.h and bluetoothapis.h correctly, and I can jump to the definition in ws2bth.h with Ctrl + Left click.

My PC is Windows 10, and in sdkddkver.h the NTDDI_VERSION is defined as NTDDI_WIN10_CO, I tried to change it to NTDDI_WIN10, but I have no permission.

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4995)
#include <Winsock2.h>
#include <ws2bth.h>
#include "pch.h"
#include "framework.h"
#include "MFCDemo1.h"
#include "MFCDemo1Dlg.h"
#include "afxdialogex.h"
#include "bluetoothApis.h"
#include "iostream"

#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "bthprops.lib")
#pragma comment(lib,"Ws2_32.lib")

using namespace std;

CString selectedDevice;
SOCKET socketClient;
WSADATA wsaData;

BOOL initSocket(BLUETOOTH_DEVICE_INFO deviceInfo, SOCKET& socketClient,CWnd* editOutput) {
    //init
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        editOutput->SetWindowTextW(_T("fail to init"));
        return FALSE;
    }

    //build socket
    SOCKET socketHandle = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
    if (socketHandle == INVALID_SOCKET) {
        editOutput->SetWindowTextW(_T("fail to build"));
        WSACleanup();
        return FALSE;
    }

    socketClient = socketHandle;

    
    SOCKADDR_BTH serverAddress;
    memset(&serverAddress, 0, sizeof(serverAddress));
    serverAddress.addressFamily = AF_BTH;
    BTH_ADDR deviceAddress = deviceInfo.Address.ullLong;
    serverAddress.btAddr = deviceAddress;
    serverAddress.port = 0;
    serverAddress.serviceClassId = SerialPortServiceClass_UUID;

    int err = ::connect(socketHandle, (SOCKADDR*)&serverAddress, sizeof(serverAddress));
    if (0 == err) {
        editOutput->SetWindowTextW(_T("success"));
    }
    else {
        editOutput->SetWindowTextW(_T("fail"));
        closesocket(socketHandle);
        WSACleanup();
        return FALSE;
    }
}

I hope you can help me solve it.

0

There are 0 answers