有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

BluetoothGattCallback中从未调用java onDescriptorWrite()

我想订阅多个特性。目前,我通过在调用每个writeDescriptor(描述符)之间使用计时器来完成这项工作(大部分是成功的)。我想通过使用onDescriptorWrite()回调来实现这一点,但它从未被调用

我有以下回电

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());


        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);
        Log.d("onDescWrite", "yes");
        if(characteristics_with_notifications_or_indications.size() > 0) {
            characteristics_with_notifications_or_indications.remove(0);
            subscribeToCharacteristics();
        }
    }
};

它在我的连接函数中初始化

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

这是我写的描述符

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
            UUID.fromString(CESGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));

descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

我想知道何时通过回调完成writeDescriptor(),以便订阅另一个特性。然而,即使我成功订阅了一个特性并接收到有效数据,onDescriptorWrite()仍然不会被调用。如何调用onDescriptorWrite()


共 (1) 个答案

  1. # 1 楼答案

    所以我解决了问题。回调函数没有被调用,因为我有一个包含所有具有notify/indicate属性的特征的列表。然而,在写入描述符之前,我会检查某些UUID。嗯,我没有检查其中一个特性,因为我没有使用它,而它恰好是列表上的第一个特性之一!因此,我甚至不会为调用回调而写入描述符