一、BluetoothGatt介绍
蓝牙分为经典蓝牙和低功耗蓝牙(BLE),我们常用的蓝牙遥控器就是低功耗蓝牙。
低功耗蓝牙(BLE)连接都是建立在 GATT (Generic Attribute Profile) 协议之上。
GATT全称Generic Attribute Profile(直译即:通用属性协议),是一个在蓝牙连接之上的发送和接收较短的数据段的通用规范,这些很短的数据段被称为属性(Attribute)。
BLE架构
如下为BLE的架构图:
物理层:定义与无线电(RF)有关的所有蓝牙技术方面,包括调制方案、频段、信道使用、发射器和接收器特性等。定义了几种不同的物理层参数组合,并称为PHY。
链接层:定义空中接口数据包格式、比特流处理过程(如检错)、状态机和用于无线通信和链路控制的协议等。定义使用底层无线电进行无连接、连接导向和等时通信的几种不同方式,称为逻辑传输。
等时自适应层(ISOAL):允许使用等时通信的设备使用不同的帧持续时间。执行成帧PDU的分段和重组,或未成帧PDU的分割和重新组合。
主机控制接口(HCI):为主机组件和控制器之间的命令和数据双向通信提供明确定义的功能接口。由数种物理传输实现之一提供支持。
逻辑链路控制和适应协议(L2CAP):在主机内充当协议复用器,确保协议由适当的主机组件服务。在下层和上层L2CAP之间执行PDU/SDU的分割和重组。
安全管理协议(SMP):用于执行诸如配对等安全过程期间的协议。
属性协议(ATT):由ATT客户端和ATT服务器使用的协议,允许在服务器的属性表中发现和使用数据。
通用属性配置文件(GATT):根据属性表中的底层属性定义高级数据类型,称为服务、特征和描述符。定义了使用ATT与属性表进行交互的更高级别程序。
通用接入配置文件(GAP):定义了非连接状态下可使用的操作模式和过程,例如如何使用广告进行无连接通信以及如何执行设备发现。定义了安全级别和模式。定义了一些用户界面标准。
二、BluetoothGatt相关接口
IBluetoothGatt
API for interacting with BLE / GATT
用于与 BLE/GATT 交互的 API
IBluetoothGatt代码位于:
packages/modules/Bluetooth/system/binder/android/bluetooth/IBluetoothGatt.aidl
IBluetoothGatt的定义:
oneway interface IBluetoothGatt {}
IBluetoothGatt API:
void getDevicesMatchingConnectionStates(in int[] states, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void registerScanner(in IScannerCallback callback, in WorkSource workSource, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void unregisterScanner(in int scannerId, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void startScan(in int scannerId, in ScanSettings settings, in List<ScanFilter> filters, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void startScanForIntent(in PendingIntent intent, in ScanSettings settings, in List<ScanFilter> filters, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void stopScanForIntent(in PendingIntent intent, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void stopScan(in int scannerId, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void flushPendingBatchResults(in int scannerId, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void startAdvertisingSet(in AdvertisingSetParameters parameters, in AdvertiseData advertiseData,in AdvertiseData scanResponse, in PeriodicAdvertisingParameters periodicParameters, in AdvertiseData periodicData, in int duration, in int maxExtAdvEvents, in IAdvertisingSetCallback callback, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void stopAdvertisingSet(in IAdvertisingSetCallback callback, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void getOwnAddress(in int advertiserId, in AttributionSource attributionSource, in SynchronousResultReceiver receiver);
void enableAdvertisingSet(in int advertiserId, in boolean enable, in int duration, in int maxExtAdvEvents, in AttributionSource attr