mardi 21 juin 2016

Unable to connect to Bluetooth Device using Window's Bluetooth API in C++

I have created a program using only Window's Bluetooth API that lists Bluetooth devices within range then searches for a specific Bluetooth device (HC-05 Bluetooth module). If the search is successful,the program authenticates and set service state of device to create a virtual comport using Serial Port GUID(1101). My aim is to programmatically pair and communicate with BT device without bothering the user.

Below is a segment of main(), where authentication and creation of virtual port is attempted.

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);

The pairDevice() function used in above segment is:

bool pairDevice(BLUETOOTH_DEVICE_INFO device){

    DWORD errorCode;
    bool result=false;
    //wchar_t passKey=L'1234n';
        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 %dn", 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);

}

All of this works fine but the problem is that even after authentication and creation of a virtual comport the device connection status remains false.

Image of Program Output

When a serial terminal attempts to communicate with above created comport it returns following error:

Element not found. (1168)

I have tired using a callback method to authenticate the device, but it is not working, returning errors such as:

The device is not connected. (1167) The device does not recognize the command. (22)

I would really appreciate if some one could debug this code as I was unable to find resources or tutorial on using window's Bluetooth APIs.

Here is the complete code.

Aucun commentaire:

Enregistrer un commentaire