BLE的回调函数onCharacteristicChanged没有被调用
创始人
2024-12-22 13:30:16
0

我可以为您提供一个示例代码,解决BLE的回调函数onCharacteristicChanged没有被调用的问题。

在使用BLE时,您需要确保正确设置了特征值的通知属性,并正确注册了相应的回调函数。下面是一个示例代码,可以帮助您解决该问题:

// 导入必要的类
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothProfile;

// 创建一个BluetoothGattCallback的子类
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    // 当连接状态发生改变时被调用
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            // 连接成功后开始发现服务
            gatt.discoverServices();
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            // 连接断开后的处理
        }
    }

    // 当服务被发现后被调用
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 获取特征值
            BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid)
                    .getCharacteristic(characteristicUuid);
            
            // 设置特征值的通知属性
            gatt.setCharacteristicNotification(characteristic, true);
            
            // 获取特征值的描述符
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
            
            // 设置描述符的值
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            
            // 写入描述符
            gatt.writeDescriptor(descriptor);
        }
    }

    // 当描述符写入完成后被调用
    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 描述符写入成功后,可以开始监听特征值变化
            gatt.setCharacteristicNotification(characteristic, true);
        }
    }

    // 当特征值改变时被调用
    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        // 处理特征值变化的数据
    }
};

在上面的代码中,您需要将serviceUuidcharacteristicUuiddescriptorUuid替换为实际的UUID值。另外,您还需要确保已经建立了BLE连接,并正确调用了BluetoothGatt.connectGatt()方法来创建BluetoothGatt对象,并将上面的mGattCallback设置给它。

这样,当特征值发生变化时,onCharacteristicChanged()函数将会被调用,并且您可以在其中处理相关的数据。

相关内容

热门资讯

第7分钟插件!微乐广西小程序脚... 第7分钟插件!微乐广西小程序脚本,微乐小程序免费黑科技,项目教程(有挂下载)-哔哩哔哩微乐小程序免费...
第四分钟插件!微信小程序微乐房... 第四分钟插件!微信小程序微乐房间怎么开挂,微乐小程序免费黑科技,学习教程(有挂下载)-哔哩哔哩1.微...
第十分钟安装!微乐游戏小程序脚... 您好,微乐小程序免费黑科技这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
六分钟辅助!微乐自建房辅助工具... 六分钟辅助!微乐自建房辅助工具别人怎么看,微乐小程序免费黑科技,烘培教程(有挂辅助器)-哔哩哔哩一、...
第四分钟插件!微乐自建房辅助工... 您好,微乐自建房辅助工具2025在哪这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275...
第九分钟安装!微乐小程序辅助,... 第九分钟安装!微乐小程序辅助,微乐小程序免费黑科技,法门教程(有挂工具)-哔哩哔哩1、进入到微乐小程...
第三分钟安装!微乐游戏辅助脚本... 第三分钟安装!微乐游戏辅助脚本安装,微乐小程序免费黑科技,模板教程(有挂修改器)-哔哩哔哩1)微乐游...
第三分钟插件!微乐自建房辅助工... 第三分钟插件!微乐自建房辅助工具安全吗,微乐小程序黑科技,操作教程(有挂脚本)-哔哩哔哩1、每一步都...
六分钟脚本!微乐家乡官方app... 六分钟脚本!微乐家乡官方app下载,微乐小程序免费黑科技,诀窍教程(有挂app)-哔哩哔哩1、下载好...
第5分钟辅助!微信小程序微乐辅... 第5分钟辅助!微信小程序微乐辅助器真吗,微乐小程序黑科技,办法教程(有挂软件)-哔哩哔哩1、点击下载...