Advertisement
glueeface

Untitled

Nov 30th, 2022 (edited)
2,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <cassert>
  5.  
  6. #define WIN32_LEAN_AND_MEAN
  7.  
  8. #include <Windows.h>
  9. #include <BluetoothAPIs.h>
  10. #include <Winsock2.h>
  11. #include <Ws2bth.h>
  12.  
  13. #pragma comment(lib, "ws2_32.lib")
  14. #pragma comment(lib, "irprops.lib")
  15. #pragma comment(lib, "bluetoothapis.lib")
  16. #pragma warning(disable : 4995)
  17.  
  18. BOOL CALLBACK BTHeadsetAuthCallbackEx(__in_opt LPVOID /*pvParam*/, __in PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
  19. {
  20.   BLUETOOTH_AUTHENTICATE_RESPONSE resp;
  21.   resp.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
  22.   resp.authMethod = pAuthCallbackParams->authenticationMethod;
  23.   resp.negativeResponse = FALSE;
  24.   resp.passkeyInfo.passkey = pAuthCallbackParams->Passkey;
  25.   DWORD ret = BluetoothSendAuthenticationResponseEx(NULL, &resp);
  26.   if (ret != ERROR_SUCCESS)
  27.   {
  28.     std::cout << "BluetoothSendAuthenticationResponseEx failed with %u" << ret << std::endl;
  29.     return FALSE;
  30.   }
  31.  
  32.   return TRUE;
  33. }
  34.  
  35.  
  36. int main()
  37. {
  38.   BLUETOOTH_DEVICE_SEARCH_PARAMS btSearchParams;
  39.  
  40.   btSearchParams.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
  41.   btSearchParams.cTimeoutMultiplier = 5;  //5*1.28s search timeout
  42.   btSearchParams.fIssueInquiry = true;    //new inquiry
  43.  
  44.   //return all known and unknown devices
  45.   btSearchParams.fReturnAuthenticated = true;
  46.   btSearchParams.fReturnConnected = true;
  47.   btSearchParams.fReturnRemembered = true;
  48.   btSearchParams.fReturnUnknown = true;
  49.  
  50.   btSearchParams.hRadio = NULL;   //search on all local radios
  51.  
  52.  
  53.  
  54.   BLUETOOTH_DEVICE_INFO btDeviceInfo;
  55.   ZeroMemory(&btDeviceInfo, sizeof(BLUETOOTH_DEVICE_INFO));   //"initialize"
  56.   btDeviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
  57.  
  58.   HBLUETOOTH_DEVICE_FIND btDeviceFindHandle = NULL;
  59.   GUID state1 = { 0x0000110b, 0x0000, 0x1000, { 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb } };
  60.   GUID state2 = { 0x0000110e, 0x0000, 0x1000, { 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb } };
  61.   char flag;
  62.   btDeviceFindHandle = BluetoothFindFirstDevice(&btSearchParams, &btDeviceInfo);
  63.   do {
  64.     if (btDeviceFindHandle)
  65.     {
  66.       std::cout << "device detected..." << std::endl;
  67.       printf("class of device: %ul\n", btDeviceInfo.ulClassofDevice);
  68.       printf("adress of device: %X\n", btDeviceInfo.Address);
  69.       printf("name of device: %S\n", btDeviceInfo.szName);
  70.       printf("connect/continue? [c/e]");
  71.       scanf_s("%c", &flag);
  72.       if (flag == 'c') {
  73.         HBLUETOOTH_AUTHENTICATION_REGISTRATION authCallbackHandle = NULL;
  74.         DWORD err = BluetoothRegisterForAuthenticationEx(&btDeviceInfo, &authCallbackHandle, (PFN_AUTHENTICATION_CALLBACK_EX)&BTHeadsetAuthCallbackEx, NULL);
  75.         if (err != ERROR_SUCCESS)
  76.         {
  77.           DWORD err = GetLastError();
  78.           std::cout << "BluetoothRegisterForAuthentication Error" << err << std::endl;
  79.         }
  80.         HBLUETOOTH_AUTHENTICATION_REGISTRATION regHandle;
  81.         err = BluetoothSetServiceState(&btDeviceFindHandle, &btDeviceInfo, &state1, BLUETOOTH_SERVICE_ENABLE);
  82.         if (err != ERROR_SUCCESS && err != 87)
  83.         {
  84.           std::cout << "BluetoothSetServiceState1 failed with " << err << std::endl;
  85.           //return false;
  86.         }
  87.         err = BluetoothSetServiceState(&btDeviceFindHandle, &btDeviceInfo, &state2, BLUETOOTH_SERVICE_ENABLE);
  88.         if (err != ERROR_SUCCESS && err != 87)
  89.         {
  90.           std::cout << "BluetoothSetServiceState2 failed with " << err << std::endl;
  91.           //return false;
  92.         }
  93.           std::cout << "BluetoothSetServiceState successfull" << std::endl;
  94.           if (!BluetoothFindNextDevice(btDeviceFindHandle, &btDeviceInfo)) {
  95.             err = GetLastError();
  96.             std::cout << "Find next dev failed with " << err << std::endl;
  97.             break;
  98.           }
  99.  
  100.       }
  101.     }
  102.   } while (flag != 'e');
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement