树莓派BLE GATT客户端使用bluetoothctl连接后自动断开的原因及配置疑问
Hey there, I’ve run into this exact issue before with Raspberry Pi BLE setups—let’s break down why this happens and how to fix it.
Why Does bluetoothctl Disconnect Immediately?
The core difference between bluetoothctl and hcitool lecc is that bluetoothctl automatically triggers GATT service discovery right after connecting. If your GATT server (from the python-gatt-server project) doesn’t respond correctly to this discovery request, or has invalid service/characteristic configurations, BlueZ (the Linux Bluetooth stack) will drop the connection because it can’t resolve the expected services.
hcitool lecc only establishes the low-level BLE link without initiating GATT service discovery, so it stays connected even if the server’s GATT setup is incomplete.
Step-by-Step Fixes
1. Validate Your GATT Server’s Service Definition
First, double-check the service and characteristic configurations in your python-gatt-server code:
- Ensure all UUIDs (service/characteristic) follow BLE standards (16-bit or properly formatted 128-bit UUIDs).
- Verify characteristic properties (read, write, notify, etc.) are set correctly—missing required properties can cause discovery failures.
- Check the server’s console output for any errors during service registration (e.g., "failed to register service" messages).
2. Skip Connection Checks in bluetoothctl
Try connecting with the --nocheck flag to bypass BlueZ’s default validation (including automatic service discovery):
bluetoothctl power on agent on default-agent scan off # Disable scanning first to avoid conflicts connect C0:EE:40:3A:D0:45 --nocheck
If the connection stays up with this flag, the issue is definitely related to GATT service discovery.
3. Update BlueZ to the Latest Version
Older BlueZ versions on Raspberry Pi have known bugs with BLE GATT interactions. Upgrade to the latest stable release:
sudo apt update && sudo apt upgrade -y bluez
After upgrading, restart the Bluetooth service:
sudo systemctl restart bluetooth
4. Check BlueZ Debug Logs for Exact Errors
To get detailed insights into why the connection drops, run BlueZ in debug mode:
sudo systemctl stop bluetooth sudo bluetoothd -d -n &
Now open a new terminal, run bluetoothctl and attempt the connection again. Look for log lines like GATT service discovery failed or Invalid characteristic attribute—these will point you directly to the root issue.
5. Test GATT Service Discovery with gatttool
Use gatttool to manually test if your server responds to service discovery:
gatttool -b C0:EE:40:3A:D0:45 -I > connect > primary
If the primary command returns a list of services, your server is working correctly. If it throws an error, you’ll need to fix the service registration code in your python-gatt-server setup.
Final Notes
Most of the time, this issue boils down to a misconfigured GATT service that fails BlueZ’s discovery checks. Start with the --nocheck test to confirm, then use debug logs to pinpoint the exact configuration problem in your server code.
内容的提问来源于stack exchange,提问作者Qiao

