-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetoothMonitorGUI.cpp
More file actions
1121 lines (942 loc) · 39.5 KB
/
BluetoothMonitorGUI.cpp
File metadata and controls
1121 lines (942 loc) · 39.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <windows.h>
#include <shellapi.h>
#include <bluetoothapis.h>
#include <commctrl.h>
#include <string>
#include <vector>
#include <set>
#include <thread>
#include <mutex>
#include <fstream>
#include <codecvt>
#include <locale>
#include <unordered_map>
#pragma comment(lib, "Bthprops.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
using namespace std;
// 窗口类名和标题
const wchar_t CLASS_NAME[] = L"BluetoothMonitorClass";
const wchar_t WINDOW_TITLE[] = L"蓝牙设备自动连接";
// 消息和控件ID
#define WM_TRAYICON (WM_USER + 1)
#define ID_TRAY_EXIT 1001
#define ID_TRAY_SHOW 1002
#define ID_TRAY_CONFIG 1003
#define ID_LOG_EDIT 2001
#define ID_DEVICE_LIST 2002
#define ID_BTN_START 2003
#define ID_BTN_STOP 2004
#define ID_BTN_CLEAR 2005
#define ID_DEVICE_CONNECT 3001
#define ID_DEVICE_DISCONNECT 3002
#define ID_DEVICE_COPY_MAC 3003
#define ID_DEVICE_REFRESH 3004
#define ID_DEVICE_COPY_NAME 3005
#define ID_DEVICE_ADD_MONITOR 3006
#define ID_DEVICE_REMOVE_MONITOR 3007
// 蓝牙设备信息结构
struct BluetoothDeviceInfo {
BLUETOOTH_ADDRESS address;
wstring name;
bool connected;
};
// 全局变量
HINSTANCE g_hInst = nullptr;
HWND g_hwndMain = nullptr;
HWND g_hwndLog = nullptr;
HWND g_hwndDeviceList = nullptr;
NOTIFYICONDATAW g_nid = {};
bool g_bRunning = true;
mutex g_logMutex;
thread* g_pMonitorThread = nullptr;
vector<BluetoothDeviceInfo> g_currentDevices;
set<wstring> g_monitorDevices;
// 手动断开后,阻止自动重连的设备(按MAC字符串标识)
set<wstring> g_blockAutoReconnect;
// 每台设备的重连冷却时间戳
unordered_map<wstring, chrono::steady_clock::time_point> g_lastConnectAttempt;
static const int CONNECT_COOLDOWN_MS = 8000; // 8秒冷却
// 将 BLUETOOTH_ADDRESS 转换为字符串
wstring BluetoothAddressToString(const BLUETOOTH_ADDRESS& addr) {
wchar_t buffer[18];
swprintf_s(buffer, L"%02X:%02X:%02X:%02X:%02X:%02X",
addr.rgBytes[5], addr.rgBytes[4], addr.rgBytes[3],
addr.rgBytes[2], addr.rgBytes[1], addr.rgBytes[0]);
return wstring(buffer);
}
// 添加日志
void AddLog(const wstring& message) {
lock_guard<mutex> lock(g_logMutex);
if (g_hwndLog) {
int len = GetWindowTextLength(g_hwndLog);
SendMessage(g_hwndLog, EM_SETSEL, len, len);
SendMessage(g_hwndLog, EM_REPLACESEL, FALSE, (LPARAM)message.c_str());
SendMessage(g_hwndLog, EM_REPLACESEL, FALSE, (LPARAM)L"\r\n");
SendMessage(g_hwndLog, EM_SCROLLCARET, 0, 0);
}
}
// 读取配置文件
set<wstring> LoadConfig(const wstring& configFile) {
set<wstring> monitorDevices;
wifstream file(configFile);
if (!file.is_open()) {
return monitorDevices;
}
wstring line;
while (getline(file, line)) {
size_t commentPos = line.find(L'#');
if (commentPos != wstring::npos) {
line = line.substr(0, commentPos);
}
size_t start = line.find_first_not_of(L" \t\r\n");
size_t end = line.find_last_not_of(L" \t\r\n");
if (start != wstring::npos && end != wstring::npos) {
line = line.substr(start, end - start + 1);
if (!line.empty()) {
monitorDevices.insert(line);
}
}
}
file.close();
return monitorDevices;
}
// 保存配置文件
bool SaveConfig(const wstring& configFile, const set<wstring>& monitorDevices) {
// 使用 UTF-8 编码
wofstream file(configFile, ios::out | ios::trunc);
if (!file.is_open()) {
return false;
}
// 设置 UTF-8 locale
file.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
file << L"# 蓝牙设备自动连接配置文件\n";
file << L"# 每行填写一个要监控的设备名称\n";
file << L"# 使用 # 开头的行为注释\n";
file << L"# 如果此文件为空或不存在,请右键添加设备到监控列表\n";
file << L"\n";
for (const auto& deviceName : monitorDevices) {
file << deviceName << L"\n";
}
file.flush();
file.close();
return file.good() || !file.bad();
}
// 名称匹配:patterns 中任意一项作为子串出现在 text 中即认为匹配
bool MatchAnySubstring(const wstring& text, const set<wstring>& patterns) {
if (patterns.empty()) return true;
for (const auto& p : patterns) {
if (!p.empty() && text.find(p) != wstring::npos) return true;
}
return false;
}
// 获取所有已配对的蓝牙设备
vector<BluetoothDeviceInfo> GetPairedDevices() {
vector<BluetoothDeviceInfo> devices;
BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams = { 0 };
searchParams.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
searchParams.fReturnAuthenticated = TRUE;
searchParams.fReturnRemembered = TRUE;
searchParams.fReturnConnected = TRUE;
searchParams.fReturnUnknown = FALSE;
searchParams.fIssueInquiry = FALSE;
searchParams.cTimeoutMultiplier = 1;
BLUETOOTH_DEVICE_INFO deviceInfo = { 0 };
deviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(&searchParams, &deviceInfo);
if (hFind != NULL) {
do {
BluetoothDeviceInfo info;
info.address = deviceInfo.Address;
info.name = deviceInfo.szName;
info.connected = deviceInfo.fConnected;
devices.push_back(info);
} while (BluetoothFindNextDevice(hFind, &deviceInfo));
BluetoothFindDeviceClose(hFind);
}
return devices;
}
// 带可选主动查询的设备获取(用于提高“在线/可连接”检测的及时性)
vector<BluetoothDeviceInfo> GetPairedDevicesWithInquiry(bool doInquiry) {
vector<BluetoothDeviceInfo> devices;
BLUETOOTH_DEVICE_SEARCH_PARAMS searchParams = { 0 };
searchParams.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
searchParams.fReturnAuthenticated = TRUE;
searchParams.fReturnRemembered = TRUE;
searchParams.fReturnConnected = TRUE;
searchParams.fReturnUnknown = FALSE;
searchParams.fIssueInquiry = doInquiry ? TRUE : FALSE;
searchParams.cTimeoutMultiplier = doInquiry ? 2 : 1;
BLUETOOTH_DEVICE_INFO deviceInfo = { 0 };
deviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(&searchParams, &deviceInfo);
if (hFind != NULL) {
do {
BluetoothDeviceInfo info;
info.address = deviceInfo.Address;
info.name = deviceInfo.szName;
info.connected = deviceInfo.fConnected;
devices.push_back(info);
} while (BluetoothFindNextDevice(hFind, &deviceInfo));
BluetoothFindDeviceClose(hFind);
}
return devices;
}
// 辅助:错误码转字符串
wstring Win32ErrorToString(DWORD code) {
LPWSTR buf = nullptr;
DWORD len = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, code, 0, (LPWSTR)&buf, 0, NULL);
wstring msg = len ? wstring(buf) : L"";
if (buf) LocalFree(buf);
return msg;
}
// 辅助:GUID 转字符串
wstring GuidToString(const GUID& g) {
wchar_t buf[64];
swprintf_s(buf, 64, L"{%08lX-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}",
g.Data1, g.Data2, g.Data3,
g.Data4[0], g.Data4[1], g.Data4[2], g.Data4[3], g.Data4[4], g.Data4[5], g.Data4[6], g.Data4[7]);
return wstring(buf);
}
// 辅助:打开第一个蓝牙无线电句柄
HANDLE OpenFirstRadio() {
BLUETOOTH_FIND_RADIO_PARAMS params = { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
HBLUETOOTH_RADIO_FIND hFind = nullptr;
HANDLE hRadio = nullptr;
hFind = BluetoothFindFirstRadio(¶ms, &hRadio);
if (hFind != NULL) {
BluetoothFindRadioClose(hFind);
return hRadio;
}
return nullptr;
}
// 辅助:判断服务是否为音频相关
bool IsAudioService(const GUID& guid) {
return (guid == AudioSinkServiceClass_UUID ||
guid == AudioSourceServiceClass_UUID ||
guid == AVRemoteControlTargetServiceClass_UUID ||
guid == AVRemoteControlServiceClass_UUID ||
guid == HandsfreeServiceClass_UUID ||
guid == HeadsetServiceClass_UUID);
}
// 辅助:判断服务是否为 GATT 服务
bool IsGATTService(const GUID& guid) {
return (guid.Data1 == 0x1800 || guid.Data1 == 0x1801) &&
guid.Data2 == 0x0000 && guid.Data3 == 0x1000;
}
// 连接蓝牙设备(参考提供的代码:通过禁用/启用音频相关服务触发连接)
bool ConnectDevice(const BLUETOOTH_ADDRESS& address, const wstring& deviceName) {
AddLog(L"尝试连接设备: " + deviceName + L" [" + BluetoothAddressToString(address) + L"]");
BLUETOOTH_DEVICE_INFO deviceInfo = { 0 };
deviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
deviceInfo.Address = address;
DWORD result = BluetoothGetDeviceInfo(NULL, &deviceInfo);
if (result != ERROR_SUCCESS) {
AddLog(L" 获取设备信息失败: " + to_wstring(result) + L" " + Win32ErrorToString(result));
return false;
}
if (deviceInfo.fConnected) {
AddLog(L" 设备已连接");
return true;
}
HANDLE hRadio = OpenFirstRadio();
if (!hRadio) {
AddLog(L" 未找到蓝牙适配器");
return false;
}
// 优先从“已安装服务”中过滤目标服务,减少 1060/87 错误
vector<GUID> installed;
{
const DWORD CAP = 32;
GUID guids[CAP] = {};
DWORD returned = CAP;
DWORD er = BluetoothEnumerateInstalledServices(hRadio, &deviceInfo, &returned, guids);
if (er == ERROR_SUCCESS && returned > 0) {
for (DWORD i = 0; i < returned; ++i) installed.push_back(guids[i]);
}
}
auto contains = [](const vector<GUID>& vec, const GUID& g) {
for (const auto& x : vec) if (x == g) return true; return false;
};
// 参考实现:先禁用再启用常见音频相关服务(按优先级)
vector<GUID> wanted = {
AudioSinkServiceClass_UUID, // A2DP 音频接收器
AudioSourceServiceClass_UUID, // A2DP 音频源
HandsfreeServiceClass_UUID, // 免提
HeadsetServiceClass_UUID, // 头戴式/耳机
AVRemoteControlTargetServiceClass_UUID, // AVRCP 目标
AVRemoteControlServiceClass_UUID // AVRCP 控制器
};
vector<GUID> services;
if (!installed.empty()) {
for (const auto& g : wanted) if (contains(installed, g)) services.push_back(g);
}
if (services.empty()) services = wanted; // 无法枚举时按全量尝试
bool anySuccess = false;
for (const auto& svc : services) {
// 先禁用
BluetoothSetServiceState(hRadio, &deviceInfo, &svc, BLUETOOTH_SERVICE_DISABLE);
Sleep(150);
// 再启用(先用 hRadio,87 时回退到 NULL)
DWORD r = BluetoothSetServiceState(hRadio, &deviceInfo, &svc, BLUETOOTH_SERVICE_ENABLE);
if (r == ERROR_INVALID_PARAMETER) {
r = BluetoothSetServiceState(NULL, &deviceInfo, &svc, BLUETOOTH_SERVICE_ENABLE);
}
if (r == ERROR_SUCCESS) {
anySuccess = true;
AddLog(L" 成功启用服务: " + GuidToString(svc));
Sleep(1200);
// 检查是否已连接
DWORD r2 = BluetoothGetDeviceInfo(NULL, &deviceInfo);
if (r2 == ERROR_SUCCESS && deviceInfo.fConnected) {
AddLog(L" 连接成功");
CloseHandle(hRadio);
return true;
}
} else if (r == ERROR_SERVICE_DOES_NOT_EXIST) {
// 跳过未安装的服务
} else {
AddLog(L" 启用服务失败: " + to_wstring(r) + L" " + Win32ErrorToString(r));
}
}
// 最终再检查一次连接状态
DWORD r3 = BluetoothGetDeviceInfo(NULL, &deviceInfo);
if (r3 == ERROR_SUCCESS && deviceInfo.fConnected) {
AddLog(L" 连接成功");
CloseHandle(hRadio);
return true;
}
CloseHandle(hRadio);
AddLog(L" 连接失败");
return false;
}
// 断开蓝牙设备(遍历已安装服务逐一禁用)
bool DisconnectDevice(const BLUETOOTH_ADDRESS& address, const wstring& deviceName) {
wstring msg = L"尝试断开设备: " + deviceName + L" [" + BluetoothAddressToString(address) + L"]";
AddLog(msg);
BLUETOOTH_DEVICE_INFO deviceInfo = { 0 };
deviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
deviceInfo.Address = address;
DWORD result = BluetoothGetDeviceInfo(NULL, &deviceInfo);
if (result != ERROR_SUCCESS) {
AddLog(L" 获取设备信息失败: " + to_wstring(result) + L" " + Win32ErrorToString(result));
return false;
}
if (!deviceInfo.fConnected) {
AddLog(L" 设备未连接");
return true;
}
HANDLE hRadio = OpenFirstRadio();
if (!hRadio) {
AddLog(L" 无法打开本地蓝牙适配器");
return false;
}
vector<GUID> serviceGuids;
const DWORD CAP = 32;
GUID guids[CAP] = {};
DWORD returned = CAP;
result = BluetoothEnumerateInstalledServices(hRadio, &deviceInfo, &returned, guids);
if (result == ERROR_SUCCESS && returned > 0) {
for (DWORD i = 0; i < returned; ++i) serviceGuids.push_back(guids[i]);
}
if (serviceGuids.empty()) {
serviceGuids.push_back(HumanInterfaceDeviceServiceClass_UUID); // HID
serviceGuids.push_back(HandsfreeServiceClass_UUID); // 免提
serviceGuids.push_back(AudioSinkServiceClass_UUID); // 音频接收器
serviceGuids.push_back(AudioSourceServiceClass_UUID); // 音频源
serviceGuids.push_back(HeadsetServiceClass_UUID); // 头戴式/耳机
serviceGuids.push_back(AVRemoteControlTargetServiceClass_UUID); // AVRCP 目标
serviceGuids.push_back(AVRemoteControlServiceClass_UUID); // AVRCP 控制器
serviceGuids.push_back(SerialPortServiceClass_UUID); // 串口
}
bool ok = false;
for (const auto& svc : serviceGuids) {
result = BluetoothSetServiceState(hRadio, &deviceInfo, &svc, BLUETOOTH_SERVICE_DISABLE);
if (result == ERROR_SUCCESS) ok = true;
}
if (hRadio) CloseHandle(hRadio);
this_thread::sleep_for(chrono::milliseconds(500));
AddLog(ok ? L" 断开成功" : L" 断开失败");
// 若断开成功,标记此设备禁止自动重连,直到用户手动连接为止
if (ok) {
wstring key = BluetoothAddressToString(address);
g_blockAutoReconnect.insert(key);
AddLog(L" 已设置为手动断开:自动重连已禁用(直到手动连接)");
}
return ok;
}
// 更新设备列表显示
void UpdateDeviceList(const vector<BluetoothDeviceInfo>& devices, const set<wstring>& monitorDevices) {
if (!g_hwndDeviceList) return;
g_currentDevices = devices;
g_monitorDevices = monitorDevices;
// 保存当前选中的设备名称
wstring selectedDeviceName;
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex != -1 && selectedIndex < (int)g_currentDevices.size()) {
selectedDeviceName = g_currentDevices[selectedIndex].name;
}
ListView_DeleteAllItems(g_hwndDeviceList);
int newSelectedIndex = -1;
for (size_t i = 0; i < devices.size(); i++) {
const auto& device = devices[i];
bool shouldMonitor = !monitorDevices.empty() && MatchAnySubstring(device.name, monitorDevices);
LVITEM lvi = {};
lvi.mask = LVIF_TEXT;
lvi.iItem = (int)i;
lvi.iSubItem = 0;
lvi.pszText = (LPWSTR)device.name.c_str();
ListView_InsertItem(g_hwndDeviceList, &lvi);
wstring addr = BluetoothAddressToString(device.address);
ListView_SetItemText(g_hwndDeviceList, (int)i, 1, (LPWSTR)addr.c_str());
const wchar_t* status = device.connected ? L"已连接" : L"未连接";
ListView_SetItemText(g_hwndDeviceList, (int)i, 2, (LPWSTR)status);
const wchar_t* monitor = shouldMonitor ? L"是" : L"否";
ListView_SetItemText(g_hwndDeviceList, (int)i, 3, (LPWSTR)monitor);
// 记录之前选中的设备的新位置
if (!selectedDeviceName.empty() && device.name == selectedDeviceName) {
newSelectedIndex = (int)i;
}
}
// 恢复选中状态
if (newSelectedIndex != -1) {
ListView_SetItemState(g_hwndDeviceList, newSelectedIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
ListView_EnsureVisible(g_hwndDeviceList, newSelectedIndex, FALSE);
}
}
// 显示设备右键菜单
void ShowDeviceContextMenu(HWND hwnd) {
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex == -1) return;
if (selectedIndex >= (int)g_currentDevices.size()) return;
const auto& device = g_currentDevices[selectedIndex];
bool isMonitored = !g_monitorDevices.empty() && g_monitorDevices.count(device.name) > 0;
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
if (device.connected) {
AppendMenu(hMenu, MF_STRING, ID_DEVICE_DISCONNECT, L"断开连接");
} else {
AppendMenu(hMenu, MF_STRING, ID_DEVICE_CONNECT, L"手动连接");
}
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
if (isMonitored && !g_monitorDevices.empty()) {
AppendMenu(hMenu, MF_STRING, ID_DEVICE_REMOVE_MONITOR, L"从监控列表移除");
} else {
AppendMenu(hMenu, MF_STRING, ID_DEVICE_ADD_MONITOR, L"添加到监控列表");
}
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, ID_DEVICE_COPY_NAME, L"复制设备名称");
AppendMenu(hMenu, MF_STRING, ID_DEVICE_COPY_MAC, L"复制MAC地址");
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, ID_DEVICE_REFRESH, L"刷新设备列表");
SetForegroundWindow(hwnd);
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(hMenu);
}
// 监控线程
void MonitorThread() {
AddLog(L"========================================");
AddLog(L"蓝牙设备自动连接程序已启动");
AddLog(L"========================================");
// 使用全局配置,如果为空则从文件加载
if (g_monitorDevices.empty()) {
g_monitorDevices = LoadConfig(L"config.txt");
}
set<wstring> monitorDevices = g_monitorDevices;
vector<BluetoothDeviceInfo> pairedDevices = GetPairedDevicesWithInquiry(true);
if (pairedDevices.empty()) {
AddLog(L"未找到已配对的蓝牙设备");
AddLog(L"请先在系统设置中配对蓝牙设备");
return;
}
AddLog(L"找到 " + to_wstring(pairedDevices.size()) + L" 个已配对的设备");
vector<BluetoothDeviceInfo> devicesToMonitor;
for (const auto& device : pairedDevices) {
// 只监控配置文件中明确指定的设备(子串匹配)
bool shouldMonitor = !monitorDevices.empty() && MatchAnySubstring(device.name, monitorDevices);
wstring msg = L" - " + device.name + L" [" + BluetoothAddressToString(device.address) + L"]";
msg += device.connected ? L" (已连接)" : L" (未连接)";
if (shouldMonitor) {
msg += L" [监控中]";
devicesToMonitor.push_back(device);
}
AddLog(msg);
}
if (devicesToMonitor.empty()) {
AddLog(L"没有需要监控的设备");
AddLog(L"请右键点击设备列表中的设备,选择\"添加到监控列表\"");
UpdateDeviceList(pairedDevices, monitorDevices);
return;
}
UpdateDeviceList(pairedDevices, monitorDevices);
AddLog(L"开始监听设备状态...");
vector<bool> lastConnectedState(devicesToMonitor.size(), false);
for (size_t i = 0; i < devicesToMonitor.size(); i++) {
lastConnectedState[i] = devicesToMonitor[i].connected;
}
int checkCount = 0;
int scanCount = 0;
while (g_bRunning) {
checkCount++;
// 每 3 次检查做一次主动扫描
bool doInquiry = (checkCount % 3) == 0;
if (doInquiry) {
scanCount++;
AddLog(L"[" + to_wstring(checkCount) + L"] 执行主动扫描 #" + to_wstring(scanCount) + L"...");
}
vector<BluetoothDeviceInfo> currentDevices = GetPairedDevicesWithInquiry(doInquiry);
UpdateDeviceList(currentDevices, monitorDevices);
for (size_t i = 0; i < devicesToMonitor.size(); i++) {
if (!g_bRunning) break;
const auto& pairedDevice = devicesToMonitor[i];
bool currentlyConnected = false;
bool deviceFound = false;
for (const auto& current : currentDevices) {
if (memcmp(¤t.address, &pairedDevice.address, sizeof(BLUETOOTH_ADDRESS)) == 0) {
currentlyConnected = current.connected;
deviceFound = true;
break;
}
}
if (!deviceFound) {
continue;
}
if (currentlyConnected && !lastConnectedState[i]) {
AddLog(L"[" + to_wstring(checkCount) + L"] ✅ 设备已连接: " + pairedDevice.name);
lastConnectedState[i] = true;
}
else if (!currentlyConnected && lastConnectedState[i]) {
// 二次确认,避免误判
BLUETOOTH_DEVICE_INFO di = {0};
di.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
di.Address = pairedDevice.address;
DWORD rchk = BluetoothGetDeviceInfo(NULL, &di);
if (rchk == ERROR_SUCCESS && di.fConnected) {
// 仍然连接,跳过
} else {
AddLog(L"[" + to_wstring(checkCount) + L"] ❌ 设备已断开: " + pairedDevice.name);
lastConnectedState[i] = false;
}
}
else if (!currentlyConnected && doInquiry) {
AddLog(L"[" + to_wstring(checkCount) + L"] 🔍 发现设备未连接,尝试连接: " + pairedDevice.name);
// 自动重连前检查:是否被手动断开阻止,以及是否处于冷却期
wstring mac = BluetoothAddressToString(pairedDevice.address);
if (g_blockAutoReconnect.count(mac) > 0) {
AddLog(L" ⏸ 用户手动断开,跳过自动重连: " + pairedDevice.name);
continue;
}
auto now = chrono::steady_clock::now();
auto it = g_lastConnectAttempt.find(mac);
if (it != g_lastConnectAttempt.end()) {
auto elapsed = chrono::duration_cast<chrono::milliseconds>(now - it->second).count();
if (elapsed < CONNECT_COOLDOWN_MS) {
AddLog(L" ⏱ 冷却中,跳过本次重连: " + pairedDevice.name);
continue;
}
}
g_lastConnectAttempt[mac] = now;
if (ConnectDevice(pairedDevice.address, pairedDevice.name)) {
lastConnectedState[i] = true;
}
}
}
for (int i = 0; i < 10 && g_bRunning; i++) {
this_thread::sleep_for(chrono::milliseconds(500));
}
}
AddLog(L"监控已停止");
}
// 创建托盘图标
void CreateTrayIcon(HWND hwnd) {
g_nid.cbSize = sizeof(NOTIFYICONDATAW);
g_nid.hWnd = hwnd;
g_nid.uID = 1;
g_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
g_nid.uCallbackMessage = WM_TRAYICON;
g_nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcscpy_s(g_nid.szTip, WINDOW_TITLE);
Shell_NotifyIcon(NIM_ADD, &g_nid);
}
// 显示托盘菜单
void ShowTrayMenu(HWND hwnd) {
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, ID_TRAY_SHOW, L"显示窗口");
AppendMenu(hMenu, MF_STRING, ID_TRAY_CONFIG, L"打开配置文件");
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, ID_TRAY_EXIT, L"退出");
SetForegroundWindow(hwnd);
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(hMenu);
}
// 窗口过程
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
{
// 创建设备列表
g_hwndDeviceList = CreateWindowEx(
0, WC_LISTVIEW, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_SINGLESEL,
10, 10, 760, 200,
hwnd, (HMENU)ID_DEVICE_LIST, g_hInst, NULL
);
// 设置列表视图列
LVCOLUMN lvc = {};
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = (LPWSTR)L"设备名称";
lvc.cx = 200;
ListView_InsertColumn(g_hwndDeviceList, 0, &lvc);
lvc.pszText = (LPWSTR)L"MAC地址";
lvc.cx = 180;
ListView_InsertColumn(g_hwndDeviceList, 1, &lvc);
lvc.pszText = (LPWSTR)L"连接状态";
lvc.cx = 100;
ListView_InsertColumn(g_hwndDeviceList, 2, &lvc);
lvc.pszText = (LPWSTR)L"监控";
lvc.cx = 80;
ListView_InsertColumn(g_hwndDeviceList, 3, &lvc);
ListView_SetExtendedListViewStyle(g_hwndDeviceList, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
// 创建日志编辑框
g_hwndLog = CreateWindowEx(
WS_EX_CLIENTEDGE, L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY,
10, 250, 760, 280,
hwnd, (HMENU)ID_LOG_EDIT, g_hInst, NULL
);
// 创建按钮
CreateWindow(
L"BUTTON", L"开始监控",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, 540, 100, 30,
hwnd, (HMENU)ID_BTN_START, g_hInst, NULL
);
CreateWindow(
L"BUTTON", L"停止监控",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
120, 540, 100, 30,
hwnd, (HMENU)ID_BTN_STOP, g_hInst, NULL
);
CreateWindow(
L"BUTTON", L"清空日志",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
230, 540, 100, 30,
hwnd, (HMENU)ID_BTN_CLEAR, g_hInst, NULL
);
// 创建托盘图标
CreateTrayIcon(hwnd);
// 初始化配置文件(首次运行创建空配置)
wifstream testFile(L"config.txt");
if (!testFile.good()) {
// 文件不存在,创建空配置
set<wstring> emptyConfig;
SaveConfig(L"config.txt", emptyConfig);
}
testFile.close();
// 自动开始监控
g_bRunning = true;
g_pMonitorThread = new thread(MonitorThread);
break;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_BTN_START:
if (!g_bRunning) {
g_bRunning = true;
g_pMonitorThread = new thread(MonitorThread);
AddLog(L"监控已启动");
}
break;
case ID_BTN_STOP:
if (g_bRunning) {
g_bRunning = false;
AddLog(L"正在停止监控...");
// 异步等待线程结束
if (g_pMonitorThread) {
thread cleanup_thread([](thread* pThread) {
if (pThread && pThread->joinable()) {
pThread->join();
delete pThread;
}
}, g_pMonitorThread);
cleanup_thread.detach();
g_pMonitorThread = nullptr;
}
AddLog(L"监控已停止");
}
break;
case ID_BTN_CLEAR:
SetWindowText(g_hwndLog, L"");
break;
case ID_TRAY_SHOW:
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
break;
case ID_TRAY_CONFIG:
ShellExecute(NULL, L"open", L"notepad.exe", L"config.txt", NULL, SW_SHOW);
break;
case ID_TRAY_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_DEVICE_CONNECT:
{
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex != -1 && selectedIndex < (int)g_currentDevices.size()) {
const auto& device = g_currentDevices[selectedIndex];
thread([device]() {
// 手动连接前,取消自动重连阻止
wstring mac = BluetoothAddressToString(device.address);
g_blockAutoReconnect.erase(mac);
ConnectDevice(device.address, device.name);
Sleep(1000);
vector<BluetoothDeviceInfo> devices = GetPairedDevicesWithInquiry(true);
UpdateDeviceList(devices, g_monitorDevices);
}).detach();
}
break;
}
case ID_DEVICE_DISCONNECT:
{
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex != -1 && selectedIndex < (int)g_currentDevices.size()) {
const auto& device = g_currentDevices[selectedIndex];
thread([device]() {
DisconnectDevice(device.address, device.name);
Sleep(1000);
vector<BluetoothDeviceInfo> devices = GetPairedDevicesWithInquiry(true);
UpdateDeviceList(devices, g_monitorDevices);
}).detach();
}
break;
}
case ID_DEVICE_COPY_NAME:
{
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex != -1 && selectedIndex < (int)g_currentDevices.size()) {
const auto& device = g_currentDevices[selectedIndex];
if (OpenClipboard(hwnd)) {
EmptyClipboard();
size_t size = (device.name.length() + 1) * sizeof(wchar_t);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, size);
if (hMem) {
memcpy(GlobalLock(hMem), device.name.c_str(), size);
GlobalUnlock(hMem);
SetClipboardData(CF_UNICODETEXT, hMem);
}
CloseClipboard();
AddLog(L"已复制设备名称: " + device.name);
}
}
break;
}
case ID_DEVICE_COPY_MAC:
{
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex != -1 && selectedIndex < (int)g_currentDevices.size()) {
const auto& device = g_currentDevices[selectedIndex];
wstring macAddr = BluetoothAddressToString(device.address);
if (OpenClipboard(hwnd)) {
EmptyClipboard();
size_t size = (macAddr.length() + 1) * sizeof(wchar_t);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, size);
if (hMem) {
memcpy(GlobalLock(hMem), macAddr.c_str(), size);
GlobalUnlock(hMem);
SetClipboardData(CF_UNICODETEXT, hMem);
}
CloseClipboard();
AddLog(L"已复制MAC地址: " + macAddr);
}
}
break;
}
case ID_DEVICE_REFRESH:
{
AddLog(L"正在刷新设备列表...");
vector<BluetoothDeviceInfo> devices = GetPairedDevicesWithInquiry(true);
UpdateDeviceList(devices, g_monitorDevices);
AddLog(L"设备列表已刷新");
break;
}
case ID_DEVICE_ADD_MONITOR:
{
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex != -1 && selectedIndex < (int)g_currentDevices.size()) {
const auto& device = g_currentDevices[selectedIndex];
// 重新加载配置
g_monitorDevices = LoadConfig(L"config.txt");
g_monitorDevices.insert(device.name);
if (SaveConfig(L"config.txt", g_monitorDevices)) {
AddLog(L"已添加到监控列表: " + device.name);
// 重启监控线程以应用更改
if (g_bRunning && g_pMonitorThread) {
g_bRunning = false;
AddLog(L"正在重启监控...");
thread* oldThread = g_pMonitorThread;
g_pMonitorThread = nullptr;
// 异步清理旧线程
thread([oldThread, hwnd]() {
if (oldThread && oldThread->joinable()) {
oldThread->join();
}
delete oldThread;
// 启动新线程
Sleep(500);
g_bRunning = true;
g_pMonitorThread = new thread(MonitorThread);
}).detach();
} else if (!g_bRunning) {
// 如果监控未运行,启动它
g_bRunning = true;
g_pMonitorThread = new thread(MonitorThread);
}
// 更新显示
vector<BluetoothDeviceInfo> devices = GetPairedDevicesWithInquiry(true);
UpdateDeviceList(devices, g_monitorDevices);
} else {
AddLog(L"添加失败: 无法保存配置文件");
}
}
break;
}
case ID_DEVICE_REMOVE_MONITOR:
{
int selectedIndex = ListView_GetNextItem(g_hwndDeviceList, -1, LVNI_SELECTED);
if (selectedIndex != -1 && selectedIndex < (int)g_currentDevices.size()) {
const auto& device = g_currentDevices[selectedIndex];
// 重新加载配置
g_monitorDevices = LoadConfig(L"config.txt");
g_monitorDevices.erase(device.name);
if (SaveConfig(L"config.txt", g_monitorDevices)) {
AddLog(L"已从监控列表移除: " + device.name);
// 重启监控线程以应用更改
if (g_bRunning && g_pMonitorThread) {
g_bRunning = false;
AddLog(L"正在重启监控...");
thread* oldThread = g_pMonitorThread;
g_pMonitorThread = nullptr;
// 异步清理旧线程
thread([oldThread, hwnd]() {
if (oldThread && oldThread->joinable()) {
oldThread->join();
}
delete oldThread;
// 启动新线程(如果还有设备需要监控)
Sleep(500);
if (!g_monitorDevices.empty()) {
g_bRunning = true;
g_pMonitorThread = new thread(MonitorThread);
}
}).detach();
}
// 更新显示
vector<BluetoothDeviceInfo> devices = GetPairedDevicesWithInquiry(true);
UpdateDeviceList(devices, g_monitorDevices);