我仅使用 Windows 的蓝牙 API 创建了一个程序,该程序列出范围内的蓝牙设备,然后搜索特定的蓝牙设备(HC-05 蓝牙模块)。如果搜索成功,程序将验证并设置设备的服务状态,以使用串行端口 GUID (1101) 创建虚拟端口。我的目标是以编程方式与 BT 设备配对和通信,而不打扰用户。
下面是 main() 的一部分,其中尝试验证和创建虚拟端口。
if(desired_device_info.fAuthenticated==FALSE){ //if device is not authenticated then,
BluetoothGetDeviceInfo(m_radio,&desired_device_info); //get updated device information
if(!pairDevice(desired_device_info)){//attempt to pair with the device.
cout<<"Authentication failed, Try manually"<<endl;
CloseAllHandle();
return 0;}
}
ret=BluetoothSetServiceState(m_radio,&desired_device_info,&serial,BLUETOOTH_SERVICE_ENABLE);
if(ret !=ERROR_SUCCESS && ret!=E_INVALIDARG){
if(ret == ERROR_INVALID_PARAMETER)
cout<< "Invalid Parameter" << endl;
if(ret == ERROR_SERVICE_DOES_NOT_EXIST)
cout<< "Service not found" << endl;
cout<<"Press any key to exit"<<endl;
CloseAllHandle();
x=_getch();
return 0;
}
BluetoothGetDeviceInfo(m_radio,&desired_device_info); //get updated device infor
BluetoothUpdateDeviceRecord(&desired_device_info);
上面片段中使用的pairDevice()函数是:
bool pairDevice(BLUETOOTH_DEVICE_INFO device){
DWORD errorCode;
bool result=false;
//wchar_t passKey=L'1234\n';
PWSTR * passKey = new PWSTR[1];
passKey[0]=L"1234";// this is the default pass key/pin code for HC-05, can be changed to a custom value.
errorCode=BluetoothAuthenticateDevice(NULL,m_radio,&device,*passKey,4); //here 4 is the size of device passkey
//errorCode=BluetoothRegisterForAuthenticationEx(&device, &hRegHandle, (PFN_AUTHENTICATION_CALLBACK_EX)&bluetoothAuthCallback, NULL);
// if(errorCode != ERROR_SUCCESS)
// {
// fprintf(stderr, "BluetoothRegisterForAuthenticationEx ret %d\n", errorCode);
// CloseAllHandle();
// _getch();
// return false;
// //ExitProcess(2);
//
// }
//errorCode = BluetoothAuthenticateDeviceEx(NULL,m_radio, &device, NULL, MITMProtectionNotRequired);
switch(errorCode)
{case(ERROR_SUCCESS):
cout<<"Device authenticated successfully"<<endl;
result=true;
break;
case(ERROR_CANCELLED):
cout<<"Device authenticated failed"<<endl;
result=false;
break;
case(ERROR_INVALID_PARAMETER):
cout<<"Invalid parameters"<<endl;
result=false;
break;
case(ERROR_NO_MORE_ITEMS):
cout<<"Device not available"<<endl;
result=false;
break;
}
if(errorCode != ERROR_SUCCESS)
cout<<"Failure due to: "<<GetLastError() <<endl;
return result;
}
void CloseAllHandle(void){
if(CloseHandle(m_radio) == FALSE){
cout<<"CloseHandle() failed with error code "<< GetLastError()<<endl;
}
BluetoothUnregisterAuthentication(hRegHandle);
}
所有这些工作正常,但问题是,即使在身份验证和创建虚拟端口之后,设备连接状态仍然是错误的。
当串行终端尝试与上面创建的端口通信时,它会返回以下错误:
Element not found. (1168)
我厌倦了使用回调方法来验证设备,但它不起作用,返回错误,例如:
The device is not connected. (1167) The device does not recognize the command. (22)
如果有人可以调试此代码,我将非常感激,因为我无法找到有关使用 Windows 蓝牙 API 的资源或教程。
Here是完整的代码。
最佳答案
首先,作为一个快速(第一个)答案,您将无法使用这组 API(“蓝牙”)进行“通信”(向/从...发送/读取数据):您需要使用“套接字”蓝牙 API,如下所述:https://msdn.microsoft.com/en-us/library/aa362928(v=VS.85).aspx (因为你没有在任何地方提到它,我认为你没有使用它,对吧?) 这里有很多关于它的帖子,但我可以建议您也阅读这个很棒的教程 http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4j.html
关于c++ - 无法使用 C++ 中的 Window 蓝牙 API 连接到蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37908566/