Guest User

Bluetooth Device search and connect

a guest
Jun 19th, 2016
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.32 KB | None | 0 0
  1.  
  2.  
  3. //This program discovers and list all bluetooth devices in range and attempts to establish connections
  4. //The program starts with looking at the status of bluetooth on computer(i.e. Local Radio) by calling IsBluetoothOn()
  5. //if it is Off then it attempts to open local radio of computer by calling func TurnOnBluetooth()
  6. //if the function fails to open bluetooth, it prints to request user to enable bluetooth manually and restart program
  7. //then when bluetooth radio of computer is enabled, the program attempts to find all the device within range and
  8. //finds the device named hc-05 by using the function FindBtDev(). This function if successful will return the data
  9. //about bluetooth module to main function, if function fails then the program notify user that unable to find device and
  10. //after some delay again looks for the device.
  11. //finally when the device is found and data is return by FindBtDev() then status of connection and authentication of device
  12. //is checked, if device is not authenticated (i.e false) then the pairDevice() function is called to attempt to pair with device.
  13. //finally the BluetoothSetService function is used to set seiral serivce class and enable service of the device. this creates a
  14. //virtual com.
  15. //BUGS and ISSUES:
  16. // the trunning on bluetooth radio function is not working at all. need to debug it.
  17.  
  18. //==========Header============================
  19. #include <winsock2.h>
  20. #include <windows.h>
  21. #include <ws2bth.h>
  22. #include <BluetoothAPIs.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string>
  26. #include <iostream>
  27. #include <conio.h>
  28. #include <initguid.h>
  29. #include <windef.h>
  30. //#include <tchar.h>
  31. #pragma comment(lib, "ws2_32.lib")
  32. #pragma comment(lib, "irprops.lib")
  33. #pragma comment(lib, "Bthprops.lib")
  34. using namespace std;
  35.  
  36. #define MAX_NAME 248
  37.  
  38. typedef DWORD (WINAPI *f_BluetoothEnableRadio)(BOOL);
  39. typedef int (WINAPI* f_BthSetMode)(enum RadioMode);
  40.  
  41. //================================function prototypes===================================================================================
  42. bool TurnOnBluetooth(void);//attempts to turn on bluetooth radio of computer and returns true if successful and false if error occurs.
  43. int IsBluetoothOn(void);//returns 0 if bluetooth radio off, returns 1 if On and returns -1 if the function failed to complete.
  44. bool pairDevice(BLUETOOTH_DEVICE_INFO); //returns true if successful and false if not takes bluetooth deivce info struct.
  45. bool FindBtDev(BLUETOOTH_DEVICE_INFO*); //returns true if successful and false if not takes bluetooth deivce info struct's pointer.
  46. void CloseAllHandle(void);//a function to close all windows handles. wil be called at last as a clean up function.
  47. //======================================================================================================================================
  48.  
  49. HANDLE m_radio ; //Windows handle to local radio, declared here as a global variable
  50. HBLUETOOTH_AUTHENTICATION_REGISTRATION hRegHandle = 0;
  51. /// Bluetooth states.
  52. enum RadioMode //enumeration created for bluetooth radio state.
  53. {
  54. /// Bluetooth off.
  55. Off,
  56. /// Bluetooth is on but not discoverable.
  57. On,
  58. /// Bluetooth is on and discoverable.
  59. Discoverable,
  60. };
  61.  
  62.  
  63.  
  64. BOOL CALLBACK bluetoothAuthCallback(LPVOID pvParam, PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
  65. {
  66. DWORD dwRet;
  67. BLUETOOTH_AUTHENTICATE_RESPONSE response;
  68. BLUETOOTH_PIN_INFO HcPin;
  69. HcPin.pin[0]='1';
  70. HcPin.pin[1]='2';
  71. HcPin.pin[2]='3';
  72. HcPin.pin[3]='4';
  73. HcPin.pinLength=4;
  74. BLUETOOTH_PASSKEY_INFO pass={1234};
  75. ::ZeroMemory(&response,sizeof(BLUETOOTH_AUTHENTICATE_RESPONSE));
  76. response.authMethod = pAuthCallbackParams->authenticationMethod;
  77. response.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
  78. response.pinInfo= HcPin;
  79. response.negativeResponse = FALSE;
  80. response.passkeyInfo=pass;
  81. fprintf(stderr, "BluetoothAuthCallback 0x%x\n", pAuthCallbackParams->deviceInfo.Address.ullLong);
  82.  
  83. //dwRet = BluetoothSendAuthenticationResponse(NULL, &(pAuthCallbackParams->deviceInfo), L"1234");
  84. dwRet = BluetoothSendAuthenticationResponseEx(m_radio, &response);
  85. if(dwRet != ERROR_SUCCESS)
  86. {
  87. fprintf(stderr, "BluetoothSendAuthenticationResponse ret %d\n", dwRet);
  88. //ExitProcess(2);
  89. return TRUE;
  90. }
  91. fprintf(stderr, "BluetoothAuthCallback finish\n");
  92. // ExitProcess(0);
  93. return FALSE;
  94. }
  95.  
  96.  
  97.  
  98.  
  99. int main(int argc, char ** argv) //main code here.
  100.  
  101. {
  102. //======================variables===============================================
  103. char x;
  104. DWORD ret;
  105. const GUID serial=SerialPortServiceClass_UUID;
  106. bool retVal;
  107. int IsOn; //variable that tells whether function is working or not
  108. BLUETOOTH_DEVICE_INFO desired_device_info={sizeof(BLUETOOTH_DEVICE_INFO),0,}; //a struct variable that will store info of desired device
  109. // DWORD numServices;
  110. // GUID guidServices[10];
  111.  
  112. //==========================================================================================
  113.  
  114. IsOn=IsBluetoothOn(); //start by checking wether bluetooth module is on or off
  115.  
  116. if(IsOn==0){ //i.e the device is not ON then//then ask user to turn on the bluetooth
  117.  
  118. //attempt to turn on the device
  119. //if(!TurnOnBluetooth()){
  120. //if result of our above attempt is zero then failed to open bluetooth module
  121. // then ask user to enable bluetooth manually,
  122. cout<<"Enable bluetooth Manually and restart program"<<endl;
  123. cout<<"Press any key to exit"<<endl;
  124. x=_getch();
  125. return 0;
  126. //}
  127.  
  128.  
  129. }else if(IsOn==-1){
  130. cout<<"Error Occured Closing Program"<<endl;
  131. cout<<"Press any key to exit"<<endl;
  132. x=_getch();
  133. return 0;
  134. }
  135.  
  136. //if IsOn==1 then local bluetooth is on
  137. retVal= FindBtDev(&desired_device_info);
  138. if(retVal==false)
  139. {//device not found
  140. cout<<"Error cannot find device"<<endl;
  141. cout<<"Searching again, Press any key to exit program"<<endl;
  142. while(FindBtDev(&desired_device_info)==false){ //contiune looking until you find the device
  143. Sleep(1000);
  144. if(_getch()){ //if user press any key exit from the search
  145. CloseAllHandle();
  146. return 0;}
  147. }
  148.  
  149. }
  150.  
  151. if(!BluetoothIsConnectable(m_radio)) //check if local bluetooth radios accept incomming connections
  152. { //if not then attempt to turn this true
  153. cout<<"Incomming connection was not ON, turning it On"<<endl;
  154. if(BluetoothEnableIncomingConnections(m_radio,TRUE))
  155. cout<<"Incomming connections enabled"<<endl;
  156. else
  157. cout<<"Error Unable to enable incoming connections"<<endl;
  158. }else
  159. cout<<"Incomming connection was ON!"<<endl;
  160.  
  161. if(desired_device_info.fConnected==FALSE && desired_device_info.fRemembered==TRUE){ //iff device is connected, then will we attempt to establish comport communication
  162. cout<<"Device is out of range or switched Off."<<endl;
  163. }
  164.  
  165.  
  166.  
  167. if(desired_device_info.fRemembered==FALSE && desired_device_info.fConnected==FALSE){ //if the device is not remembered then attempt to create authentication and comport.
  168. cout<<"Device Found Attempting to connect"<<endl;
  169.  
  170. if(desired_device_info.fAuthenticated==FALSE){ //if device is not authenticated then,
  171. BluetoothGetDeviceInfo(m_radio,&desired_device_info); //get updated device information
  172. if(!pairDevice(desired_device_info)){//attempt to pair with the device.
  173. cout<<"Authentication failed, Try manually"<<endl;
  174. CloseAllHandle();
  175. return 0;}
  176. }
  177.  
  178. ret=BluetoothSetServiceState(m_radio,&desired_device_info,&serial,BLUETOOTH_SERVICE_ENABLE); //this will make the device as bluetooth com port hopefully
  179. if(ret !=ERROR_SUCCESS && ret!=E_INVALIDARG){// if the operation is not successful and does not contain invalid argument
  180. if(ret == ERROR_INVALID_PARAMETER)
  181. cout<< "Invalid Parameter" << endl;
  182.  
  183. if(ret == ERROR_SERVICE_DOES_NOT_EXIST)
  184. cout<< "Service not found" << endl;
  185.  
  186. cout<<"Press any key to exit"<<endl;
  187. CloseAllHandle();
  188. x=_getch();
  189. return 0;
  190. }
  191.  
  192. /*ret = BluetoothEnumerateInstalledServices(m_radio, &desired_device_info, &numServices, guidServices);
  193. if(ret !=ERROR_SUCCESS)
  194. cout<<"Error getting device service. error ID: "<<ret<<endl;*/
  195.  
  196. BluetoothGetDeviceInfo(m_radio,&desired_device_info); //get updated device infor
  197.  
  198. BluetoothUpdateDeviceRecord(&desired_device_info);
  199.  
  200. cout<<"Name: "<<desired_device_info.szName <<endl;
  201.  
  202. wprintf(L" \tAddress: %02X:%02X:%02X:%02X:%02X:%02X\r\n", desired_device_info.Address.rgBytes[5],
  203.  
  204. desired_device_info.Address.rgBytes[4], desired_device_info.Address.rgBytes[3], desired_device_info.Address.rgBytes[2],
  205.  
  206. desired_device_info.Address.rgBytes[1], desired_device_info.Address.rgBytes[0]);
  207.  
  208. wprintf(L" \tConnected: %s\r\n", desired_device_info.fConnected ? L"true" : L"false");
  209. wprintf(L" \tAuthenticated: %s\r\n", desired_device_info.fAuthenticated ? L"true" : L"false");
  210. wprintf(L" \tRemembered: %s\r\n", desired_device_info.fRemembered ? L"true" : L"false");
  211.  
  212.  
  213. cout<<"Operation Successful check if comport created"<<endl;
  214. }//end of if device is not remembered.
  215.  
  216.  
  217. cout<<"Press any key to exit"<<endl;
  218.  
  219. CloseAllHandle();
  220. x=_getch();
  221.  
  222. return 0;
  223.  
  224. } //end of main
  225.  
  226.  
  227. //bool TurnOnBluetooth(void){
  228.  
  229. // OSVERSIONINFO osvi; //this struct will store the version of windows
  230. // DWORD dwType = REG_SZ;
  231. // HKEY hKey;
  232. // BOOL fEnable=TRUE; //because its the turn on function
  233. // HINSTANCE hGetProcIDDLL;
  234. // TCHAR value[1024]; //will store the address to the dll file we need to load
  235. // DWORD value_length = 1024*sizeof(TCHAR);
  236. // const std::string& location = "SYSTEM\\CurrentControlSet\\Services\\BTHPORT\\Parameters\\Radio Support";
  237. // const std::string& ValueName = "SupportDLL";
  238. //
  239. //
  240. // ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); //allocating memory by filing with zeros
  241. // osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  242. //
  243. // GetVersionEx(&osvi);
  244. //
  245. // if((osvi.dwMajorVersion <= 6) && osvi.dwMinorVersion <= 1){ // windows 7 or less then
  246. //
  247. // if(ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE,location.c_str(),0, KEY_QUERY_VALUE,&hKey)) //go to the above registry location
  248. // {
  249. // cout<<"Error Locating Bluetooth Module,Check Bluetooth Driver"<<endl;
  250. // RegCloseKey(hKey); //close this handle
  251. // return 0;
  252. //
  253. //
  254. // }else if(ERROR_SUCCESS !=RegQueryValueExA(hKey, ValueName.c_str(), 0, 0, (LPBYTE) value, &value_length))
  255. // { //if registry location is found then look for the valueName and get its string Value
  256. // cout<<"Bluetooth Driver might be corrupt,reinstall driver or Enable Manually"<<endl;
  257. // RegCloseKey(hKey); //close this handle
  258. // return 0;
  259. // }
  260. //
  261. // RegCloseKey(hKey); //close this handle
  262. //
  263. // std::wstring stringValue =std::wstring(value, (size_t)value_length - 1);
  264. // size_t i = stringValue.length();
  265. // while( i > 0 && stringValue[i-1] == '\0' ){
  266. // --i;
  267. // }
  268. // stringValue=stringValue.substr(0,i);
  269. //
  270. // size_t count = stringValue.find('/');
  271. // while (count != string::npos){
  272. //
  273. // stringValue[count]='\\'+'\\';
  274. // count= stringValue.find('\\', i + 1);
  275. // }
  276. //
  277. // cout<<"The bluetooth SUpport DLL is located in:"<<endl;
  278. // cout<<stringValue.c_str()<<endl;
  279. // hGetProcIDDLL = LoadLibrary(stringValue.c_str()); //load the dll
  280. // if (hGetProcIDDLL == NULL) {
  281. // cout << "cannot locate the bluetooth driver file, Enable Manually" << endl;
  282. // FreeLibrary(hGetProcIDDLL); //unload the library and free the handle
  283. // return 0;
  284. // }
  285. // //now get the function and run it.
  286. // f_BluetoothEnableRadio BluetoothEnableRadio= (f_BluetoothEnableRadio)GetProcAddress(hGetProcIDDLL,"BluetoothEnableRadio");
  287. // //return error if function was not found
  288. // if(!BluetoothEnableRadio)
  289. // {
  290. // cout << "Driver does not provide Software switch of bluetooth, Enable Manually" << endl;
  291. // FreeLibrary(hGetProcIDDLL); //unload the library and free the handle
  292. // return 0;
  293. // }
  294. //
  295. // if(ERROR_SUCCESS !=BluetoothEnableRadio(fEnable)){
  296. //
  297. // cout << "Unable to switch ON bluetooth module, Enable Manually" << endl;
  298. // FreeLibrary(hGetProcIDDLL); //unload the library and free the handle
  299. // return 0;
  300. // }
  301. // cout<<"Bluetooth Module Enabled"<<endl;
  302. // FreeLibrary(hGetProcIDDLL); //unload the library and free the handle
  303. //
  304. // }else{
  305. // cout<<"Software not fully compatibile with current version of Windows"<< endl;
  306. // cout<<"Cannot Enable Bluetooth Automatically"<<endl;
  307. // return 0;
  308. // }
  309. //return 1; //the operation was successfull
  310. //}
  311.  
  312.  
  313.  
  314. //return false if error occured and true if operation is successful
  315. int IsBluetoothOn(void){
  316. int ErrorCode;
  317. HANDLE r_radio;
  318. HBLUETOOTH_RADIO_FIND m_bt;
  319. BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {sizeof(BLUETOOTH_FIND_RADIO_PARAMS)}; //a struct that needs to be passed
  320. int result;
  321. //do a search now to check
  322. m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &r_radio);
  323. if(m_bt != NULL){
  324. cout<<"Bluetooth Radio Found !"<<endl;
  325. result=1;
  326. if(CloseHandle(r_radio) == FALSE){
  327. cout<<"CloseHandle() failed with error code "<< GetLastError()<<endl;
  328. result= -1;
  329. } //close the handle
  330.  
  331.  
  332. }else{
  333. ErrorCode=GetLastError();
  334. result= -1;
  335. if(ErrorCode == ERROR_NO_MORE_ITEMS){
  336. cout<<"Bluetooth Module not enabled !"<<endl;
  337. result= 0;
  338. }else
  339.  
  340. cout<<"Bluetooth Radio search failed with error code"<< ErrorCode<<endl;
  341. }
  342.  
  343.  
  344.  
  345. return result;
  346. }
  347.  
  348. bool FindBtDev(BLUETOOTH_DEVICE_INFO *desired_device_info){
  349. //======================Handles============================================================
  350.  
  351. HBLUETOOTH_RADIO_FIND m_bt;
  352. HBLUETOOTH_DEVICE_FIND m_bt_dev ;
  353. //==========================================================================================
  354. //================VAriables=================================================================
  355. int m_radio_id;
  356. int m_device_id;
  357. DWORD mbtinfo_ret;
  358. bool FuncResult=false;
  359.  
  360. //=================================================================================================
  361. //=======================Struct=====================================================================
  362. BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {sizeof(BLUETOOTH_FIND_RADIO_PARAMS)};
  363. BLUETOOTH_RADIO_INFO m_bt_info = {sizeof(BLUETOOTH_RADIO_INFO),0,};
  364. BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
  365. sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
  366. 1,
  367. 0,
  368. 1,
  369. 1,
  370. 1,
  371. 15,
  372. NULL};
  373. BLUETOOTH_DEVICE_INFO m_device_info = {sizeof(BLUETOOTH_DEVICE_INFO),0,};
  374. //================================================================================================
  375. // Iterate for available bluetooth radio devices in range
  376. // Starting from the local
  377.  
  378.  
  379. m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);
  380. if(m_bt == NULL){
  381. //cout<<"Bluetooth Radio Found !"<<endl;
  382. cout<<"Bluetooth Radio failed with error code"<< GetLastError()<<endl;
  383. FuncResult= false;
  384. }
  385.  
  386.  
  387. m_radio_id = 0;
  388.  
  389.  
  390.  
  391. do {
  392.  
  393. // Then get the radio device info....
  394.  
  395. mbtinfo_ret = BluetoothGetRadioInfo(m_radio, &m_bt_info);
  396.  
  397. if(mbtinfo_ret == ERROR_SUCCESS)
  398.  
  399. cout<<"Bluetooth Radio looks fine!"<<endl;
  400.  
  401. else{
  402.  
  403. cout<<"BluetoothGetRadioInfo() failed wit error code "<< mbtinfo_ret<<endl;
  404. FuncResult= false;
  405. }
  406.  
  407. //commented out radio information i dont need.
  408. /*wprintf(L"Radio %d:\r\n", m_radio_id);
  409.  
  410. wprintf(L"\tInstance Name: %s\r\n", m_bt_info.szName);
  411.  
  412. wprintf(L"\tAddress: %02X:%02X:%02X:%02X:%02X:%02X\r\n", m_bt_info.address.rgBytes[5],
  413.  
  414. m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[2],
  415.  
  416. m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[0]);
  417.  
  418. wprintf(L"\tClass: 0x%08x\r\n", m_bt_info.ulClassofDevice);
  419.  
  420. wprintf(L"\tManufacturer: 0x%04x\r\n", m_bt_info.manufacturer);*/
  421.  
  422.  
  423.  
  424. m_search_params.hRadio = m_radio;
  425.  
  426. ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
  427.  
  428. m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
  429.  
  430.  
  431.  
  432. // Next for every radio, get the device
  433.  
  434. m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
  435.  
  436.  
  437.  
  438. if(m_bt_dev != NULL)
  439.  
  440. cout<<"BluetoothFindFirstDevice() is working!"<<endl;
  441.  
  442. else{
  443.  
  444. cout<<"Failed to find Devices with error code: "<<GetLastError()<<endl;
  445. FuncResult= false;
  446. }
  447.  
  448.  
  449.  
  450. m_radio_id++;
  451.  
  452. m_device_id = 0;
  453.  
  454.  
  455.  
  456. //Get the device info
  457.  
  458. do
  459.  
  460. {
  461.  
  462. //-----this list all the bluetooth devices nearby, this can be commented out.
  463. wprintf(L"\n\tDevice %d:\r\n", m_device_id);
  464.  
  465. wprintf(L" \tInstance Name: %s\r\n", m_device_info.szName);
  466.  
  467. wprintf(L" \tAddress: %02X:%02X:%02X:%02X:%02X:%02X\r\n", m_device_info.Address.rgBytes[5],
  468.  
  469. m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[2],
  470.  
  471. m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[0]);
  472.  
  473. wprintf(L" \tClass: 0x%08x\r\n", m_device_info.ulClassofDevice);
  474.  
  475. wprintf(L" \tConnected: %s\r\n", m_device_info.fConnected ? L"true" : L"false");
  476.  
  477. wprintf(L" \tAuthenticated: %s\r\n", m_device_info.fAuthenticated ? L"true" : L"false");
  478.  
  479. wprintf(L" \tRemembered: %s\r\n", m_device_info.fRemembered ? L"true" : L"false");
  480.  
  481. m_device_id++;
  482. //------------------------------------------------------------------------
  483.  
  484. if(wcscmp(m_device_info.szName,L"HC-05")==0) //this search specifically for hc-05 bluetooth module.
  485. {
  486. *desired_device_info=m_device_info;
  487. FuncResult= true;
  488. break;
  489. }else{
  490. FuncResult= false;
  491. }
  492.  
  493.  
  494. } while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));
  495.  
  496.  
  497.  
  498. // NO more device, close the device handle
  499.  
  500. if(BluetoothFindDeviceClose(m_bt_dev) == TRUE)
  501.  
  502. cout<<"BluetoothFindDeviceClose(m_bt_dev) is OK!"<<endl;
  503.  
  504. else{
  505.  
  506. cout<<"BluetoothFindDeviceClose(m_bt_dev) failed with error code "<< GetLastError()<<endl;
  507. FuncResult= false;
  508.  
  509. }
  510.  
  511.  
  512.  
  513. } while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
  514.  
  515.  
  516.  
  517. // No more radio, close the radio handle
  518.  
  519. if(BluetoothFindRadioClose(m_bt) == TRUE)
  520.  
  521. cout<<"BluetoothFindRadioClose(m_bt) is OK!"<<endl;
  522.  
  523. else{
  524.  
  525. cout<<"BluetoothFindRadioClose(m_bt) failed with error code "<< GetLastError()<<endl;
  526. FuncResult= false;
  527. }
  528.  
  529. return FuncResult;
  530. }
  531.  
  532. bool pairDevice(BLUETOOTH_DEVICE_INFO device){
  533.  
  534. DWORD errorCode;
  535. bool result=false;
  536. //wchar_t passKey=L'1234\n';
  537. PWSTR * passKey = new PWSTR[1];
  538. passKey[0]=L"1234";// this is the default pass key/pin code for HC-05, can be changed to a custom value.
  539. errorCode=BluetoothAuthenticateDevice(NULL,m_radio,&device,*passKey,4); //here 4 is the size of device passkey
  540.  
  541. //errorCode=BluetoothRegisterForAuthenticationEx(&device, &hRegHandle, (PFN_AUTHENTICATION_CALLBACK_EX)&bluetoothAuthCallback, NULL);
  542. // if(errorCode != ERROR_SUCCESS)
  543. // {
  544. // fprintf(stderr, "BluetoothRegisterForAuthenticationEx ret %d\n", errorCode);
  545. // CloseAllHandle();
  546. // _getch();
  547. // return false;
  548. // //ExitProcess(2);
  549. //
  550. // }
  551.  
  552.  
  553. //errorCode = BluetoothAuthenticateDeviceEx(NULL,m_radio, &device, NULL, MITMProtectionNotRequired);
  554. switch(errorCode)
  555. {case(ERROR_SUCCESS):
  556. cout<<"Device authenticated successfully"<<endl;
  557. result=true;
  558. break;
  559. case(ERROR_CANCELLED):
  560. cout<<"Device authenticated failed"<<endl;
  561. result=false;
  562. break;
  563. case(ERROR_INVALID_PARAMETER):
  564. cout<<"Invalid parameters"<<endl;
  565. result=false;
  566. break;
  567. case(ERROR_NO_MORE_ITEMS):
  568. cout<<"Device not available"<<endl;
  569. result=false;
  570. break;
  571. }
  572.  
  573. if(errorCode != ERROR_SUCCESS)
  574. cout<<"Failure due to: "<<GetLastError() <<endl;
  575.  
  576. return result;
  577. }
  578.  
  579. void CloseAllHandle(void){
  580.  
  581. if(CloseHandle(m_radio) == FALSE){
  582. cout<<"CloseHandle() failed with error code "<< GetLastError()<<endl;
  583. }
  584. BluetoothUnregisterAuthentication(hRegHandle);
  585.  
  586. }
Add Comment
Please, Sign In to add comment