Android开发 - BluetoothGattCallback 类处理蓝牙 (BLE) 设备的连接和通信解析

BluetoothGattCallback 是什么

  • BluetoothGattCallback 是一个抽象类,用于接收 BLE 设备的各种回调事件。这些事件包括连接状态的变化服务的发现特性的读取和写入

BluetoothGattCallback 的主要方法

  • onConnectionStateChange(BluetoothGatt gatt, int status, int newState):当设备的连接状态发生变化时调用。比如:设备连接成功或断开连接

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        // 检查连接状态是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 检查新的连接状态是否为已连接
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.i("BluetoothGattCallback", "Connected to GATT server.");
                // 连接成功,开始发现服务
                gatt.discoverServices();
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                Log.i("BluetoothGattCallback", "Disconnected from GATT server.");
                // 连接断开
            }
        } else {
            // 连接状态变化失败,记录警告
            Log.w("BluetoothGattCallback", "Connection state change failed: " + status);
        }
    }
    
    • 参数解析
      • gatt:代表当前的 GATT 连接
      • status:连接的状态码(通常是 BluetoothGatt.GATT_SUCCESS 表示成功)
      • newState新的连接状态(BluetoothProfile.STATE_CONNECTEDBluetoothProfile.STATE_DISCONNECTED
  • onServicesDiscovered(BluetoothGatt gatt, int status):当 BLE 设备的服务被发现时调用。这是在连接成功后进行服务发现时触发的回调

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        // 检查服务发现是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.i("BluetoothGattCallback", "Services discovered.");
            // 遍历发现的服务
            for (BluetoothGattService service : gatt.getServices()) {
                // 打印每个服务的 UUID
                Log.i("BluetoothGattCallback", "Service UUID: " + service.getUuid());
            }
        } else {
            // 服务发现失败,记录警告
            Log.w("BluetoothGattCallback", "Service discovery failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • status:服务发现的状态码(通常是 BluetoothGatt.GATT_SUCCESS 表示成功)

  • onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status):当读取特性值的操作完成时调用。此时可以获取特性的值

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        // 检查读取操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 获取读取到的特性值
            byte[] value = characteristic.getValue();
            // 打印特性值
            Log.i("BluetoothGattCallback", "Characteristic read: " + Arrays.toString(value));
        } else {
            // 读取操作失败,记录警告
            Log.w("BluetoothGattCallback", "Characteristic read failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • characteristic被读取的特性对象

      • status:读取操作的状态码

  • onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status):当写入特性值的操作完成时调用

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        // 检查写入操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 写入成功,记录信息
            Log.i("BluetoothGattCallback", "Characteristic written successfully.");
        } else {
            // 写入操作失败,记录警告
            Log.w("BluetoothGattCallback", "Characteristic write failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • characteristic被写入的特性对象

      • status:写入操作的状态码

  • onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic):当特性值发生变化时调用。这通常是因为设备向手机推送了新的数据

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        // 设备推送的数据发生变化
        byte[] value = characteristic.getValue();
        // 打印变化后的特性值
        Log.i("BluetoothGattCallback", "Characteristic changed: " + Arrays.toString(value));
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • characteristic发生变化的特性对象

  • onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status):当读取描述符的操作完成时调用

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        // 检查读取操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 获取读取到的描述符值
            byte[] value = descriptor.getValue();
            // 打印描述符值
            Log.i("BluetoothGattCallback", "Descriptor read: " + Arrays.toString(value));
        } else {
            // 读取操作失败,记录警告
            Log.w("BluetoothGattCallback", "Descriptor read failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • descriptor被读取的描述符对象

      • status读取操作的状态码

  • onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status):当写入描述符的操作完成时调用

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        // 检查写入操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 写入成功,记录信息
            Log.i("BluetoothGattCallback", "Descriptor written successfully.");
        } else {
            // 写入操作失败,记录警告
            Log.w("BluetoothGattCallback", "Descriptor write failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • descriptor被写入的描述符对象

      • status写入操作的状态码

  • onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status):当读取远程设备的信号强度指示 (RSSI) 完成时调用

    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        // 检查读取操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 打印远程设备的信号强度
            Log.i("BluetoothGattCallback", "RSSI read: " + rssi);
        } else {
            // 读取操作失败,记录警告
            Log.w("BluetoothGattCallback", "RSSI read failed: " + status);
        }
    }
    
    • gatt:代表当前的 GATT 连接

    • rssi:设备的信号强度

    • status读取操作的状态码

总结

  • BluetoothGattCallback 类的主要作用是监听和处理与 BLE 设备交互的各种事件。通过重写这些方法,你可以处理设备的连接读取和写入数据接收数据变化等操作,从而实现与 BLE 设备的有效通信
posted @ 2024-08-23 16:21  阿俊学JAVA  阅读(39)  评论(0编辑  收藏  举报