欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

程序员文章站 2022-06-10 10:25:33
星期三, 05. 九月 2018 02:03上午 beautifulzzzz ![][ bar] 1、前言 上一篇讲了如何编译安装BlueZ 5,本篇主要在于玩BlueZ,用命令行去操作BLE设备: [ [BlueZ] 1、Download install and use the BlueZ and ......

星期三, 05. 九月 2018 02:03上午 - beautifulzzzz

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

1、前言

上一篇讲了如何编译安装bluez-5,本篇主要在于玩bluez,用命令行去操作ble设备:

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

2、gatttool —— 老工具趟坑

刚开始跟着 get started with bluetooth low energy on linux 操作gatttool,发现坑太多(主要原因是工具老了):

采用sudo 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.

过一回会10s自动断开,网上说这个工具老了,不建议用了():

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.

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

3、bluetoothctl——nb的新工具

命令行进入bluetoothctl操作环境()

bluetoothctl

我在手机上用lightblue模拟一个ble设备ty_prod,之后对其service进行修改,调用scan on进行搜索还是老的,
最终发现要先用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]

索性就用iphone自带的服务做测试了~

[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

我们用current time service,列出所有attributes操作如下:

[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对应的服务如下图:

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

我们选择current time进行操作uuid:0x2a2b

[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

读出结果大致意思应该是:2018-9/5-1:36:17 周三

读取一下0x180a的device information:

[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    

当然写、使能notify也很简单,看help即可。最后断开连接、并退出!!!

[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

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

[1].cannot connect to ble device on raspberry pi
[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

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

@beautifulzzzz
智能硬件、物联网,热爱技术,关注产品
博客:http://blog.beautifulzzzz.com
园友交流群:414948975