ZBLOG

OCF_LE_SET_ADVERTISING_DATA 广播出来的蓝牙不能配对

如果使用 OCF_LE_SET_ADVERTISING_DATA(HCI 命令)广播蓝牙设备后,发现设备无法被配对,通常是由于以下原因之一导致的:


1. 检查广播数据格式

蓝牙设备的可配对性取决于广播数据(Advertising Data)中的 Flags 字段和广播参数(Advertising Parameters)。

(1) Flags 必须包含 BR/EDR Not Supported + LE General Discoverable

  • 正确的 Flags 值:0x06
    
    0x01 = LE Limited Discoverable Mode  
    0x02 = LE General Discoverable Mode  
    0x04 = BR/EDR Not Supported  
    0x06 = LE General Discoverable + BR/EDR Not Supported (推荐)
    
  • 错误的配置示例:
    • Flags=0x00 → 设备不可被发现,也无法配对。
    • Flags=0x04 → BR/EDR Not Supported,但未设置 Discoverable,导致无法被发现。

(2) C代码示例:设置正确的 Advertising Data

uint8_t adv_data[] = {
    0x02,             // Length of this data segment
    0x01,             // AD Type: Flags
    0x06,             // Flags: LE General Disc. + BR/EDR Not Supported

    // Optional: Add device name or other advertising data
    0x05,             // Length of name segment
    0x09,             // AD Type: Complete Local Name
    'T', 'E', 'S', 'T'
};

2. 确保 Advertising Parameters 允许连接

即使广播数据正确,如果 HCI 参数设置为 Non-Connectable,设备仍然无法配对。

(1) HCI Command: OCF_LE_SET_ADVERTISING_PARAMETERS

必须确保:

  • Advertising_Type = ADV_IND (0)(可连接 & 可扫描)
  • Own_Address_Type = PUBLIC (0) / RANDOM (1)
  • Filter Policy = Allow Any (0)
hci_le_set_advertising_parameters(
    hci_dev,
    100,      // Min Interval (ms)
    100,      // Max Interval (ms)
    4,        // ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED (ADV_IND)
    1,        // Own Address Type: Random (or Public)
    7,        // Direct Address Type: Public 
    "000000000000", // Direct Address (ignored if not used)
    7         // Filter Policy: Allow All Connections & Scans
);

(2) BlueZ D-Bus API(替代方案)

如果不想直接操作 HCI Socket,可以使用 BlueZ D-Bus API:

# Enable connectable advertising via bluetoothctl
bluetoothctl advertise on connectable discoverable

或通过 C D-Bus API:

GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&builder, "{sv}", "Type", g_variant_new_string("peripheral")); 
g_dbus_proxy_call_sync(adv_mgr_proxy,
    "RegisterAdvertisement",
    g_variant_new("(oa{sv})", "/org/bluez/myadv", &builder),
    G_DBUS_CALL_FLAGS_NONE,
    -1,
    NULL,
    &error);

3. Pairing Issues? Check SMP (Security Manager Protocol)

即使广播是可连接的,如果 Security Mode (SMP) 配置错误,仍然可能导致配对失败。

(1) BlueZ Pairing Modes

Mode Description
Just Works No MITM protection
Passkey Entry User enters PIN
Numeric Comparison Secure pairing for BLE >4.2

(2) C代码示例:启用 JustWorks Pairing(最低安全模式)

// Set IO Capability to NoInputNoOutput ("Just Works")
hci_send_cmd(hci_dev,
    OCF_WRITE_SIMPLE_PAIRING_MODE,
    1);       // Enable SSP

hci_send_cmd(hci_dev,
    OCF_WRITE_SECURE_CONNECTIONS_SUPPORT,
    1);       // Enable Secure Connections if supported

// Set MITM Protection OFF for testing:
hci_send_cmd(hci_dev,
    OCF_AUTHENTICATION_REQUESTED,
    conn_handle);

4. Debugging Steps

(1) Use btmon / hcidump to Verify Advertising Data

sudo btmon -w debug.log &
sudo hcitool lescan --duplicates   # Check if device appears as connectable.

观察日志是否包含:

> HCI Event: LE Meta Event (Subevent: Extended Advertising Report)
> Flags: Connectable | Scannable | Legacy PDU 
> Data: Flags=06 ...

(2) Test with nRF Connect / LightBlue App

用手机 App(如 nRF Connect)扫描设备: ✅ 正常情况:

  • Device shows as “Connectable”
  • Clicking it allows pairing

异常情况:

  • If it shows “Non-Connectable Beacon” → Advertising Data or Parameters are wrong.

5. Final Fixes Summary

Issue Solution
🔹 Cannot see device in scan Ensure Flags=0x06 (General Disc. + BR/EDR Not Supported)
🔹 Device seen but cannot pair Check Advertising Type=ADV_IND, and ensure SMP is enabled
🔹 Pairing fails after connection Adjust IO Capability (JustWorks, PasskeyEntry)

如果你能提供具体的代码片段或 HCI Logs (btmon),我可以进一步分析!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=24041

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?