You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派3上BlueZ hci_le_set_scan_parameters()函数调用失败咨询

Troubleshooting hci_le_set_scan_parameters() Returning -1 on Raspberry Pi 3

Hey there, let's break down why your BlueZ HCI LE scan parameter setup is failing. I've tackled similar BLE debugging issues on Raspberry Pi before, so here are the most likely fixes to try:

1. Root Permissions Are Almost Certainly Required

HCI raw socket operations (like configuring scan parameters) demand root privileges—this is the #1 culprit for -1 returns here. You have two easy fixes:

  • Set the setuid bit on your binary: SSH into your Pi, navigate to your compiled app's directory, and run:
    sudo chmod u+s ./your_executable_name
    
    This lets the app run as root automatically whenever it's launched.
  • Run via sudo in VS2017: In your VS Linux project properties, go to Debugging > Command and prepend sudo to your executable path (e.g., sudo ./your_executable_name). The remote debug session will now run with elevated rights.

2. Verify Your Bluetooth Adapter Is Active

If your Pi's Bluetooth adapter isn't powered up, HCI calls will fail every time:

  • SSH into your Pi and run hciconfig hci0—look for "UP RUNNING" in the output.
  • If it's not active, enable it with:
    sudo hciconfig hci0 up
    
  • You can also confirm BLE support with hciconfig hci0 features—check for flags related to LE (Low Energy) support.

3. Double-Check Your Function Parameters

Invalid arguments to hci_le_set_scan_parameters() will trigger -1 returns. Common mistakes include:

  • Scan interval/window values: These must be between 0x0004 (2.5ms) and 0x4000 (10.24s), and the window cannot be larger than the interval.
  • Own BD address type: Use LE_PUBLIC_ADDRESS (0) or LE_RANDOM_ADDRESS (1)—match this to your adapter's configured address type.
  • Filter policy: For basic scanning, use 0 (accept all advertisements) to avoid unnecessary restrictions.

4. Capture Detailed Error Context

Don't stop at knowing it returns -1—get the actual error message to pinpoint the issue. Add this code right after the failed call:

if (hci_le_set_scan_parameters(fd, scan_type, scan_interval, scan_window,
                               own_bdaddr_type, filter_policy, 1000) < 0) {
    perror("Failed to set scan parameters");
    // Or use BlueZ's dedicated error helper:
    fprintf(stderr, "BlueZ Error: %s\n", hci_strerror(errno));
}

Common errors you might see:

  • EPERM: Permission denied (you skipped step 1)
  • EINVAL: Invalid parameters (step 3 needs fixing)
  • ENETDOWN: Bluetooth adapter is not powered up (step 2)

5. Check BlueZ Version Compatibility

Even with the latest Raspbian, older BlueZ versions might have stricter parameter checks or missing features. Run bluetoothd -v to check your version. If it's outdated, update it with:

sudo apt update && sudo apt install bluez

内容的提问来源于stack exchange,提问作者Paul Tomov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:31:28