HarmonyOS-鸿蒙app开发 —基于java网络与连接_BLE外围设备数据管理

HarmonyOS-鸿蒙app开发 —基于java网络与连接_BLE外围设备数据管理

场景介绍

BLE外围设备作为服务端,可以接收来自中心设备(客户端)的GATT连接请求,应答来自中心设备的特征值内容读取和写入请求,并向中心设备提供数据,从而实现信息交互和消息同步。同时外围设备还可以主动向中心设备发送数据。

接口说明

低功耗蓝牙外围设备操作类BlePeripheralManager的接口说明如下。

表1 外围设备管理API介绍

接口名

功能描述

BlePeripheralManager(Context context, BlePeripheralManagerCallback callback, int transport)

获取外围设备管理回调对象。

getServices()

获取外围设备的所有服务。

addService(GattService service)

将GATT服务加入服务端。

cancelConnection(BlePeripheralDevice device)

取消与中心设备的GATT连接。

clearServices()

删除所有的GATT服务。

close()

关闭GATT服务端。

getDevicesByStates(int[] states)

通过状态获取连接的中心设备列表。

notifyCharacteristicChanged(BlePeripheralDevice device, GattCharacteristic characteristic, boolean confirm)

通知中心设备特征值出现变化。

removeGattService(GattService service)

移除特定的服务。

sendResponse(BlePeripheralDevice device, int requestId, int status, int offset, byte[] value)

发送回复给中心设备。

低功耗蓝牙外围设备管理回调类BlePeripheralManagerCallback的接口说明如下。

表2 外围设备管理回调API介绍

接口名

功能描述

receiveCharacteristicReadEvent(BlePeripheralDevice device, int requestId, int offset, GattCharacteristic characteristic)

收到中心设备对特征值的读取请求回调。

receiveCharacteristicWriteEvent(BlePeripheralDevice device, int requestId, GattCharacteristic characteristic, boolean isPrep, boolean needRsp, int offset, byte[] value)

收到中心设备对特征值的写入请求。

connectionStateChangeEvent(BlePeripheralDevice device, int interval, int latency, int timeout, int status)

中心设备连接事件回调。

receiveDescriptorReadEvent(BlePeripheralDevice device, int transId, int offset, GattDescriptor descriptor)

收到中心设备对描述值的读取请求回调。

receiveDescriptorWriteRequestEvent(BlePeripheralDevice device, int transId, GattDescriptor descriptor, boolean isPrep, boolean needRsp, int offset, byte[] value)

收到中心设备对描述值的写入请求回调。

executeWriteEvent(BlePeripheralDevice device, int requestId, boolean execute)

向中心设备执行写入操作的回调。

mtuUpdateEvent(BlePeripheralDevice device, int mtu)

中心设备MTU值变化的回调。

notificationSentEvent(BlePeripheralDevice device, int status)

向中心设备发送通知的回调。

serviceAddedEvent(int status, GattService service)

向外围设备添加服务结果回调。

开发步骤

  1. 调用BlePeripheralManager(Context context, BlePeripheralManagerCallback callback, int transport)接口创建外围设备服务端并开启服务。
  2. 调用GattService(UUID uuid, boolean isPrimary)接口创建服务对象,向外围设备添加服务。

从回调接口onCharacteristicWriteRequest中获取中心设备发送来的消息,调用notifyCharacteristicChanged接口向中心设备发送通知。

private BlePeripheralDevice blePeripheralDevice;

private MyBlePeripheralManagerCallback peripheralManagerCallback = new MyBlePeripheralManagerCallback();

// 创建外围设备服务端并开启服务

private BlePeripheralManager peripheralManager = new BlePeripheralManager(this, peripheralManagerCallback, 0);

// 创建服务对象,向外围设备添加服务

private GattService service = new GattService(UUID.fromString(Service_UUID), true);

private GattCharacteristic writeCharacteristic = new GattCharacteristic(Character_UUID,

GattCharacteristic.PROPERTY_WRITE, GattCharacteristic.PROPERTY_WRITE);

service.addCharacteristic(writeCharacteristic);

peripheralManager.addServerService(service);

// 向中心设备发送通知

writeCharacteristic.setValue(data);

peripheralManager.notifyCharacteristicChanged(blePeripheralDevice, writeCharacteristic, false);

 

// 外围设备管理回调

public class MyBlePeripheralManagerCallback extends BlePeripheralManagerCallback {

// 中心设备向外围设备写入数据回调

@Override

public void receiveCharacteristicWriteEvent(BlePeripheralDevice device, int requestId,

GattCharacteristic characteristic, boolean isPrep, boolean needRsp, int offset, byte[] value){

// 中心设备写入外围设备的数据为value,对数据进行处理

}

// 外围设备连接状态变化回调

@Override

public void connectionStateChangeEvent(BlePeripheralDevice device, int interval, int latency, int timeout, int status){

if (status == ProfileBase.STATE_CONNECTED) {

// 中心设备连接服务端成功

blePeripheralDevice = device;

}

}

// 向中心设备发送通知回调

@Override

public void notificationSentEvent(BlePeripheralDevice device, int status){

if (status == BlePeripheralDevice.OPERATION_SUCC) {

// 向对中心设备发送通知成功回调

}

}

}

🚀 如未找到文章请搜索栏搜素 | Ctrl+D收藏本站 | 联系邮箱:15810050733@qq.com