命令hcitool
是蓝牙工具中使用得非常广泛的工具,刚开始接触它的时候,对它的许多选项都非常模糊,于是最近花了点时间专门研究了下,于是就整理成了下面的这篇博客
hcitool 命令选项
hcitool
命令有许多的自选项,我们来一一查看这些选项:
$ hcitool --help
hcitool - HCI Tool ver 5.50
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 master/slave 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
lewladd Add device to LE White List
lewlrm Remove device from LE White List
lewlsz Read size of LE White List
lewlclr Clear LE White List
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 Update
For more information on the usage of each command use:
hcitool <command> --help
下面将对这些命令进行详细介绍
各个子命令的应用实例
dev命令
该命令用于查询系统上有几个蓝牙设备,并显示出蓝牙设备的地址
$hcitool dev
Devices:
hci1 00:1A:7D:DA:71:0B
hci0 E4:5F:01:3D:DA:11
我系统上有两个蓝牙设备,于是他们都被显示出来
inq命令
发现周围蓝牙设备的信号
$ hcitool -i hci0 inq
Inquiring ...
11:11:22:33:35:2E clock offset: 0x2412 class: 0x240404
scan 命令
该命令就类似于手机上扫描蓝牙的功能,他会显示出蓝牙名字和设备地址
$ hcitool -i hci0 scan
Scanning ...
A6:00:6B:CD:B2:02 headset
88:A9:B7:E6:F1:8F MyPhone
因为我系统上有2个蓝牙设备,所以我增加了选项-i hci0
表示用这个设备来执行scan
命令
scan
命令还可以带一些参数,可以查看下
$ hcitool scan --help
Usage:
scan [--length=N] [--numrsp=N] [--iac=lap] [--flush] [--class] [--info] [--oui] [--refresh]
其中用的比较多的就是--length=N
表示扫描时间的长短,时间等于1.28s * N
,--info
表示显示详细信息
$ hcitool -i hci0 scan --info
Scanning ...
BD Address: 88:A9:B7:E6:F1:8F [mode 1, clkoffset 0x6528]
Device name: MyPhone
--class
还可以查询出设备的类型
$ hcitool -i hci0 scan --class
Scanning ...
BD Address: 88:A9:B7:E6:F1:8F [mode 1, clkoffset 0x6721]
Device name: MyPhone
Device class: Phone, Smart phone (0x7a020c)
其余参数可以自行尝试
name 命令
当我们只知道设备的蓝牙地址而不知道名字的时候,可以用这个命令查询设备名
$ hcitool -i hci0 name 88:A9:B7:E6:F1:8F
MyPhone
info 命令
该命令需要蓝牙地址作为输入参数,它的功能是查询对应设备的详细信息
$ sudo hcitool -i hci0 info 88:A9:B7:E6:F1:8F
Requesting information ...
BD Address: 88:A9:B7:E6:F1:8F
Device Name: MyPhone
LMP Version: (0xa) LMP Subversion: 0x4228
Manufacturer: Broadcom Corporation (15)
Features page 0: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
<3-slot packets> <5-slot packets> <encryption> <slot offset>
<timing accuracy> <role switch> <sniff mode> <RSSI>
<channel quality> <SCO link> <HV2 packets> <HV3 packets>
<u-law log> <A-law log> <CVSD> <paging scheme> <power control>
<transparent SCO> <broadcast encrypt> <EDR ACL 2 Mbps>
<EDR ACL 3 Mbps> <enhanced iscan> <interlaced iscan>
<interlaced pscan> <inquiry with RSSI> <extended SCO>
<EV4 packets> <EV5 packets> <AFH cap. slave>
<AFH class. slave> <LE support> <3-slot EDR ACL>
<5-slot EDR ACL> <sniff subrating> <pause encryption>
<AFH cap. master> <AFH class. master> <EDR eSCO 2 Mbps>
<EDR eSCO 3 Mbps> <3-slot EDR eSCO> <extended inquiry>
<LE and BR/EDR> <simple pairing> <encapsulated PDU>
<err. data report> <non-flush flag> <LSTO> <inquiry TX power>
<EPC> <extended features>
Features page 1: 0x0f 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Features page 2: 0x7f 0x07 0x00 0x00 0x00 0x00 0x00 0x00
可见,,信息非常全面,我终于知道了我的手机的蓝牙芯片是Broadcom公司生产的
cmd命令
这个命令非常好用,它允许我们不用写代码就可以发送自己想发的任何HCI命令,我们先看下帮助信息
$ hcitool cmd --help
Usage:
cmd <ogf> <ocf> [parameters]
Example:
cmd 0x03 0x0013 0x41 0x42 0x43 0x44
从帮助信息看出,cmd
命令接收一串hex数据作为输入参数,上面给出了一个例子cmd 0x03 0x0013 0x41 0x42 0x43 0x44
我们查询蓝牙Spec可以知道,这一串数字表示设置蓝牙名字,0x41 0x42 0x43 0x44
的ascii形式是1234
,即这条命令是把本地蓝牙名称设置为1234
,我们可以试一下
$ sudo hcitool cmd 0x03 0x0013 0x41 0x42 0x43 0x44
< HCI Command: ogf 0x03, ocf 0x0013, plen 4
41 42 43 44
> HCI Event: 0x0e plen 4
01 13 0C 00
从上面看出,命令已经被成功执行了,0x0e
表示command complete event
,最有一个一个字节00
是命令的返回码,表示命令执行成功,有兴趣的还可以尝试更多的命令
con 命令
该命令显示设备当前所有的蓝牙连接,包括经典蓝牙的连接和BLE的连接
$ hcitool -i hci0 con
Connections:
< LE 12:34:56:C2:9C:C7 handle 64 state 1 lm MASTER
cc 命令
该命令用于创建经典蓝牙的连接
dc
该命令用于断开经典蓝牙的连接
sr
该命令用于经典蓝牙切换Master/Slave
lescan 命令
该命令用于扫描周围的BLE设备,也就是低功耗蓝牙
$ sudo hcitool -i hci0 lescan
LE Scan ...
51:85:4C:6C:52:CC (unknown)
51:85:4C:6C:52:CC (unknown)
72:BE:89:B7:24:BA (unknown)
72:BE:89:B7:24:BA (unknown)
该命令还可以接收一些扫描参数,可以自行尝试
$ hcitool lescan --help
Usage:
lescan [--privacy] enable privacy
lescan [--passive] set scan type passive (default active)
lescan [--whitelist] scan for address in the whitelist only
lescan [--discovery=g|l] enable general or limited discoveryprocedure
lescan [--duplicates] don't filter duplicates
leinfo 命令
该命令用于获取BLE设备的信息,它接收一个蓝牙地址作为参数输入
$ sudo hcitool -i hci0 leinfo 12:34:56:C2:9C:C7
Requesting information ...
Handle: 70 (0x0046)
LMP Version: 5.0 (0x9) LMP Subversion: 0x321
Manufacturer: Broadcom Corporation (15)
Features: 0xff 0x00 0x00 0x08 0x00 0x00 0x00 0x00
LE白名单命令
获取白名单列表大小
$ sudo hcitool -i hci0 lewlsz
添加白名单
$ sudo hcitool -i hci0 lewladd 12:34:56:C2:9C:C7
移除白名单
$ sudo hcitool -i hci0 lewlrm 12:34:56:C2:9C:C7
清空白名单
$ sudo hcitool -i hci0 lewlclr
BLE的连接命令
连接BLE设备
$ sudo hcitool -i hci0 lecc 12:34:56:C2:9C:C7
Connection handle 64
断开BLE连接
$ sudo hcitool -i hci0 ledc 64
更新BLE连接参数
$ sudo hcitool -i hci0 lecup --help
Usage:
lecup <handle> <min> <max> <latency> <timeout>
Options:
--handle=<0xXXXX> LE connection handle
--min=<interval> Range: 0x0006 to 0x0C80
--max=<interval> Range: 0x0006 to 0x0C80
--latency=<range> Slave latency. Range: 0x0000 to 0x03E8
--timeout=<time> N * 10ms. Range: 0x000A to 0x0C80
min/max range: 7.5ms to 4s. Multiply factor: 1.25ms
timeout range: 100ms to 32.0s. Larger than max interval
$ sudo hcitool -i hci0 lecup --handle 64 --timetou 100