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

时间:2021-08-03 17:34:41

星期三, 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选项(#1)

➜  ~  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自动断开,网上说这个工具老了,不建议用了(#2):

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操作环境(#6)

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蓝牙低功耗设备

LINKS

[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

[BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备的更多相关文章

  1. [BlueZ] 3、使用 meshctl 连接控制一个 sig mesh 灯

    目录 前言 1.准备工作 2.meshctl 连接.配置.控制 sig mesh 灯 3.最终效果: LINKS 前言 本文介绍如何使用 meshctl 配对.连接.控制一个 sig mesh 球泡灯 ...

  2. Android 蓝牙开发之搜索、配对、连接、通信大全

            蓝牙( Bluetooth®):是一种无线技术标准,可实现固定设备.移动设备和楼宇个人域网之间的短距离数据 交换(使用2.4-2.485GHz的ISM波段的UHF无线电波).蓝牙设备最 ...

  3. R语言使用RMySQL连接及读写Mysql数据库 测试通过

    R语言使用RMySQL连接及读写Mysql数据库 简单说下安装过程,一般不会有问题,重点是RMySQL的使用方式. 系统环境说明 Redhat系统:Linux 460-42.6.32-431.29.2 ...

  4. web 项目 连接mycat 读写分离失效问题,

    问题描述:mycat 读写分离已配好,在sql工具上查询操作是可以的,但是在项目中,读数据就走write 数据库, 解决      :环境spring +mvc +ibaites,在java中自己写j ...

  5. shell脚本连接、读写、操作mysql数据库实例

    本文介绍了如何在shell中读写mysql数据库.主要介绍了如何在shell 中连接mysql数据库,如何在shell中创建数据库,创建表,插入csv文件,读取mysql数据库,导出mysql数据库为 ...

  6. [国外] 解决Windows10下google搜索连接不上,但其它网页都能成功登入的问题

    我的电脑突然在学校不能登陆google了,在家可以连接但是非常慢. 在网上搜索发现是由于DNS出了问题,在此记录一下. 可以通过控制面板——网络和Internet——网络和共享中心——点击现在使用的网 ...

  7. R语言使用RMySQL连接及读写Mysql数据库

    简单说下安装过程,一般不会有问题,重点是RMySQL的使用方式. 系统环境说明 Redhat系统:Linux 460-42.6.32-431.29.2.el6.x86_64 系统编码:LANG=zh_ ...

  8. redis-cli -h xxxxx -p xxxx monitor 监控host为xxxx,端口为xxx,redis连接及读写操作

    # redis-cli -p monitor OK ] " lua] " lua] " "-1"

  9. 手机通过数据线连接电脑后,找不到设备--Android Studio

    手机通过数据线连接电脑后,找不到可用的设备.允许USB调试,,因为小米手机还要设置USB开发者模式. 1.打开设置/关于手机 四次点击“MIUI版本”如图: 2.返回上一页打开更多设置 看到开发者选项 ...

随机推荐

  1. Mysql自动备份工具1.0(2013年11月15日更新)

    Mysql自动备份工具1.0 下载地址 2013-11-15 1.解决日历控件在Windows7/8/8.1环境下遮挡按钮问题:2.解决按月备份当月没有该日期问题: 2013-11-13 1.Mysq ...

  2. Linux及安全课程——相关链接总结

    附录:学习笔记链接总结 MOOC课程学习笔记与实验: 第一周:计算机是如何工作的 第二周:操作系统是怎么工作的 -- 一个简单的时间片轮转多道程序内核代码及分析 第三周:构造一个简单的Linux系统M ...

  3. Quartz Scheduler(2.2.1) - Usage of JobDataMap

    The JobDataMap can be used to hold any amount of (serializable) data objects which you wish to have ...

  4. css2与css3的区别

    css2与css3的区别 CSS3引进了一些新的元素新的特性,我收集以下,自己做了一个小结: animation(基础动画)eg:  div{animation: myfirst 5s linear ...

  5. PHP 中 static 和 self 的区别

    使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类: 使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的.也可以称之为&quot ...

  6. C#委托冒泡

    委托的实现,就是编译器自行定义了一个类:有三个重要参数1.制定操作对象,2.指定委托方法3.委托链 看如下一个列子: class DelegatePratice { public static voi ...

  7. django——FBV与CBV

    引言 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class bas ...

  8. python按照文件创建日期整理文件至文件夹

    # -*- coding: utf-8 -*- # @Time : 2019-02-15 13:31 # @Author : cxa # @File : sortbydate.py # @Softwa ...

  9. Linux源码包安装过程及注意事项

    众做周知RedHat Linux使用RPM包管理器安装rpm包,但是RPM包是由红帽编译打包的,通常相对于最新版落后了很多,甚至可能缺少我们所要使用的功能.如果你需要定制想要的软件功能.自定义安装路径 ...

  10. JavaScript验证字符串只能包含数字或者英文字符的代码实例

    验证字符串只能包含数字或者英文字符的代码实例:本章节分享一段代码实例,它实现了验证字符串内容是否只包含英文字符或者数字.代码实例如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...