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

一.前言

Andorid 5.0 之前是无法进行 外围设备开发的,在Android 5.0 API 21 android.bluetooth.le包下,新增加 Scaner相关类和 Advertiser 相关类。目前最后使用Scanner相关类实现蓝牙扫描。这段时间对蓝牙的学习与理解,对中心设备与周边设备做下面总结。

先看下 android.bluetooth.le 包下内容:

0818b9ca8b590ca3270a3433284dd417.png

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上 需要以用户友好的方式显示用户的心跳信息。

(来源于网络)

0818b9ca8b590ca3270a3433284dd417.png

2.Central 发现并连接广播中的 Peripheral

在BLE中 ,Peripheral 通过广播的方式向Central提供数据是主要方式。主要操作如下:

服务端 外围设备(Peripheral): 向外广播数据包(Advertising)形式的数据,比如设备名称,功能等!

客户端 中心设备(Central ) : 可以通过实现扫描和监听到广播数据包,并展示数据。

如图所示 :

(图片来源于网络)

0818b9ca8b590ca3270a3433284dd417.png

例如 :

温度传感器设备会向外广播它获取到的温度信息

3. 外围设备(Peripheral )数据如何构成

Peripheral 包含一个或者多个Service(服务)以及有关其连接信号强度的有用信息 。

Service :是指为了实现一个功能或者特性的数据采集和相关行为的集合,包含一个或多个特征(Characteristic)。

Characteristic :它包括一个value和0至多个对次value的描述(Descriptor),知道更多数据细节。

Descriptor : 对Characteristic的描述,例如范围、计量单位等

如图所示:

0818b9ca8b590ca3270a3433284dd417.png

三. 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 ,比如 @Override

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if (newState == BluetoothGatt.STATE_CONNECTED) {

gatt.discoverServices();

}

@Override

public 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)

四.参考文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值