1、使能hci接口

# hciconfig hci0 up

2、使用hcitool搜索BLE设备

# hcitool lescan

LE Scan ...
D0:39:72:BE:D2:26 (unknown)
D0:39:72:BE:D2:26 HMDongle
D0:39:72:BE:D2:26 (unknown)

搜索到设备后按CTRL+C停止搜索,设备名称为HMDongle,是一个蓝牙串口设备,MAC地址为D0:39:72:BE:D2:26

3、使用gatttool连接设备

先查看gatttool的help

# gattool -h

Usage:
  gatttool [OPTION...]

Help Options:
  -h, --help                                Show help options
  --help-all                                Show all help options
  --help-gatt                               Show all GATT commands
  --help-params                             Show all Primary Services/Characteristics arguments
  --help-char-read-write                    Show all Characteristics Value/Descriptor Read/Write arguments

Application Options:
  -i, --adapter=hciX                        Specify local adapter interface
  -b, --device=MAC                          Specify remote Bluetooth address
  -t, --addr-type=[public | random]         Set LE address type. Default: public
  -m, --mtu=MTU                             Specify the MTU size
  -p, --psm=PSM                             Specify the PSM for GATT/ATT over BR/EDR
  -l, --sec-level=[low | medium | high]     Set security level. Default: low
  -I, --interactive                         Use interactive mode

使用interactive方式连接设备

# gatttool -b D0:39:72:BE:D2:26 -I

[   ][D0:39:72:BE:D2:26][LE]>

查看help

[   ][D0:39:72:BE:D2:26][LE]> help
help                                           Show this help
exit                                           Exit interactive mode
quit                                           Exit interactive mode
connect         [address [address type]]       Connect to a remote device
disconnect                                     Disconnect from a remote device
primary         [UUID]                         Primary Service Discovery
characteristics [start hnd [end hnd [UUID]]]   Characteristics Discovery
char-desc       [start hnd] [end hnd]          Characteristics Descriptor Discovery
char-read-hnd   <handle> [offset]              Characteristics Value/Descriptor Read by handle
char-read-uuid  <UUID> [start hnd] [end hnd]   Characteristics Value/Descriptor Read by UUID
char-write-req  <handle> <new value>           Characteristic Value Write (Write Request)
char-write-cmd  <handle> <new value>           Characteristic Value Write (No response)
sec-level       [low | medium | high]          Set security level. Default: low
mtu             <value>                        Exchange MTU for GATT/ATT
[   ][D0:39:72:BE:D2:26][LE]>

连接设备

[   ][D0:39:72:BE:D2:26][LE]> connect
[CON][D0:39:72:BE:D2:26][LE]>

连接成功之后命令行前面会有[CON]标识

查看设备提供的服务

[CON][D0:39:72:BE:D2:26][LE]> primary
[CON][D0:39:72:BE:D2:26][LE]> 
attr handle: 0x0001, end grp handle: 0x000b uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000c, end grp handle: 0x000f uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0xffff uuid: 0000ffe0-0000-1000-8000-00805f9b34fb

查看特性

[CON][D0:39:72:BE:D2:26][LE]> characteristics
[CON][D0:39:72:BE:D2:26][LE]> 
handle: 0x0002, char properties: 0x02, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, char properties: 0x0a, char value handle: 0x0007, uuid: 00002a02-0000-1000-8000-00805f9b34fb
handle: 0x0008, char properties: 0x0a, char value handle: 0x0009, uuid: 00002a03-0000-1000-8000-00805f9b34fb
handle: 0x000a, char properties: 0x02, char value handle: 0x000b, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x000d, char properties: 0x20, char value handle: 0x000e, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x0011, char properties: 0x16, char value handle: 0x0012, uuid: 0000ffe1-0000-1000-8000-00805f9b34fb

其中handle是特性的句柄,char properties是特性的属性值,char value handle是特性值的句柄,uuid是特性的标识;

我想,可以把特性当作是设备特供的寄存器,寄存器会有属性,如只读、只写或可读可写,在蓝牙协议里面,还有notify的属性,当然,这个通知属性当作是寄存器的中断服务也是说得过去的;那么,第一个handle可以理解为寄存器的编号,第二个handle理解为寄存器的地址,properties即是寄存器属性,如果是可读可写,那么直接读写操作寄存器的地址即可。

上面列出了好多个特性,其中只有一个才是读写蓝牙串口的特性,很容易看得出来,它的uuid为ffe1,属性值为0x16,可读可写(without response)可通知。在这里,从蓝牙串口读取数据并不是直接去读取0x0012这个handle,而是通过notify获取数据,首先要使能notify功能,怎么使能呢,就是把特性值handle加1即0x0013,往这个0x0013handle写入0x0100,如:

[CON][D0:39:72:BE:D2:26][LE]> char-write-req 0x0013 0100  
[CON][D0:39:72:BE:D2:26][LE]> Characteristic value was written successfully

往handle写入十六进制值时不要带0x,不然会出错

[CON][D0:39:72:BE:D2:26][LE]> char-write-req 0x0013 0x0100
[CON][D0:39:72:BE:D2:26][LE]> Characteristic Write Request failed: Attribute value length is invalid

如果要向设备发送串口数据就直接往0x0012写入数据即可

[CON][D0:39:72:BE:D2:26][LE]> char-write-cmd 0x0012 48

在这里我碰到了问题,始终获取不到蓝牙串口发送过来的数据,根本无法收到notify,即使加了--listen参数也不行,郁闷啊!首先设备是没问题的,使用lightblue工具都可以正常收发串口数据。

gatttool的使用的更多相关文章

  1. 【蓝牙】蓝牙,调试 hcitool与gatttool实例

    Bluez协议栈在安装完以后,会提供两个命令行调试工具,hcitool与gattool,我们可以根据提供的工具来轻松的调试我们的蓝牙设备,调试BLE设备时,需要获取root权限. 蓝牙设备的开启与关闭 ...

  2. 物联网安全拔“牙”实战——低功耗蓝牙(BLE)初探

    物联网安全拔“牙”实战——低功耗蓝牙(BLE)初探 唐朝实验室 · 2015/10/30 10:22 Author: FengGou 0x00 目录 0x00 目录 0x01 前言 0x02 BLE概 ...

  3. BLE Hacking:使用Ubertooth one扫描嗅探低功耗蓝牙

    0×00 前言 低功耗蓝牙(Low Energy; LE),又视为Bluetooth Smart或蓝牙核心规格4.0版本.其特点具备节能.便于采用,是蓝牙技术专为物联网(Internet of Thi ...

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

    星期三, 05. 九月 2018 02:03上午 - beautifulzzzz 1.前言 上一篇讲了如何编译安装BlueZ-5,本篇主要在于玩BlueZ,用命令行去操作BLE设备: [BlueZ] ...

  5. 玩转BLE(3)_使用微信蓝牙精简协议伪造记步数据

    1. 前言 在物联网时代,有一个问题肯定会让人头疼(现在已经初露端倪了): 物联网中的IOT设备有两个主要特点: 1)简单小巧(不具备复杂的人机交互接口,需要手机等终端设备辅助完成配置.控制等功能). ...

  6. 玩转BLE(1)_Eddystone beacon

    1. 前言 你相信两条命令就可以把自己的破手机变成一个Beacon节点吗?不相信的话就接着往下看吧. 通过前几篇“蓝牙协议分析”相关的文章,特别是“蓝牙协议分析(3)_蓝牙低功耗(BLE)协议栈介绍” ...

  7. ubuntu GCC 版本切换

    (1)  查看gcc以及g++的版本 gcc  -v g++ -v star@ai:~ $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_L ...

  8. 蓝牙学习 (8)配对raspberryPi和SensorTag CC2650

    在上一篇中,用raspberryPi能够扫描到Ti SensorTag. 但是没有获得更多的数据,并且发现sensor Tag并没有回复scan request. https://blog.csdn. ...

  9. 树莓派 - 蓝牙 (1) 试试Beacon

    首先先了解一下bluez, 以及常用的tools. - hcitool.bluetoothctl等工具,可以进行BLE设备的扫描.连接.配对.广播等操作: - hcitool可以发送HCI comma ...

随机推荐

  1. 《DSP using MATLAB》示例 Example 9.14

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  2. FastAdmin 的 url 有一个 ref=addtabs 是怎么添加的?

    FastAdmin 的 url 有一个 ref=addtabs 是怎么添加的? 在使用 FastAdmin 时你会发现 url 中有一个 ref=addtabs . 以下是 Karson 的解释 这个 ...

  3. 解决一个java facets问题

    经常被一个问题困扰: JavaServer Faces 2.2 can not be installed : One or more constraints have not been satisfi ...

  4. 代码规范 for node.js with 'npm-coding-style'

    npm-coding-style npm's "funny" coding style Description npm's coding style is a bit unconv ...

  5. Oracle归档日志与非归档日志的切换及路径设置

    --==================== -- Oracle 归档日志 --==================== Oracle可以将联机日志文件保存到多个不同的位置,将联机日志转换为归档日志的 ...

  6. 【翻译】HTML5开发——轻量级Web Database存储库html5sql.js

    方式1: html5sql官方网址:http://html5sql.com/ 阅读之前,先看W3C关于WEB Database的一段话: Beware. This specification is n ...

  7. pycharm修改代码模板支持中文输出

    python2.x默认不支持中文输出,需要在py的开头添加 #coding: utf- 在pycharm里面,选项,editor,file and code templates,选择python sc ...

  8. Java toString()方法的神奇之处

    Java 手册 toString(String类中) public String toString() 返回此对象本身(它已经是一个字符串!). 指定者: 接口 CharSequence 中的 toS ...

  9. Bootstrap-CL:警告

    ylbtech-Bootstrap-CL:警告 1.返回顶部 1. Bootstrap 警告(Alerts) 本章将讲解警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警 ...

  10. 5月24日上课笔记-js操作DOM

    解析properpties配置文件 类加载器 ResourceBundle 一.jquery操作DOM 1.jquery操作css css("",""); cs ...