串口蓝牙断开检测c++



如何检测蓝牙设备在windows上是否断开连接?

这是我尝试的:

//return true if there is a connection
bool works()
{
//portcom(handle to virtual serial port) and dcb are already initialised
if(!GetCommState(portcom, &dcb)) return 0; 
return SetCommState(portcom, &dcb);
}

但它只检查是否存在港口和它的存在(因为蓝牙端口不关闭)。所以这行不通:(

我想你可以通过枚举蓝牙设备来检查蓝牙是否连接.

蓝牙api

使用BluetoothFindFirstDeviceBluetoothFindNextDevice枚举设备,通过BLUETOOTH_DEVICE_INFO_STRUCT结构中的fConnected参数判断蓝牙是否连接。

typedef struct _BLUETOOTH_DEVICE_INFO {
DWORD             dwSize;
BLUETOOTH_ADDRESS Address;
ULONG             ulClassofDevice;
BOOL              fConnected;
BOOL              fRemembered;
BOOL              fAuthenticated;
SYSTEMTIME        stLastSeen;
SYSTEMTIME        stLastUsed;
WCHAR             szName[BLUETOOTH_MAX_NAME_SIZE];
} BLUETOOTH_DEVICE_INFO_STRUCT;

最新更新