Android - BlueTooth BLE 之 Central 与 Peripheral 理解
来源:互联网 发布:arcgis裁剪栅格数据 编辑:程序博客网 时间:2024/05/22 02:12
一.前言
Andorid 5.0 之前是无法进行 外围设备开发的,在Android 5.0 API 21 android.bluetooth.le包下,新增加 Scaner相关类和 Advertiser 相关类。目前最后使用Scanner相关类实现蓝牙扫描。这段时间对蓝牙的学习与理解,对中心设备与周边设备做下面总结。
先看下 android.bluetooth.le
包下内容:
1. Android BLE 周边设备 (Peripheral)可以通过 Advertiser 相关类实现操作;
2. Android BLE 中心设备 (Central)可以通过 Scanner相关类实现蓝牙扫描;
3. Android BLE 建立中心连接的时候,使用 BlueToothDevice#connectGatt()
实现
如果你对 Central 与 Peripheral 理解的话,就移步下面文章 !
Android 5.0 BLE 实现中心与外围设备 (待更…)
二. Central 和 Peripheral
1. 蓝牙通信规则
Bluetooth BLE 的交互有两个角色 : Central
与 Peripheral
。可以对比传统的CS结构,理解为客户端-服务端
结构。
- 服务端 (外围设备):
Peripheral
通常具有其他设备所需要的数据,即数据提供服务; 客户端(中心设备):
Central
通常通过使用Perpheral
的信息来实现一些特定的功能 ;例如 : 如下图所示,心跳监听器提供心跳数据,在你的其他设备的app上 需要以用户友好的方式显示用户的心跳信息。
(来源于网络)
2.Central 发现并连接广播中的 Peripheral
在BLE中 ,Peripheral
通过广播的方式向Central
提供数据是主要方式。主要操作如下:
- 服务端 外围设备(
Peripheral
): 向外广播数据包(Advertising)形式的数据,比如设备名称,功能等! - 客户端 中心设备(
Central
) : 可以通过实现扫描和监听到广播数据包,并展示数据。
如图所示 :
(图片来源于网络)
例如 :
温度传感器设备会向外广播它获取到的温度信息
3. 外围设备(Peripheral
)数据如何构成
Peripheral
包含一个或者多个Service
(服务)以及有关其连接信号强度的有用信息 。
Service
:是指为了实现一个功能或者特性的数据采集和相关行为的集合,包含一个或多个特征(Characteristic
)。Characteristic
:它包括一个value
和0至多个对次value的描述(Descriptor
),知道更多数据细节。Descriptor
: 对Characteristic的描述,例如范围、计量单位等
如图所示:
三. Central 与 Peripheral 交互总结
在 Central
与 Perpheral
建立连接后,就可以发现(discoverServices
)Perpheral
提供的所有的Services
和Characteristic
。然后,Central
可以通过读写Service
中的Characteristic
的value
与Perpheral
进行交互。
以Android 开发举例 :
I . Central (中心设备)开发流程
建立中心角色
确定中心角色,客户端确立。扫描 外设
扫描设备:Android 5.0 以下使用BluetoothAdapter#startLeScan()
实现扫描,Android5.0及其以上,使用android.bluetooth.le
包下BluetoothLeScaner#startScan()
实现。建立连接
中心设备主要的类BluetoothGatt
核心对象,通过BlueToothDevice#connectGatt()
可以获取到操作对象获取services与characteristics
在连接设备的时候,会使用BluetoothGattCallback
进行一些数据返回和状态返回,当连接成功时,进行discoverservices
,比如@Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothGatt.STATE_CONNECTED) { gatt.discoverServices(); } @Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); //当调用 discoverServices的时候,会调用此方法 printServices(gatt);}
- 与外设设备交互和订阅
Characteristic
通知 断开连接
代码如下:public void disconnect() { if (mBluetoothGatt != null) { mBluetoothGatt.disconnect(); mBluetoothGatt.close(); }}
II . Peripheral(外围设备)开发流程
确定外围设备
初始化外围设备管理对象
Android 5.0 之前没有外围设备开发,目前使用BluetoothLeAdvertiser
进行外围设备管理;可以通过下面的方法获取BluetoothAdapter#getBluetoothLeAdvertiser()
可以获取到该对象。打开外围广播
根据SERIVE_UUID
,打开一个外围设备连接,通过下面类实现BluetoothLeAdvertiser#startAdvertising()
;获取外围设备
Server
通过BluetoothGattServer
进行订阅通知,读写Characteristic操作,通过下面方法获取。mBluetoothManager.openGattServer(mContext, bluetoothGattServerCallback)
四.参考文档
IOS Core Bluetooth 蓝牙通信
https://developer.android.google.cn/reference/android/bluetooth/le/AdvertiseData.html
- Android - BlueTooth BLE 之 Central 与 Peripheral 理解
- Core Bluetooth框架之一:Central与Peripheral
- Core Bluetooth框架之一:Central与Peripheral
- BLE工作模式: Central+Peripheral(Advertising+Connected)
- Android BLE开发: BLE Peripheral开发流程
- 【BLE】peripheral设备如何获取central设备的PublicAddr
- CC2540 Central与CC2540 Peripheral 密码配对
- Android - BlueTooth BLE 开篇
- android5.0(Lollipop) BLE Peripheral深入理解系统篇之初体验
- android5.0(Lollipop) BLE Peripheral深入理解系统篇之提高篇
- BLE之Central类介绍
- [Bluetooth]: android 平台上BLE连接流程之优化方案
- 【三】CC2541 Central 与 Peripheral 主从机实验 小结
- android蓝牙-isMultipleAdvertisementSupported(API21)判断支持不支持BLE Peripheral
- Android as Bluetooth Low Energy Peripheral (GATT server).
- How to Advertise Android as a Bluetooth LE Peripheral
- Android as Bluetooth Low Energy Peripheral (GATT server).
- Android BLE开发之Android手机与BLE终端通信
- angular......ngRepeat嵌套
- 入门训练 圆的面积
- swift3.0 coreData的使用-日记本demo
- C++中函数调用时的三种参数传递方式
- Windows远程连接Ubuntu (远程桌面和XDMCP)
- Android - BlueTooth BLE 之 Central 与 Peripheral 理解
- 51nod 1140 矩阵相乘结果的判断
- 关键字static/const的作用
- 机器学习之Validation(验证,模型选择)
- Objective-C 自定义UISlider滑杆 分段样式
- 推荐:iOS开发最佳实践总结
- 我的ionic配置
- 富文本框tinyeditor的用法
- Android ImageView的setImageLevel和level-list使用简介