#include <iomanip> #include <iostream> #include <Windows.h> #include <BluetoothAPIs.h>
#pragma comment(lib, "Bthprops.lib") #pragma warning(disable: 4995)
using namespace std;
ostream &operator << (ostream &os, const wchar_t *wstr) { if (os == cout) WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wstr, wcslen(wstr), NULL, NULL); return os; }
void show_address(BLUETOOTH_ADDRESS *addr) { int i; cout << hex << uppercase << setfill('0'); for (i = 5; i >= 0; i--) { cout << setw(2) << (int)addr->rgBytes[i]; if (i != 0) cout << ':'; } cout << dec; }
void show_time(LPSYSTEMTIME lpTime, bool fConvertToLocal = true) { SYSTEMTIME stLocal; ZeroMemory(&stLocal, sizeof(stLocal)); if (memcmp(lpTime, &stLocal, sizeof(SYSTEMTIME)) == 0) { cout << L"无"; return; } if (fConvertToLocal) { SystemTimeToTzSpecificLocalTime(NULL, lpTime, &stLocal); // 转换为系统时区 lpTime = &stLocal; }
cout << setfill('0'); cout << setw(4) << lpTime->wYear << '-' << lpTime->wMonth << '-' << lpTime->wDay << ' '; cout << setw(2) << lpTime->wHour << ':'; cout << setw(2) << lpTime->wMinute << ':'; cout << setw(2) << lpTime->wSecond; }
int main(void) { /* 查找蓝牙发射器 */ BLUETOOTH_FIND_RADIO_PARAMS btfrp; btfrp.dwSize = sizeof(BLUETOOTH_FIND_RADIO_PARAMS);
HANDLE hRadio; HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&btfrp, &hRadio); if (hFind == NULL) { cout << TEXT("无法找到蓝牙发射器") << endl; return 0; }
BLUETOOTH_RADIO_INFO btri; btri.dwSize = sizeof(BLUETOOTH_RADIO_INFO); int i = 0; do { i++; BluetoothGetRadioInfo(hRadio, &btri); cout << TEXT("[蓝牙发射器") << setw(3) << setfill('0') << i << ']' << endl; cout << TEXT("名称: ") << btri.szName << endl; cout << TEXT("地址: "); show_address(&btri.address); cout << endl << TEXT("制造商编号: ") << btri.manufacturer << endl; CloseHandle(hRadio); } while (BluetoothFindNextRadio(hFind, &hRadio)); BluetoothFindRadioClose(hFind); cout << endl;
/* 查找蓝牙设备 */ BLUETOOTH_DEVICE_SEARCH_PARAMS btsp; btsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS); btsp.cTimeoutMultiplier = 10; btsp.fReturnAuthenticated = TRUE; btsp.fReturnConnected = TRUE; btsp.fReturnRemembered = TRUE; btsp.fReturnUnknown = TRUE; btsp.fIssueInquiry = FALSE; // 是否启动新的查询 btsp.hRadio = NULL; // 使用所有收发器
bool found = false; BLUETOOTH_DEVICE_INFO btdi, found_info; btdi.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); DWORD dwErr; HBLUETOOTH_DEVICE_FIND hFindDevice = BluetoothFindFirstDevice(&btsp, &btdi); if (hFindDevice == NULL) { dwErr = GetLastError(); if (dwErr == ERROR_NO_MORE_ITEMS) cout << TEXT("未找到任何设备") << endl; else cout << TEXT("查找设备时出错") << endl; return 0; } i = 0; do { i++; cout << TEXT("[设备") << setw(3) << setfill('0') << i << ']' << endl; cout << TEXT("名称: ") << btdi.szName << endl; if (wcscmp(btdi.szName, L"octMCU2") == 0) { found = true; found_info = btdi; } cout << TEXT("地址: "); show_address(&btdi.Address); cout << endl << TEXT("连接状态: ") << ((btdi.fConnected) ? TEXT("已连接") : TEXT("未连接")) << endl; cout << TEXT("配对状态: ") << ((btdi.fAuthenticated) ? TEXT("已配对") : TEXT("未配对")) << endl; cout << TEXT("发现时间: "); show_time(&btdi.stLastSeen); cout << endl << TEXT("最后使用时间: "); show_time(&btdi.stLastUsed); cout << endl << endl; } while (BluetoothFindNextDevice(hFindDevice, &btdi)); BluetoothFindDeviceClose(hFindDevice);
if (found) { cout << L"已找到所需设备"; if (!found_info.fAuthenticated) cout << L", 开始配对..."; cout << endl; if (!found_info.fAuthenticated) { WCHAR szKey[] = L"1805"; dwErr = BluetoothAuthenticateDevice(NULL, NULL, &found_info, szKey, wcslen(szKey)); switch (dwErr) { case ERROR_SUCCESS: cout << L"配对成功" << endl; break; default: cout << L"配对失败, 错误码为: " << dwErr << endl; } } } else cout << L"未找到所需设备" << endl; return 0; }
|