Bluez协议栈在安装完以后,会提供两个命令行调试工具,hcitool与gattool,我们可以根据提供的工具来轻松的调试我们的蓝牙设备。
当然
需要注意的是,在调试BLE设备时,需要获得root权限。
蓝牙设备的开启与关闭
首先,查看在使用hci工具时,我们需要查看当前能够识别的蓝牙设备,这里我们需要使用的工具是hciconfig
命令,该命令如ifconfig
一样,可以控制蓝牙设备的开启与关闭,在默认情况下,蓝牙设备在插入host时是不会自动开启的,所以我们在调试之前要先开启设备。
如果我们需要查看蓝牙设备的话:
$ hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: 00:02:72:14:27:0E ACL MTU: 1021:8 SCO MTU: 64:1
DOWN
RX bytes:2715 acl:1 sco:0 events:146 errors:0
TX bytes:2500 acl:0 sco:0 commands:133 errors:0
会输出以上内容,我们可以看到蓝牙设备的编号为hci0
,这是host分配给设备的ID,我们用来启动或关闭设备也是需要该ID来控制。
开启与关闭设备:
#设备打开
sudo hciconfig hci0 up
#设备关闭
sudo hciconfig hci0 down
hcitool命令及参数
在打开蓝牙设备以后,就可以使用hcitool工具集对蓝牙进行控制,工具集参数 分为两部分,一为正常的蓝牙设备调试,二为低功耗即BLE设备, 工具参数如下:
$ hcitool
hcitool - HCI Tool ver 4.93
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