本文讲述的是在OpenHarmony轻量设备(L0设备)上进行BLE相关应用程序的开发。应用所在设备作为Gatt Client与其他Gatt Server设备进行连接和通信。

本文重点讲述BLE设备的连接。在完成BLE设备扫描,并获取到目标设备的mac地址之后,可以开始进行BLE设备的连接。

BLE设备连接的接口

在进行设备连接之前,需要先创建GattClientDevice类型,此类型包含了GATT client的所需要的操作。

// 创建GattClientDevice实例
createGattClientDevice(deviceId: string): GattClientDevice
​
// 以下为GattClientDevice类的方法
// client端发起连接
GattClientDevice.connect(): number
// client端断开连接
GattClientDevice.disconnect(): number
// client端订阅BLE设备连接状态变化的事件
GattClientDevice.on(type: 'BLEConnectionStateChange', callback: Callback<number, BLEConnectionChangeState>): void
// client端取消订阅BLE设备连接状态变化的事件。
GattClientDevice.off(type: 'BLEConnectionStateChange', callback?: Callback<number, BLEConnectionChangeState>): void
// 关闭Gatt client客户端功能,接口调用后GattClientDevice实例将不能再使用
GattClientDevice.close():number
详细的BLE设备连接接口说明请参考 蓝牙ble模块

BLE设备连接接口的使用

以下为BLE设备连接的示例代码,通过按钮触发BLE设备的连接和断开连接操作。

1、先根据扫描结果得到要连接的对端设备的mac,创建GattClientDevice对象实例。

2、订阅BLE设备连接状态变化事件。在设备连接后,每次BLE设备连接状态发生变化时,都会调用事件的回调函数。应用程序由此可以获取到BLE设备实时的连接状态,并进行相应的处理。

3、当用户点击"连接"按钮,client端调用GattClientDevice的connect接口开始连接。

需要注意的是,connect接口调用成功后BLE不一定就能够连接成功。在应用实际测试中,与对端设备的BLE连接状态可能出现先为connect,然后立即变化为disconnect状态。这个可能与对端设备自己的连接策略有关。

因此BLE连接需要多次调用connect接口,并在BLE设备连接状态变化事件的回调函数中确定当前BLE链路的实际状态。

4、当用户点击"断开连接"按钮,client端调用GattClientDevice的disconnect接口断开连接。

5、当用户退出本页面时,先断开BLE连接,取消订阅BLE设备连接状态变化事件,然后关闭GATT client。

// connect.js
import ble from '@ohos.bluetooth.ble';

let bleData = {
    gattClient: null,
    bleConnected: false, // 当前BLE设备是否为连接状态
};

function onDeviceConnect()
{
    console.debug('onDeviceConnect calling');
}

function onDeviceDisconnect()
{
    console.debug('onDeviceDisconnect calling');
}

// 订阅BLE设备连接状态变化事件的回调函数
function onGattConnStateChange(code, connChangeState) 
{
    console.debug(`onGattConnStateChange return code ${code} state ${connChangeState.state}`);
    if (code != 0) {
        return;
    }

    if (connChangeState.state === 2) {
        console.debug('ble state change: connect');
        bleData.bleConnected = true;
        // 设备连接成功后,开始连接成功后的业务处理
        onDeviceConnect();
    }
    if (connChangeState.state === 0) {
        console.debug('ble state change: disconnect');
        bleData.bleConnected = false;
        // 设备断连后,上层业务进行相应的处理
        onDeviceDisconnect();
    }
}
  
export default {
    data: {
        remoteMac: '00:ac:13:fe:65:41', // 保存扫描阶段选择要连接的对端设备mac
    },
    
    // 用户操作开始BLE连接
    onConnectClick() {
        if (bleData.bleConnected) {
            console.debug('ble state is connected!');           
            return;
        }
        
        if (bleData.gattClient == null) {
            bleData.gattClient = ble.createGattClientDevice(this.remoteMac);
        }
        if (bleData.gattClient == null) {
            console.error('ble gatt client device is null');            
            return;
        }
        
        bleData.gattClient.on('BLEConnectionStateChange', onGattConnStateChange);
        let ret = bleData.gattClient.connect();
        if (ret !== 0) {
            console.error(`failed to connect gatt server, ret=${ret}`);
        }        
    },
    
    // 用户操作断开BLE连接
    onDisconnectClick() {
        if (!bleData.bleConnected) {
            console.warn('ble state is disconnected!');           
            return;
        }
        if (bleData.gattClient == null) {
            console.error('gatt client device is null');            
            return;
        }
        
        let ret = bleData.gattClient.disconnect();
        if (ret != 0) {
            console.error(`failed to disconnect gatt client, ret=${ret}`);
        }
    },
    
    onDestroy() {
        if (bleData.gattClient == null) {
            return;
        }        
        if (bleData.bleConnected) {
            bleData.gattClient.disconnect();
        }
        bleData.gattClient.off('BLEConnectionStateChange');
        bleData.gattClient.close();
        bleData.gattClient = null;
    },
}

 

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐