2016-06-19 21 views
0

範囲内のBluetoothデバイスを一覧表示し、特定のBluetoothデバイス(HC-05 Bluetoothモジュール)を検索するWindowのBluetooth APIのみを使用してプログラムを作成しました。検索が成功すると、プログラムはデバイスのサービス状態を認証して設定し、シリアルポートGUID(1101)を使用して仮想コンポートを作成します。私の目的は、ユーザーを煩わせることなくプログラム的にBTデバイスとペアリングして通信することです。C++でWindowのBluetooth APIを使用してBluetoothデバイスに接続できません

以下は、認証と仮想ポートの作成が試行される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); 

} 

このすべてが正常に動作しますが、問題はさらに仮想コンポートの認証、作成後に、デバイスの接続状態が偽のままということです。

Image of Program Output

シリアル端末は、上記で作成したとの通信を試みると、エラーが次の返しコンポート:

Element not found. (1168)

、Iは、デバイスを認証するコールバックメソッドを使用して疲れたが、それが動作していません

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

私はresourを見つけることができなかったので、このコードをデバッグすることができたら本当に感謝します。ウィンドウズのBluetooth APIの使用に関するチュートリアル。

Hereは完全なコードです。

+0

英語は母国語ではありませんが、できるだけ文法的に正確であるように最善を尽くしました。 –

答えて

0

まず、クイック(最初の)回答として、このAPIセット(「Bluetooth」)であなたは「通信」することができません。 https://msdn.microsoft.com/en-us/library/aa362928(v=VS.85).aspx(ここではどこにも言及していないので、あなたはそれを使用していないと思われます) ここには多くの記事がありますが、私はアドバイスできますこの偉大なチュートリアルも読んでくださいhttp://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4j.html

+0

私はwinsockの「接続」機能を試してみましたが、今は問題は接続機能が呼び出されてユーザーがデバイスとのペアリングを要求されたときです。どうすればこの問題を回避できますか?私は接続関数の前に認証を試みて、認証コールバック関数を試しましたが、両方とも機能しませんでした。 –

+0

認証を自動処理するには、BluetoothWin32Authenticationを使用し、必要なSSP情報、つまり古いデバイスのPINを提供するコールバックを処理します。 – alanjmcf

関連する問題