树莓派3上BlueZ hci_le_set_scan_parameters()函数调用失败咨询
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:
This lets the app run as root automatically whenever it's launched.sudo chmod u+s ./your_executable_name - Run via sudo in VS2017: In your VS Linux project properties, go to Debugging > Command and prepend
sudoto 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) and0x4000(10.24s), and the window cannot be larger than the interval. - Own BD address type: Use
LE_PUBLIC_ADDRESS(0) orLE_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

