[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备
星期三, 05. 九月 2018 02:03上午 - beautifulzzzz
1、前言
上一篇讲了如何编译安装bluez-5,本篇主要在于玩bluez,用命令行去操作ble设备:
2、gatttool —— 老工具趟坑
刚开始跟着 get started with bluetooth low energy on linux 操作gatttool,发现坑太多(主要原因是工具老了):
采用
过一回会10s自动断开,网上说这个工具老了,不建议用了():
命令行进入bluetoothctl操作环境():
我在手机上用lightblue模拟一个ble设备ty_prod,之后对其service进行修改,调用scan on进行搜索还是老的, 索性就用iphone自带的服务做测试了~ 我们用current time service,列出所有attributes操作如下: 上面 我们选择current time进行操作uuid:0x2a2b 读出结果大致意思应该是:2018-9/5-1:36:17 周三 读取一下0x180a的device information: 当然写、使能notify也很简单,看help即可。最后断开连接、并退出!!! [1].cannot connect to ble device on raspberry pisudo gatttool -b 4d:69:98:0e:91:5e -i
去连接
发现会报错:error: connect error: connection refused (111)
最终参考link-11发现需要加random选项():➜ ~ sudo gatttool -b 4d:69:98:0e:91:5e -i
[4d:69:98:0e:91:5e][le]> connect
attempting to connect to 4d:69:98:0e:91:5e
error: connect error: connection refused (111)
[4d:69:98:0e:91:5e][le]> exit
➜ ~ sudo gatttool -t random -b 4d:69:98:0e:91:5e -i
[4d:69:98:0e:91:5e][le]> connect
attempting to connect to 4d:69:98:0e:91:5e
connection successful
[4d:69:98:0e:91:5e][le]>
(gatttool:3104): glib-warning **: invalid file descriptor.
there are new tools to use with gatt, bluetoothctl/bluetoothd is the preferred since with that you have gap, etc,
but if want to use a stand alone tool then i suggest you use btgatt-client.
3、bluetoothctl——nb的新工具
bluetoothctl
最终发现要先用remove移除之前的设备,之后再scan就会出现[new] device 72:3b:e1:81:4e:4f ty_prod
设备
注: 用lightblue模拟的设备的mac不是固定的
注: 我发现在lightblue中无论怎么模拟ble设备,一旦被连上搜索到的service都是ipone的[bluetooth]# devices
device 28:ed:6a:a0:26:b7 ty_prod
device 58:71:33:00:00:24 bluetooth keyboard
device 00:1a:7d:da:71:0a shen-pc
device 94:87:e0:b3:ac:6f mi phone
[bluetooth]# remove 28:ed:6a:a0:26:b7
...
[bluetooth]# scan on
discovery started
[new] device 72:3b:e1:81:4e:4f ty_prod
[bluetooth]# scan off
...
discovery stopped
[bluetooth]# connect 72:3b:e1:81:4e:4f
attempting to connect to 72:3b:e1:81:4e:4f
[chg] device 72:3b:e1:81:4e:4f connected: yes
connection successful
[ty_prod]
[ty_prod]# info
device 28:ed:6a:a0:26:b7 (public)
name: tuya_mdev_test
alias: tuya_mdev_test
appearance: 0x0040
icon: phone
paired: yes
trusted: no
blocked: no
connected: yes
legacypairing: no
uuid: fax (00001111-0000-1000-8000-00805f9b34fb)
uuid: generic access profile (00001800-0000-1000-8000-00805f9b34fb)
uuid: generic attribute profile (00001801-0000-1000-8000-00805f9b34fb)
uuid: current time service (00001805-0000-1000-8000-00805f9b34fb)
uuid: device information (0000180a-0000-1000-8000-00805f9b34fb)
uuid: battery service (0000180f-0000-1000-8000-00805f9b34fb)
uuid: vendor specific (7905f431-b5ce-4e99-a40f-4b1e122d00d0)
uuid: vendor specific (89d3502b-0f36-433a-8ef4-c502ad55f8dc)
uuid: vendor specific (9fa480e0-4967-4542-9390-d343dc5d04ae)
uuid: vendor specific (d0611e78-bbb4-4591-a5f8-487910ae4366)
[chg] device 28:ed:6a:a0:26:b7 servicesresolved: no
[chg] device 28:ed:6a:a0:26:b7 connected: no
[tuya_mdev_test]# menu gatt
[tuya_mdev_test]# list-attributes 28:ed:6a:a0:26:b7
...
primary service
/org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041
00001805-0000-1000-8000-00805f9b34fb
current time service
characteristic
/org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041/char0045
00002a0f-0000-1000-8000-00805f9b34fb
local time information
characteristic
/org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041/char0042
00002a2b-0000-1000-8000-00805f9b34fb
current time
descriptor
/org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041/char0042/desc0044
00002902-0000-1000-8000-00805f9b34fb
client characteristic configuration
...
current time service
对应的服务如下图:[ty_prod]# select-attribute /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041/char0042
[tuya_mdev_test:/service0041/char0042]# read
attempting to read /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041/char0042
[chg] attribute /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041/char0042 value:
e2 07 09 05 01 24 11 03 f1 02 .....$....
e2 07 09 05 01 24 11 03 f1 02 .....$....
[tuya_mdev_test:/service0041/char0042]# attribute-info
characteristic - current time
uuid: 00002a2b-0000-1000-8000-00805f9b34fb
service: /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0041
value:
e2 07 09 05 01 2e 01 03 f5 02 ..........
notifying: yes
flags: read
flags: notify
[tuya_mdev_test:/service0006/char0007]# select-attribute /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0047/char004a
[tuya_mdev_test:/service0047/char004a]# attribute-info
characteristic - model number string
uuid: 00002a24-0000-1000-8000-00805f9b34fb
service: /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0047
flags: read
[tuya_mdev_test:/service0047/char004a]# read
attempting to read /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0047/char004a
[chg] attribute /org/bluez/hci0/dev_47_b1_26_c1_81_18/service0047/char004a value:
69 50 68 6f 6e 65 36 2c 32 iphone6,2
69 50 68 6f 6e 65 36 2c 32 iphone6,2
[tuya_mdev_test:/service0047/char004a]# disconnect 28:ed:6a:a0:26:b7
attempting to disconnect from 28:ed:6a:a0:26:b7
[chg] device 28:ed:6a:a0:26:b7 servicesresolved: no
successful disconnected
[chg] device 28:ed:6a:a0:26:b7 connected: no
[bluetooth]# quit
links
[2].invalid file descriptor gatttool of bluez 5.32
[3].get started with bluetooth low energy on linux
[4].reverse engineering a bluetooth low energy light bulb
[5].doing bluetooth low energy on linux
[6].tutorial: ble pairing the raspberry pi 3 model b with hexiwear@beautifulzzzz
智能硬件、物联网,热爱技术,关注产品
博客:http://blog.beautifulzzzz.com
园友交流群:414948975