BLE 开发指导
本文简要提供OpenHarmony的 ble 应用侧的开发指导。 工作步骤: 扫描 -->扫描到蓝牙设备 -->开始建立GATT连接 --> 进行通信 --> 通信结束 --> 断开连接 扫描 调用 ble 提供的 startBLEScan 方法扫描设备,在扫描的结果中,根据广播名字匹配自己需要发现的设备。 import bl
·
本文简要提供OpenHarmony的 ble 应用侧的开发指导。
工作步骤:
扫描 -->扫描到蓝牙设备 -->开始建立GATT连接 --> 进行通信 --> 通信结束 --> 断开连接
扫描
调用 ble 提供的 startBLEScan 方法扫描设备,在扫描的结果中,根据广播名字匹配自己需要发现的设备。
import ble from '@ohos.bluetooth.ble';
startBLEScan() {
ble.on('BLEDeviceFind', (data: Array<ble.ScanResult>) => {
for (let i = 0; i < data.length; i++) {
const scanResult: ble.ScanResult = data[i];
let exp: RegExp = new RegExp(BleScanRegStr);
// 匹配
if (exp.test(scanResult.deviceName)) {
this.scanResult = scanResult;
}
}
});
try {
ble.startBLEScan(null);
} catch (e) {
Logger.debug(BleConstants.TAG, `startBLEScan error ${JSON.stringify(e)}`);
}
}
停止扫描
import ble from '@ohos.bluetooth.ble';
stopBLEScan() {
ble.stopBLEScan();
}
连接
连接需要分为以下几步,特别注意:gatt 连接成功后,才能设置mtu,mtu 设置成功后,才能设置特征值。
- 创建GattClient
- 设置BLEMtuChange监听
- 设置BLEConnectionStateChange监听
- 设置BLECharacteristicChange监听
- 调用GattClient connect 接口
- 设置mtu
- 设置特征值
import ble from '@ohos.bluetooth.ble';
connectedClient: ble.GattClientDevice | undefined;
connectBle(scanResult: ble.ScanResult) {
this.connectedClient = ble.createGattClientDevice(scanResult.deviceId);
this.connectedClient?.on("BLEMtuChange", async (mtu: number) => {
Logger.debug(BleConstants.TAG, 'BLEMtuChange mtu:' + mtu);
try {
// mtu 设置成功设置特征值
await this.setCharacteristicChangeNotification();
} catch (error) {
}
})
this.connectedClient?.on("BLEConnectionStateChange", (state: ble.BLEConnectionChangeState) => {
if (state.state == constant.ProfileConnectionState.STATE_CONNECTED) {
// 连接成功,设置Mtu
this.connectedClient?.setBLEMtuSize(BleConstants.BLE_MTU_SIZE);
}
})
this.connectedClient?.on("BLECharacteristicChange", (characteristicChangeReq: ble.BLECharacteristic) => {
});
this.connectedClient.connect();
}
private async setCharacteristicChangeNotification() {
// 获取client 端的所有特征值
let services = await this.connectedClient?.getServices();
// 过滤自己需要的特征值
let readCharacteristic = services?.find(item => item.serviceUuid == BleConstants.GATT_SERVICE_UUID)
?.characteristics.find(item => item.characteristicUuid == BleConstants.GATT_CHARACTERISTIC_READ_UUID);
Logger.debug(BleConstants.TAG, `readCharacteristic :${JSON.stringify(readCharacteristic)}`);
if (readCharacteristic == undefined) {
return Promise.reject();
}
return await this.connectedClient?.setCharacteristicChangeIndication(readCharacteristic, true);
}
消息发送
sendData(ab: ArrayBuffer) {
// serviceUuid 特征值一定是双方协议中的
const descriptors: Array<ble.BLEDescriptor> = [];
let characteristic: ble.BLECharacteristic = {
serviceUuid: BleConstants.GATT_SERVICE_UUID,
characteristicUuid: BleConstants.GATT_CHARACTERISTIC_WRITE_UUID,
characteristicValue: ab,
descriptors: descriptors
};
return this.connectedClient?.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE)
}
消息接受
this.connectedClient?.on("BLECharacteristicChange", (characteristicChangeReq: ble.BLECharacteristic) => {
// 过滤双方协议中的的serviceUuid
if (characteristicChangeReq.serviceUuid == BleConstants.GATT_SERVICE_UUID && characteristicChangeReq.characteristicUuid == BleConstants.GATT_CHARACTERISTIC_READ_UUID) {
// valeu 就是对端给你发送的消息
let value = characteristicChangeReq.characteristicValue;
}
});
断开连接
stopConnect() {
this.connectedClient?.disconnect()
this.connectedClient?.close();
}
更多推荐
已为社区贡献13条内容
所有评论(0)