Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <cassert>
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include <BluetoothAPIs.h>
- #include <Winsock2.h>
- #include <Ws2bth.h>
- #pragma comment(lib, "ws2_32.lib")
- #pragma comment(lib, "irprops.lib")
- #pragma comment(lib, "bluetoothapis.lib")
- #pragma warning(disable : 4995)
- BOOL CALLBACK BTHeadsetAuthCallbackEx(__in_opt LPVOID /*pvParam*/, __in PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
- {
- BLUETOOTH_AUTHENTICATE_RESPONSE resp;
- resp.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
- resp.authMethod = pAuthCallbackParams->authenticationMethod;
- resp.negativeResponse = FALSE;
- resp.passkeyInfo.passkey = pAuthCallbackParams->Passkey;
- DWORD ret = BluetoothSendAuthenticationResponseEx(NULL, &resp);
- if (ret != ERROR_SUCCESS)
- {
- std::cout << "BluetoothSendAuthenticationResponseEx failed with %u" << ret << std::endl;
- return FALSE;
- }
- return TRUE;
- }
- int main()
- {
- BLUETOOTH_DEVICE_SEARCH_PARAMS btSearchParams;
- btSearchParams.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
- btSearchParams.cTimeoutMultiplier = 5; //5*1.28s search timeout
- btSearchParams.fIssueInquiry = true; //new inquiry
- //return all known and unknown devices
- btSearchParams.fReturnAuthenticated = true;
- btSearchParams.fReturnConnected = true;
- btSearchParams.fReturnRemembered = true;
- btSearchParams.fReturnUnknown = true;
- btSearchParams.hRadio = NULL; //search on all local radios
- BLUETOOTH_DEVICE_INFO btDeviceInfo;
- ZeroMemory(&btDeviceInfo, sizeof(BLUETOOTH_DEVICE_INFO)); //"initialize"
- btDeviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
- HBLUETOOTH_DEVICE_FIND btDeviceFindHandle = NULL;
- GUID state1 = { 0x0000110b, 0x0000, 0x1000, { 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb } };
- GUID state2 = { 0x0000110e, 0x0000, 0x1000, { 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb } };
- char flag;
- btDeviceFindHandle = BluetoothFindFirstDevice(&btSearchParams, &btDeviceInfo);
- do {
- if (btDeviceFindHandle)
- {
- std::cout << "device detected..." << std::endl;
- printf("class of device: %ul\n", btDeviceInfo.ulClassofDevice);
- printf("adress of device: %X\n", btDeviceInfo.Address);
- printf("name of device: %S\n", btDeviceInfo.szName);
- printf("connect/continue? [c/e]");
- scanf_s("%c", &flag);
- if (flag == 'c') {
- HBLUETOOTH_AUTHENTICATION_REGISTRATION authCallbackHandle = NULL;
- DWORD err = BluetoothRegisterForAuthenticationEx(&btDeviceInfo, &authCallbackHandle, (PFN_AUTHENTICATION_CALLBACK_EX)&BTHeadsetAuthCallbackEx, NULL);
- if (err != ERROR_SUCCESS)
- {
- DWORD err = GetLastError();
- std::cout << "BluetoothRegisterForAuthentication Error" << err << std::endl;
- }
- HBLUETOOTH_AUTHENTICATION_REGISTRATION regHandle;
- err = BluetoothSetServiceState(&btDeviceFindHandle, &btDeviceInfo, &state1, BLUETOOTH_SERVICE_ENABLE);
- if (err != ERROR_SUCCESS && err != 87)
- {
- std::cout << "BluetoothSetServiceState1 failed with " << err << std::endl;
- //return false;
- }
- err = BluetoothSetServiceState(&btDeviceFindHandle, &btDeviceInfo, &state2, BLUETOOTH_SERVICE_ENABLE);
- if (err != ERROR_SUCCESS && err != 87)
- {
- std::cout << "BluetoothSetServiceState2 failed with " << err << std::endl;
- //return false;
- }
- std::cout << "BluetoothSetServiceState successfull" << std::endl;
- if (!BluetoothFindNextDevice(btDeviceFindHandle, &btDeviceInfo)) {
- err = GetLastError();
- std::cout << "Find next dev failed with " << err << std::endl;
- break;
- }
- }
- }
- } while (flag != 'e');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement