简体   繁体   English

是否可以通过Windows C ++应用程序中的唯一地址检测到蓝牙设备?

[英]Can a Bluetooth device be detected by its unique address from Windows C++ application?

I have to write an application which has to identify "ESP32" devices & send/receive data from a Windows C++ application. 我必须编写一个应用程序,该应用程序必须标识“ ESP32”设备并从Windows C ++应用程序发送/接收数据。

Q1: I am using WSALookupServiceBegin() API to find the BT device, it was not working as expected. Q1:我正在使用WSALookupServiceBegin() API查找BT设备,它没有按预期工作。 API returns 10108 until I manually click on "Add devices" in Windows Bluetooth window. API返回10108,直到我在Windows蓝牙窗口中手动单击“添加设备”。 Is there any other API/service which can discover BT near devices or am I using the WSALookupServiceBegin() API wrongly? 是否有其他API /服务可以在设备附近发现BT,还是我错误地使用WSALookupServiceBegin() API? Is WSALookupServiceBegin() takes the device data from the Bluetooth cache? WSALookupServiceBegin()是否从蓝牙缓存中获取设备数据? I got this doubt because API works fine only after manual search in Windows. 我对此表示怀疑,因为API仅在Windows中手动搜索后才能正常工作。

Q2: is it possible to connect to any Bluetooth device just with the Mac ID of BT device from Windows? 问题2:是否可以从Windows仅使用BT设备的Mac ID连接到任何蓝牙设备?

Please find the code below. 请在下面找到代码。

WSAQUERYSET data;
HANDLE handle;
ZeroMemory(&data, sizeof(data));
data.dwSize = sizeof(data);
data.dwNameSpace = NS_BTH;
data.lpcsaBuffer = NULL;

WSALookupServiceBegin(&data, LUP_CONTAINERS, &handle);
while(WSALookupServiceNext(hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR, 
&dwSize, pwsaResults)
{
 service_classID = pwsaResults->lpServiceClassId;
 _BTH_DEVICE_INFO *dev = (_BTH_DEVICE_INFO *)pwsaResults->lpBlob->pBlobData;

    SOCKET          LocalSocket = INVALID_SOCKET;
    SOCKADDR_BTH    SockAddrBthServer;

    SockAddrBthServer.btAddr = dev->address;
    SockAddrBthServer.addressFamily = AF_BTH;
    SockAddrBthServer.serviceClassId = *service_classID;
    SockAddrBthServer.port = 0;

    // connect to socket
    LocalSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
    if (INVALID_SOCKET == LocalSocket) {
        wprintf(L"socket() call failed. WSAGetLastError = [%d]\n", WSAGetLastError());
        return -1;
    }

    if (SOCKET_ERROR == connect(LocalSocket,
        (struct sockaddr *) &SockAddrBthServer,
        sizeof(SOCKADDR_BTH))) {
        wprintf(L"connect() call failed. WSAGetLastError=[%d]\n", WSAGetLastError());
        return -1;
    }

}

OUTPUT : 输出:

connect() call failed. connect()调用失败。 WSAGetLastError=[10049] WSAGetLastError = [10049]

WSALookupServiceBegin is correct way but you have to provide correct flags for it. WSALookupServiceBegin是正确的方法,但是您必须为其提供正确的标志。 Also you can use BluetoothFidnFirstDevice and BluetoothFindNextDevice functions from Bluetooth API. 您也可以使用Bluetooth API中的BluetoothFidnFirstDeviceBluetoothFindNextDevice函数。

However both methods always return paired devices even they are not available (together with just found devices). 但是,即使它们不可用,这两种方法也总是返回配对的设备(以及刚刚找到的设备)。

From your description it looks you did not provide correct flags for WSAxxx function. 从您的描述看来,您没有为WSAxxx功能提供正确的标志。

If you know device's MAC and it has not been changed then you can connect to device by MAC without rediscovering it each time. 如果您知道设备的MAC并且尚未更改,则可以通过MAC连接到设备,而无需每次都重新发现它。 Depending on your device's authentication requirement you even do not need to pair with device (of course if your device does not need authentication and/or encryption). 根据设备的身份验证要求,甚至不需要与设备配对(当然,如果您的设备不需要身份验证和/或加密)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Windows中的蓝牙设备与c ++配对 - Pairing a bluetooth device in windows with c++ 知道蓝牙设备的名称时,能否获得它的蓝牙MAC地址? - Can you get the Bluetooth MAC Address of a Bluetooth Device when you have its name? 来自C ++ / CX的设备唯一ID - Device unique id from C++/CX 如何在C ++中运行应用程序并写入其stdin并在Windows中从其stdout读取 - how to run an application in c++ and write to its stdin and read from its stdout in windows Qt/C++ 连接经典蓝牙设备 - Qt/C++ Connect to Classic Bluetooth device 从 Android 上的 C++ 获取唯一设备标识符 - Get unique device identifier from c++ on Android 如何从 Windows 套接字 (C++) 获取连接主机的 IP 地址? - How can I get the connected host's IP address from a Windows Socket (C++)? 如何从Windows Service主要功能启动C ++本机应用程序(该应用程序与控制台交互)? - How can I launch a C++ native application from a Windows Service main function (the application interacts with the console)? 是什么会导致SetupDiGetClassDevs不返回任何设备接口? (Windows,C ++) - What can cause SetupDiGetClassDevs to return no device interfaces? (Windows, C++) Windows C++:绝对memory地址读取系统时间? - Windows C++: absolute memory address to read system time from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM