一、 BLE

现在低功耗蓝牙(BLE)连接都是建立在 GATT (Generic Attribute Profile) 协议之上。GATT 是一个在蓝牙连接之上的发送和接收很短的数据段的通用规范,这些很短的数据段被称为属性(Attribute)。

二、 GAP

详细介绍 GATT 之前,需要了解 GAP(Generic Access Profile),它在用来控制设备连接和广播。GAP 使你的设备被其他设备可见,并决定了你的设备是否可以或者怎样与合同设备进行交互。例如 Beacon 设备就只是向外广播,不支持连接,小米手环就等设备就可以与中心设备连接。

1. 设备角色

GAP 给设备定义了若干角色,其中主要的两个是:外围设备(Peripheral)和中心设备(Central)。

  • 外围设备:这一般就是非常小或者简单的低功耗设备,用来提供数据,并连接到一个更加相对强大的中心设备。例如小米手环。
  • 中心设备:中心设备相对比较强大,用来连接其他外围设备。例如手机等。

三、GATT

GATT 的全名是 Generic Attribute Profile(姑且翻译成:普通属性协议),它定义两个 BLE 设备通过叫做 Service 和 Characteristic 的东西进行通信。GATT 就是使用了 ATT(Attribute Protocol)协议,ATT 协议把 Service, Characteristic遗迹对应的数据保存在一个查找表中,次查找表使用 16 bit ID 作为每一项的索引。

一旦两个设备建立起了连接,GATT 就开始起作用了,这也意味着,你必需完成前面的 GAP 协议。这里需要说明的是,GATT 连接,必需先经过 GAP 协议。实际上,我们在 Android 开发中,可以直接使用设备的 MAC 地址,发起连接,可以不经过扫描的步骤。这并不意味不需要经过 GAP,实际上在芯片级别已经给你做好了,蓝牙芯片发起连接,总是先扫描设备,扫描到了才会发起连接。

GATT 连接需要特别注意的是:GATT 连接是独占的。也就是一个 BLE 外设同时只能被一个中心设备连接。一旦外设被连接,它就会马上停止广播,这样它就对其他设备不可见了。当设备断开,它又开始广播。

中心设备和外设需要双向通信的话,唯一的方式就是建立 GATT 连接。

一个外设只能连接一个中心设备,而一个中心设备可以连接多个外设。一旦建立起了连接,通信就是双向的了,对比前面的 GAP 广播的网络拓扑,GAP 通信是单向的。如果你要让两个设备外设能通信,就只能通过中心设备中转。

GATT 事务是建立在嵌套的Profiles, Services 和 Characteristics之上的

  • Profile Profile 并不是实际存在于 BLE 外设上的,它只是一个被 Bluetooth SIG 或者外设设计者预先定义的 Service 的集合。例如心率Profile(Heart Rate Profile)就是结合了 Heart Rate Service 和 Device Information Service。所有官方通过 GATT Profile 的列表可以从这里找到。

  • Service Service 是把数据分成一个个的独立逻辑项,它包含一个或者多个 Characteristic。每个 Service 有一个 UUID 唯一标识。 UUID 有 16 bit 的,或者 128 bit 的。16 bit 的 UUID 是官方通过认证的,需要花钱购买,128 bit 是自定义的,这个就可以自己随便设置。

  • Characteristic 在 GATT 事务中的最低界别的是 Characteristic,Characteristic 是最小的逻辑数据单元,当然它可能包含一个组关联的数据,例如加速度计的 X/Y/Z 三轴值。

Here are the roles and responsibilities that apply when an Android device interacts with a BLE device:

  • Central vs. peripheral. This applies to the BLE connection itself. The device in the central role scans, looking for advertisement, and the device in the peripheral role makes the advertisement.
  • GATT server vs. GATT client. This determines how two devices talk to each other once they've established the connection.

To understand the distinction, imagine that you have an Android phone and an activity tracker that is a BLE device. The phone supports the central role; the activity tracker supports the peripheral role (to establish a BLE connection you need one of each—two things that only support peripheral couldn't talk to each other, nor could two things that only support central).

Once the phone and the activity tracker have established a connection, they start transferring GATT metadata to one another. Depending on the kind of data they transfer, one or the other might act as the server. For example, if the activity tracker wants to report sensor data to the phone, it might make sense for the activity tracker to act as the server. If the activity tracker wants to receive updates from the phone, then it might make sense for the phone to act as the server.

In the example used in this document, the Android app (running on an Android device) is the GATT client. The app gets data from the GATT server, which is a BLE heart rate monitor that supports the Heart Rate Profile. But you could alternatively design your Android app to play the GATT server role. See BluetoothGattServer for more information.

总结一下: phone                       BLE device(小米手环)

central                     peripheral

scan                        advertiser

gatt server   ===》  gatt client

gatt client   《===  gatt server

Bluetooth BLE in Android相关推荐

  1. Android Bluetooth BLE相关开发资源汇总

    Android开启蓝牙开关 转载自Android:Bluetooth 的打开和关闭 检查系统蓝牙是否开启 BluetoothManager bluetoothManager = (BluetoothM ...

  2. android ble peripheral,Android - BlueTooth BLE 之 Central 与 Peripheral 理解

    一.前言 Andorid 5.0 之前是无法进行 外围设备开发的,在Android 5.0 API 21 android.bluetooth.le包下,新增加 Scaner相关类和 Advertise ...

  3. Android - BlueTooth BLE 之 Central 与 Peripheral

    一.前言 Andorid 5.0 之前是无法进行 外围设备开发的,在Android 5.0 API 21 android.bluetooth.le包下,新增加 Scaner相关类和 Advertise ...

  4. android ble 传输数据,Android BLE 连接及数据传输详解

    本文将展开对蓝牙低功耗从扫描蓝牙设备,建立连接到蓝牙数据通信的详细介绍,以及详细介绍GATT Profile(Generic Attribute Profile,通用属性协议)的组成结构. 权限和fe ...

  5. android上位机编程,BLE的Android上位机开发(上)

    原标题:BLE的Android上位机开发(上) 各位坛友大家好啊! 上篇BLE的Android开发小技巧(见帖:BLE4.0安卓上位机开发小技巧),但因为当时Android上位机还没有完全开发完毕,B ...

  6. android ble notify,Android BLE 以Notify的方式接收数据

    Log 欲善其事,先利其器.推荐一款功能强大的测试工具:nRF Connect 一般情况蓝牙设备所有的UUID都是硬件工程师提供的,但也有例外,比如和我对接的硬件工程师连UUID是什么都不知道--那你 ...

  7. Android ble covana,Android BLE低功耗蓝牙开发

    最近做了一个智能硬件开发(针灸仪)的项目,有一部分涉及到低功耗蓝牙的开发,就是通过蓝牙和设备进行数据的交互,比如控制改设备的LED的开关,设备的开关机,设置设备的时间和温度等,下面就项目中遇到的坑一一 ...

  8. android ble 大小,Android BLE中传输数据的最大长度怎么破

    好多小伙伴们都被一个事儿困扰过: 想在gatt client上(一般是手机上)传输长一点的数据给gatt server(一般是一个Bluetooth smart设备,即只有BLE功能的设备),但通过 ...

  9. blood pressure android app,Blood Pressure Monitor via Bluetooth/Internet in Android

    问题 I am looking out for a hardware which will push data into into the Android device. The hardware b ...

最新文章

  1. 微软推出VS Code新特性,为TypeScript和JavaScript用户提供AI辅助开发功能
  2. PPT 下载 | 神策数据张涛:企业服务客户全生命周期运营三步曲客情诊断 解决方案库...
  3. zabbix源码安装 令人窒息的操作
  4. Sublime Text2 中文乱码
  5. 解压大于4g的文件_4G显卡“到期”,ETH2.0即将到来,以太坊矿工何去何从?
  6. Oracle VM VirtualBox 随系统自动启动虚拟机的方法
  7. Vue生命周期钩子函数
  8. html5 拖放游戏,HTML5拖放API实现拖放排序的实例代码
  9. 云场景实践研究第79期:熊猫直播
  10. java电话号码生成器
  11. 系统软件版本变更规范
  12. 人教版,北师大版,北京版和苏教版的四年级数学知识点对比(附视频)
  13. 电脑xls图标未正常显示
  14. JAVA-pdfBox-纯文件流实现PDF文件加水印后转PDF图片文件下载
  15. vue如何返回上一页效果
  16. 2010世界财富排行榜
  17. 设置窗体显示在屏幕的位置
  18. PDF文件如何修改编辑,怎么添加空白页面
  19. 计算机毕业设计Java宠物互助领售平台(源码+系统+mysql数据库+lw文档)
  20. 详述 MIMIC 数据库 26张数据表(二)之 五种字典表

热门文章

  1. Android 性能优化五大误区和两大疑点!
  2. 会声会影视频剪辑详细教程
  3. python计算连续复利_复利的Python程序
  4. 2020大数据学习资料,全套源码无加密网盘下载
  5. 再牛的键盘也敲不出我的孤单
  6. 专转本计算机必背知识点,江苏专转本计算机必考重点(精华版).doc
  7. BWAPP靶场-HTML injection-Reflected(POST)
  8. solar2lunar 实现农历、天干地支
  9. Bonjour 服务
  10. 勾股定理计算机语言,勾股定理