无法使用C ++中的Window蓝牙API连接蓝牙设备

时间:2016-06-19 14:58:36

标签: c++ winapi bluetooth

我使用Window的蓝牙API创建了一个程序,该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);

}

所有这一切都运行正常,但问题是即使在验证和创建虚拟comport之后,设备连接状态仍然是错误的。

Image of Program Output

当串行终端尝试与上面创建的comport通信时,它返回以下错误:

  

找不到元素。 (1168)

我已经厌倦了使用回调方法来验证设备,但它无法正常工作,返回错误,例如:

  

设备未连接。 (1167)   设备无法识别该命令。 (22)

如果有人可以调试此代码,我真的很感激,因为我无法找到使用窗口蓝牙API的资源或教程。

Here是完整的代码。

1 个答案:

答案 0 :(得分:1)

首先,作为快速(第一)答案,您无法进行“沟通”。 (使用这组API发送/读取数据)(&#39;蓝牙&#39;):您需要使用&#39; socket&#39;这里描述的蓝牙API:https://msdn.microsoft.com/en-us/library/aa362928(v=VS.85).aspx(因为你没有在任何地方提及它,我认为你没有使用它,对吧?) 这里有很多关于它的帖子,但我可以建议你阅读这个很棒的教程http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4j.html