java - onDescriptorWrite() 从未在 BluetoothGattCallback 中调用

标签 java android android-studio bluetooth-lowenergy

我想订阅多个特征。目前我正在通过在调用每个 writeDescriptor(descriptor) 之间使用计时器来做到这一点(大部分成功)。我想改为使用 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() 被调用?

最佳答案

所以我想通了这个问题。回调函数没有被调用,因为我有一个包含所有具有通知/指示属性的特征的列表。但是,在写入描述符之前,我会检查某些 UUID。好吧,我没有检查其中一个特性,因为我没有使用它,而它恰好是列表中的第一个!所以,我什至都不会写入要调用的回调的描述符。

关于java - onDescriptorWrite() 从未在 BluetoothGattCallback 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49641272/

相关文章:

android - com.google.android.c2dm.intent.RECEIVE 还在使用吗?

java - 以编程方式设置 textView 颜色

java - 按钮图标问题,Java 8-Queens 谜题

Java Swing - 在子菜单中选择项目时增加宽大度

android - Android JavascriptInterface 中的 Javascript 命名空间

c# - 如何修复 UNITYTLS_X509VERIFY_NOT_DONE

android-studio - 无法从 android sdk 找到 zipalign.exe

android-studio - Flutter 未检测到 Android Studio

java - Android Multi-Touch 帮助 - 一次拖动两个对象

java - 将其他两个之间的单词作为正则表达式排除