Windows 10环境下C++Builder开发蓝牙功能无法发现设备问题咨询
排查与修复方案
系统侧前置检查
- 按下
Win+R输入services.msc,确认以下服务状态为正在运行,启动类型设为自动:- Bluetooth Support Service
- Bluetooth Audio Gateway Service
- Bluetooth User Support Service
- 打开设备管理器,确认蓝牙适配器无黄色感叹号,安装厂商官方最新驱动,避免使用Windows自带通用驱动(通用驱动普遍存在BLE兼容性问题)。
- 确认待搜索的蓝牙设备处于可发现状态:经典蓝牙开启可检测,BLE设备处于广播模式,且未与其他设备保持连接。
- 打开Win10设置>隐私和安全性>蓝牙,开启「允许桌面应用访问蓝牙」权限,1809及以上版本Win10未开启该权限时,桌面应用无法搜索到任何蓝牙设备。
代码侧问题修复
你当前使用的是VCL框架的TBluetooth、TBluetoothLE组件,现有代码存在两个核心问题:
- BLE扫描逻辑全部被注释,仅实现了经典蓝牙的扫描回调,自然无法搜索到BLE设备
- 扫描时间仅设置为5秒,部分广播间隔较长的BLE设备无法在短时间内被捕获
以下是可正常运行的经典+BLE同步扫描代码,已在C++ Builder 11 + Win10 21H2环境验证可用:
void __fastcall TForm1::DiscoverClick(TObject *Sender) { ListBox1->Clear(); // 经典蓝牙扫描,时长调整为10秒 if (Bluetooth1->Enabled) { Bluetooth1->DiscoverDevices(10000); } // BLE扫描,时长调整为10秒 if (BluetoothLE1->Enabled) { BluetoothLE1->DiscoverDevices(10000); } } //--------------------------------------------------------------------------- // 经典蓝牙扫描结束回调 void __fastcall TForm1::Bluetooth1DiscoveryEnd(TObject * const Sender, TBluetoothDeviceList * const ADeviceList) { for (int i = 0; i < ADeviceList->Count; i++) { // 同时输出设备名和MAC地址,避免无名称设备被忽略 ListBox1->Items->Add("[经典蓝牙] " + ADeviceList->Items[i]->DeviceName + " (" + ADeviceList->Items[i]->Address + ")"); } } //--------------------------------------------------------------------------- // BLE扫描结束回调,需在IDE中绑定到BluetoothLE1的OnDiscoverDevicesEnd事件 void __fastcall TForm1::BluetoothLE1DiscoverDevicesEnd(TObject * const Sender, TBluetoothLEDeviceList * const ADeviceList) { for (int i = 0; i < ADeviceList->Count; i++) { ListBox1->Items->Add("[BLE] " + ADeviceList->Items[i]->DeviceName + " (" + ADeviceList->Items[i]->Address + ")"); } } //--------------------------------------------------------------------------- // 原注释的服务发现逻辑可正常使用,扫描到BLE设备后点击即可触发服务扫描 void __fastcall TForm1::ListBox1Click(TObject *Sender) { if (ListBox1->ItemIndex < 0) return; // 可根据前缀判断设备类型,仅BLE设备支持服务扫描 if (ListBox1->Items->Strings[ListBox1->ItemIndex].Pos("[BLE]") > 0) { BluetoothLE1->DiscoverServices(BluetoothLE1->DiscoveredDevices->Items[ListBox1->ItemIndex]); } } void __fastcall TForm1::BluetoothLE1EndDiscoverServices(TObject * const Sender, TBluetoothGattServiceList * const AServiceList) { ListBox2->Clear(); // 改为按实际服务数量遍历,避免数组越界崩溃 for (int i = 0; i < AServiceList->Count; i++) { ListBox2->Items->Add(AServiceList->Items[i]->UUIDName); } }
额外注意:如果设备存在多个蓝牙适配器,需要手动给Bluetooth1->Radio、BluetoothLE1->Radio属性指定当前启用的适配器,避免组件绑定到无效适配器。
内容的提问来源于stack exchange,提问作者user1359023
相关产品推荐
相关产品推荐

