掌握蓝牙调试:hcitool与gatttool全解析-deepseek
摘要:hcitool和gatttool是Linux下常用的蓝牙调试工具。hcitool(v5.64)提供底层HCI控制,支持设备发现(inq/scan/lescan)、连接管理(cc/dc/lecc)和信号监控(rssi/lq)。gatttool专注于GATT层操作,用于BLE服务发现(primary)、特征值读写(char-read-write)和通知处理,支持交互模式(-I)。注意gattto
~/code/tool$ hcitool
hcitool - HCI Tool ver 5.64
Usage:
hcitool [options] <command> [command parameters]
Options:
--help Display help
-i dev HCI device
Commands:
dev Display local devices
inq Inquire remote devices
scan Scan for remote devices
name Get name from remote device
info Get information from remote device
spinq Start periodic inquiry
epinq Exit periodic inquiry
cmd Submit arbitrary HCI commands
con Display active connections
cc Create connection to remote device
dc Disconnect from remote device
sr Switch central/peripheral role
cpt Change connection packet type
rssi Display connection RSSI
lq Display link quality
tpl Display transmit power level
afh Display AFH channel map
lp Set/display link policy settings
lst Set/display link supervision timeout
auth Request authentication
enc Set connection encryption
key Change connection link key
clkoff Read clock offset
clock Read local or remote clock
lescan Start LE scan
leinfo Get LE remote information
lealadd Add device to LE Accept List
lealrm Remove device from LE Accept List
lealsz Read size of LE Accept List
lealclr Clear LE Accept List
lewladd Deprecated. Use lealadd instead.
lewlrm Deprecated. Use lealrm instead.
lewlsz Deprecated. Use lealsz instead.
lewlclr Deprecated. Use lealclr instead.
lerladd Add device to LE Resolving List
lerlrm Remove device from LE Resolving List
lerlclr Clear LE Resolving List
lerlsz Read size of LE Resolving List
lerlon Enable LE Address Resolution
lerloff Disable LE Address Resolution
lecc Create a LE Connection
ledc Disconnect a LE Connection
lecup LE Connection UpdateFor more information on the usage of each command use:
hcitool <command> --help
w@DESKTOP-KREHPF1:~/code/tool$ gatttool
Usage:
gatttool [OPTION?]Help Options:
-h, --help Show help options
--help-all Show all help options
--help-gatt Show all GATT commands
--help-params Show all Primary Services/Characteristics arguments
--help-char-read-write Show all Characteristics Value/Descriptor Read/Write argumentsApplication Options:
-i, --adapter=hciX Specify local adapter interface
-b, --device=MAC Specify remote Bluetooth address
-t, --addr-type=[public | random] Set LE address type. Default: public
-m, --mtu=MTU Specify the MTU size
-p, --psm=PSM Specify the PSM for GATT/ATT over BR/EDR
-l, --sec-level=[low | medium | high] Set security level. Default: low
-I, --interactive Use interactive mode
特性维度 hcitool gatttool 工具层级 底层 HCI 层命令工具 高层 GATT 层属性工具 主要功能 设备发现、基础连接管理、链路监控 GATT服务发现、特征值读写 协议偏向 经典蓝牙 & 低功耗蓝牙(BLE) 专用于低功耗蓝牙(BLE) 关键命令示例 lescan,lecc,info--char-read,--char-write,交互模式现状 部分命令已过时(如 lewladd)已被官方弃用 💡 使用须知与技巧
了解上述区别后,在实际使用中还有一些关键点需要注意:
权限与设备准备:使用 BLE 相关命令(如
lescan)通常需要root权限。在操作前,请确保你的蓝牙适配器已启动,可以使用hciconfig hci0 up命令来开启。
gatttool的常见连接问题:如果你在使用gatttool时遇到连接失败(例如报错 "Transport endpoint is not connected"),而hcitool lecc可以成功,一个非常常见的原因是地址类型不匹配。
解决方法:许多现代BLE设备使用随机地址。在连接时,尝试显式指定地址类型为
random。例如:gatttool -b XX:XX:XX:XX:XX:XX -t random -I。拥抱现代工具:最重要的是,
gatttool已被 BlueZ 官方标记为弃用 (deprecated)。对于新项目或脚本,强烈建议转向使用bluetoothctl命令。它是一个功能更强大、更智能且维护积极的交互式工具,能更好地处理地址类型等连接参数。💎 总结
简单来说,
hcitool就像是你蓝牙设备的“网络探测器和连接器”,负责最基础的链接建立;而gatttool则是建立链接后,与设备进行“数据对话”的专门工具。
hcitool 与 gatttool 详细用法指南
hcitool 详细用法
基本语法
bash
hcitool [options] <command> [command parameters]常用选项
-i dev:指定HCI设备(如hci0, hci1)
--help:显示帮助信息核心命令详解
1. 设备管理
bash
# 显示本地蓝牙设备 hcitool dev # 输出示例: # Devices: # hci0 00:11:22:33:44:55 # 指定设备操作 hcitool -i hci0 dev2. 设备发现与扫描
bash
# 传统蓝牙设备查询 hcitool inq # 扫描设备(更详细) hcitool scan # 低功耗蓝牙扫描 hcitool lescan # 持续LE扫描 hcitool lescan --duplicates --passive3. 设备信息获取
bash
# 获取设备名称 hcitool name <MAC地址> # 获取设备信息 hcitool info <MAC地址> # 示例: hcitool info 11:22:33:44:55:664. 连接管理
bash
# 创建经典蓝牙连接 hcitool cc <MAC地址> # 断开连接 hcitool dc <MAC地址> # 显示活动连接 hcitool con # 创建LE连接 hcitool lecc <MAC地址> # 断开LE连接 hcitool ledc <连接句柄>5. 信号质量监测
bash
# 读取RSSI信号强度 hcitool rssi <MAC地址> # 读取链路质量 hcitool lq <MAC地址> # 读取发射功率级别 hcitool tpl <MAC地址>6. 安全相关
bash
# 请求认证 hcitool auth <MAC地址> # 设置加密 hcitool enc <MAC地址> # 更改链路密钥 hcitool key <MAC地址>实用示例组合
bash
# 完整的设备发现流程 hciconfig hci0 up hcitool -i hci0 lescan hcitool -i hci0 lecc 11:22:33:44:55:66 hcitool -i hci0 con hcitool -i hci0 rssi 11:22:33:44:55:66gatttool 详细用法
基本语法
bash
gatttool [OPTIONS] [COMMANDS]主要选项
-i, --adapter=hciX:指定适配器
-b, --device=MAC:指定目标设备
-t, --addr-type=[public|random]:地址类型
-I, --interactive:交互模式
-m, --mtu=MTU:指定MTU大小
-l, --sec-level=[low|medium|high]:安全级别工作模式
1. 交互模式(推荐)
bash
gatttool -b 11:22:33:44:55:66 -I # 进入交互提示符: [11:22:33:44:55:66][LE]>2. 单次命令模式
bash
gatttool -b 11:22:33:44:55:66 --characteristics交互模式命令详解
连接管理
bash
# 连接设备 connect # 断开连接 disconnect # 退出工具 exit服务发现
bash
# 发现主服务 primary # 发现特定服务的特征 characteristics [开始句柄] [结束句柄] # 发现所有特征描述符 char-desc [开始句柄] [结束句柄]数据读写操作
bash
# 读取特征值 char-read-hnd <句柄> char-read-uuid <UUID> # 写入特征值 char-write-req <句柄> <数据> char-write-cmd <句柄> <数据> # 无响应写入 # 监听通知 char-register-notify <句柄> <on/off>常用GATT操作示例
基础连接与发现
bash
# 连接到设备并发现服务 gatttool -b 11:22:33:44:55:66 -t random -I [11:22:33:44:55:66][LE]> connect [11:22:33:44:55:66][LE]> primary [11:22:33:44:55:66][LE]> characteristics数据读写示例
bash
# 读取电池电量 char-read-uuid 2a19 # 写入设备名称 char-write-req 0x0003 4D7954657374 # 启用通知 char-write-req 0x0012 0100安全连接示例
bash
# 高安全级别连接 gatttool -b 11:22:33:44:55:66 -t random -l high -I实用场景示例
场景1:BLE传感器数据读取
bash
# 扫描设备 hcitool lescan # 连接并读取数据 gatttool -b AA:BB:CC:DD:EE:FF -I connect primary char-read-hnd 0x000c场景2:设备控制
bash
# 向设备发送控制命令 gatttool -b AA:BB:CC:DD:EE:FF --char-write-req -a 0x0012 -n 01场景3:MTU优化
bash
# 使用更大MTU提高传输效率 gatttool -b AA:BB:CC:DD:EE:FF -m 512 -I注意事项
权限要求:大多数操作需要root权限或蓝牙组权限
地址类型:现代BLE设备多使用随机地址,需指定
-t random连接稳定性:BLE连接可能不稳定,需要错误处理
工具状态:
gatttool已过时,新项目建议使用bluetoothctl调试技巧:使用
--help查看具体命令帮助故障排除
常见问题解决
bash
# 设备未找到 sudo hciconfig hci0 reset # 连接失败 gatttool -b MAC -t random -I # 尝试随机地址 # 权限问题 sudo setcap 'cap_net_raw,cap_net_admin+eip' `which gatttool`
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)