码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【全志T113-S3_100ask】12-2 Linux蓝牙通信实战(BLE初窥)


    【全志T113-S3_100ask】12-2 Linux蓝牙通信实战(BLE初窥)

    • 背景
    • (一)初始化蓝牙
    • (二)Linux下蓝牙工具(bluez 工具集)
    • (三)蓝牙扫描
    • (四)使用Bluetoothctl创建蓝牙服务
    • (五)其他操作
      • 1、启动/关闭/重启蓝牙
      • 2、测试蓝牙是否可达 l2ping
      • 3、查看功能与服务
      • 4、查看蓝牙的状态和信息
      • 5、查看蓝牙设备
      • 6、开启/关闭蓝牙广播
      • 7、查看已经连接的BLE设备
      • 8、 蓝牙认证打开/关闭
      • 9、 查看/改变蓝牙主从状态
      • 10、查看/设置蓝牙名称
      • 11、 查看支持的链路层状态
      • 12、列出低功耗蓝牙的服务
    • (六)入门推荐阅读

    背景

    介绍完BLE之后,下面简单操作蓝牙

    (一)初始化蓝牙

    # 复位蓝牙
    echo 0 > /sys/class/rfkill/rfkill0/state
    sleep 1
    echo 1 > /sys/class/rfkill/rfkill0/state
    sleep 1
    
    # 绑定蓝牙设备
    hciattach -n ttyS1 xradio > /dev/null 2>&1 &
    sleep 8
    
    # 启用蓝牙设备
    hciconfig hci0 up
    hciconfig hci0 piscan
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (二)Linux下蓝牙工具(bluez 工具集)

    • hcitool、bluetoothctl等工具,可以进行BLE设备的扫描、连接、配对、广播等操作
    • hcitool 可以发送HCI command,设置BLE的广播数据
    • bluetoothctl 可以新增蓝牙服务,返回回调等操作
    • sdptool 查看蓝牙信息和提供的服务
    • hciconfig 查看蓝牙信息
    • l2ping 测试蓝牙的连通性
    • gatttool :可以在GATT层面,完成GATT profile的连接、service attribute的读写等操作【更多】

    (三)蓝牙扫描

    hcitool scan 	#扫描经典蓝牙
    
    hcitool lescan	 #扫描BL低功耗蓝牙
    
    • 1
    • 2
    • 3

    (四)使用Bluetoothctl创建蓝牙服务

    1、先启动蓝牙,进入可搜索状态

    # 复位蓝牙
    echo 0 > /sys/class/rfkill/rfkill0/state
    sleep 1
    echo 1 > /sys/class/rfkill/rfkill0/state
    sleep 1
    
    # 绑定设备
    hciattach -n ttyS1 xradio > /dev/null 2>&1 &
    sleep 8
    
    # 启用蓝牙设备
    hciconfig hci0 up
    hciconfig hci0 piscan
    
    # 免认证
    hciconfig hci0 auth
    # 广播
    hciconfig hci0 leadv
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2、进入 bluetoothctl 交互界面

    # bluetoothctl
    Agent registered
    [CHG] Controller 22:92:C9:8C:04:EA Pairable: yes
    [bluetooth]#
    
    • 1
    • 2
    • 3
    • 4

    3、手机使用BLE调试助手连接蓝牙,如下图所示

    4、连接后,bluetoothctl会发生变化,此时已经进入交互界面

    4、但是我们没有其他服务,只能做一些简单的操作,比如读取,因为现有的服务是 bluetoothctl 提供的

    5、下面我们添加自己的服务
    5.1 进入菜单

    [6B-3C-BF-AD-2A-A8]# menu gatt
    Menu gatt:
    Available commands:
    -------------------
    list-attributes [dev/local]                       List attributes
    select-attribute <attribute/UUID>                 Select attribute
    attribute-info [attribute/UUID]                   Select attribute
    read [offset]                                     Read attribute value
    write <data=xx xx ...> [offset] [type]            Write attribute value
    acquire-write                                     Acquire Write file descriptor
    release-write                                     Release Write file descriptor
    ....
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5.2 添加自己的service和characteristic

    [bluetoothctl] register-service 0xFFFF # (Choose yes when asked if primary service)
    [bluetoothctl] register-characteristic 0xAAAA read       # (Select a value of 1 when prompted)# 输入的值是初始值,每次读取,会加一
    [bluetoothctl] register-characteristic 0xBBBB read,write # (Select a value of 0 when prompted)
    [bluetoothctl] register-characteristic 0xCCCC read       # (Select a value of 2 when prompted)
    [bluetoothctl] register-application # (This commits the services/characteristics and registers the profile)
    [bluetoothctl] back
    [bluetoothctl] advertise on 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    通过 show 操作可以看到刚才添加的0xffff 服务
    在这里插入图片描述

    5.3 手机重新连接蓝牙
    服务从 3 项变为 4 项,第 4 项即为我们刚才添加的服务。

    5.4 读写操作
    在有上传按钮的服务上传数据

    此时收到:

    在这里插入图片描述
    读操作的话,直接读取就好了,值为刚才我们设置的值。
    至此测试完毕

    (五)其他操作

    1、启动/关闭/重启蓝牙

    hciconfig hci0 up  		#启动蓝牙设备
    hciconfig hci0 off 		#关闭蓝牙设备
    hciconfig hci0 reset 	#重启蓝牙设备
    
    • 1
    • 2
    • 3

    2、测试蓝牙是否可达 l2ping

    l2ping -i hci0 -c 4 21:12:A3:C4:50:66
    
    • 1

    3、查看功能与服务

    sdptool browse local    # local可以改成其它蓝牙的mac地址
    
    • 1

    4、查看蓝牙的状态和信息

     hciconfig -a 
    
    • 1

    5、查看蓝牙设备

    hcitool dev
    Devices:
            hci0    22:22:2A:B0:9C:3C
    
    • 1
    • 2
    • 3

    6、开启/关闭蓝牙广播

    hciconfig hci0 leadv / noleadv
    
    • 1

    7、查看已经连接的BLE设备

    (此时调试助手已经连接)

    hcitool -i hci0 con
    Connections:
            > LE 60:AB:D1:B5:A6:FB handle 0 state 1 lm PERIPHERAL
    
    • 1
    • 2
    • 3

    8、 蓝牙认证打开/关闭

    直接体现在进行蓝牙连接时,是否输入连接PIN密码,用于PIN配对

    hciconfig hci0 auth/noauth
    
    • 1

    9、 查看/改变蓝牙主从状态

    hciconfig hci0 lm master、hciconfig hci0 lm slave
    
    • 1

    10、查看/设置蓝牙名称

    hciconfig hci0 name 、hciconfig hci0 name BLXX
    
    • 1

    11、 查看支持的链路层状态

    hciconfig hci0 lestates
    
    • 1

    12、列出低功耗蓝牙的服务

    bluetoothctl --monitor gatt.list-attributes
    
    • 1

    其他操作自己慢慢摸索哈,我一个人只能啃成这样了,如有写得不对的地方大佬指正。

    (六)入门推荐阅读

    • 【蓝牙】蓝牙,调试 hcitool与gatttool实例
    • Android蓝牙开发—经典蓝牙和BLE(低功耗)蓝牙的区别
  • 相关阅读:
    Delphi编程中的按键模拟及应用——使用SendInput函数实现按键模拟
    【鸿蒙 HarmonyOS 4.0】TypeScript开发语言
    Project软件为什么设置完成百分比为100%,是这样显示的啊!
    展锐S 上微距模式不识别问题跟进 --- sensor_config的解析
    MybatisX快速生成代码(mybatis plus模板)
    Portraiture5全新升级版磨皮滤镜插件神器
    【QT开发(10)】QT 进程
    数据链路层--以太网
    NPM v0 包捐赠给 Vercel 后续—— v0.dev
    国庆周《LInux学习第四课》
  • 原文地址:https://blog.csdn.net/qq_46079439/article/details/126244998
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号