场景介绍
BLE外围设备和中心设备建立GATT连接,通过该连接中心设备可以获取外围设备支持的Service、Characteristic、Descriptor、RSSI等数据。同时,中心设备可以向外围设备进行数据请求,并向外围设备写入Characteristic、Descriptor等特征值数据。
接口说明
低功耗蓝牙外围设备操作类BlePeripheralDevice的接口说明如下。
| 
 接口名  | 
 功能描述  | 
|---|---|
| 
 connect(boolean isAutoConnect, BlePeripheralCallback callback)  | 
 重新连接GATT外围设备,isAutoConnect表示是否自动进行连接。  | 
| 
 discoverServices()  | 
 搜索外围设备支持的服务,特征和描述。  | 
| 
 getServices()  | 
 获取外围设备支持的所有GATT服务。  | 
| 
 getService(UUID uuid)  | 
 根据UUID获取外围设备支持的某个GATT服务。  | 
| 
 disconnect()  | 
 与外围设备断开BLE连接。  | 
| 
 close()  | 
 关闭蓝牙GATT客户端。  | 
| 
 readCharacteristic(GattCharacteristic characteristic)  | 
 读取外围设备GATT特征。  | 
| 
 writeCharacteristic(GattCharacteristic characteristic)  | 
 写指定外围设备的GATT特征值。  | 
| 
 setNotifyCharacteristic(GattCharacteristic characteristic, boolean enable)  | 
 设置指定GATT特征通知的使能/去使能。  | 
| 
 readDescriptor (GattDescriptor descriptor)  | 
 读取外围设备GATT描述值。  | 
| 
 writeDescriptor(GattDescriptor descriptor)  | 
 写指定外围设备的GATT描述值。  | 
| 
 readRemoteRssiValue()  | 
 读取已连接外围设备的RSSI。  | 
| 
 requestBleConnectionPriority(int connPriority)  | 
 请求链接参数更新。  | 
| 
 requestBleMtuSize(int mtu)  | 
 请求用于给定连接的MTU大小。  | 
低功耗蓝牙外围设备操作回调类BlePeripheralCallback的接口说明如下。
| 
 接口名  | 
 功能描述  | 
|---|---|
| 
 servicesDiscoveredEvent(int status)  | 
 外围设备服务发生更新触发的回调。  | 
| 
 connectionStateChangeEvent(int connectionState)  | 
 外围设备GATT连接状态发生变化时的回调。  | 
| 
 characteristicReadEvent(GattCharacteristic characteristic, int ret)  | 
 GATT特征值读操作回调。  | 
| 
 characteristicWriteEvent(GattCharacteristic characteristic, int ret)  | 
 GATT特征值写操作回调。  | 
| 
 characteristicChangedEvent(GattCharacteristic characteristic)  | 
 外围设备特征通知触发的回调。  | 
| 
 descriptorReadEvent(GattDescriptor descriptor, int ret)  | 
 GATT描述值读操作回调。  | 
| 
 descriptorWriteEvent(GattDescriptor descriptor, int ret)  | 
 GATT描述值写操作回调。  | 
| 
 readRemoteRssiEvent(int rssi, int ret)  | 
 外围设备发来读取RSSI的回调。  | 
| 
 mtuUpdateEvent(int mtu, int ret)  | 
 GATT设备链接的MTU变化通知的回调。  | 
开发步骤
- 调用startScan()接口启动BLE扫描来获取外围设备。
 - 获取到外围设备后,调用connect(boolean isAutoConnect, BlePeripheraCallback callback)建立与外围BLE设备的GATT连接,boolean参数isAutoConnect用于设置是否允许设备在可发现距离内自动建立GATT连接。
 - 启动GATT连接后,会触发connectionStateChangeEvent(int connectionState)回调,根据回调结果判断是否连接GATT成功。
 - 在GATT连接成功时,中心设备可以调用discoverServices()接口,获取外围设备支持的Services、Characteristics等特征值,在回调servicesDiscoveredEvent(int status)中获取外围设备支持的服务和特征值,并根据UUID判断是什么服务。
 
根据获取到的服务和特征值,调用read和write方法可以读取或者写入对应特征值数据。
private List<GattService> services;
// 创建扫描过滤器然后开始扫描
List<BleScanFilter> filters = new ArrayList<BleScanFilter>();
centralManager.startScan(filters);
// 与外围设备建立连接,允许自动回连,连接会触发connectionStateChangeEvent回调
private BlePeripheralDevice peripheralDevice = BlePeripheralDevice.createInstance(address); // address为远端设备地址
MyBlePeripheralCallback callback = new MyBlePeripheralCallback();
peripheralDevice.connect(true, callback);
// 连接后读取外围设备RSSI值,获取后触发readRemoteRssiEvent()回调
peripheralDevice.readRemoteRssiValue();
// 实现外围设备操作回调
private class MyBlePeripheralCallback extends BlePeripheralCallback {
public void connectionStateChangeEvent(int connectionState) { // GATT连接状态变化回调
if (connectionState == ProfileBase.STATE_CONNECTED){
peripheralDevice.discoverServices(); // 连接成功获取外围设备的Service
}
}
public void servicesDiscoveredEvent(int status) { // 获取外围设备Service的回调
if (status == BlePeripheralDevice.OPERATION_SUCC){
services = peripheralDevice.getServices(); // 获取Service成功后获服务列表
for (GattService service : services){
// 对每个服务进行相应操作
}
}
}
public void characteristicChangedEvent(GattCharacteristic charecteristic) { // 外围设备主动向中心设备发送特征值通知时触发回调
// 根据通知的charecteristic获取特征值携带的数据
}
public void characteristicWriteEvent(GattCharacteristic charecteristic, int ret) {
if (ret == BlePeripheralDevice.OPERATION_SUCC){
// 向外围设备写特征值数据成功后的操作
}
}
public void characteristicReadEvent(GattCharacteristic charecteristic, int ret) {
if (ret == BlePeripheralDevice.OPERATION_SUCC){
// 向外围设备读特征值数据成功后的操作
}
}
public void descriptorReadEvent(GattDescriptor descriptor, int ret) {
// 向外围设备读描述值数据成功后的操作
}
public void descriptorWriteEvent(GattDescriptor descriptor, int ret) {
// 向外围设备写描述值数据成功后的操作
}
public void readRemoteRssiEvent(int rssi, int ret) {
if (ret == BlePeripheralDevice.OPERATION_SUCC){
// 读取外围设备RSSI值成功后的操作,对端RSSI值为rssi
}
}
}