#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; }
|