TextView 中的显示特性

问题描述 投票:0回答:1

我是开发应用程序的新手,尤其是 BLE。我正在尝试从我的 BLE 设备的 TextViews 中显示三个特征的实时数据,该设备具有具有三个特征的自定义服务。只有第一个特征显示值,而其他两个不显示。这是我在 BluetoothGattCallback 下的 onCharacteristicsChange:

       public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            UUID characteristicUUID = characteristic.getUuid();
            byte[] data = characteristic.getValue();

            String value = new String(data);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (characteristicUUID.equals(UUID.fromString("082b9438-e83c-11e8-9f32-f2801f1b9fd1"))) {
                        mDataTextView1.setText(value);
                    } else if (characteristicUUID.equals(UUID.fromString("082b9622-e83c-11e8-9f32-f2801f1b9fd1"))) {
                        mDataTextView2.setText(value);
                    } else if (characteristicUUID.equals(UUID.fromString("082b976c-e83c-11e8-9f32-f2801f1b9fd1"))) {
                        mDataTextView3.setText(value);
                    }
                }
            });
        }

这里还有我的 onServicesDiscovered under BluetoothGattCallback:

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattCharacteristic characteristic1 = gatt.getService(UUID.fromString("082b91ae-e83c-11e8-9f32-f2801f1b9fd1"))
                        .getCharacteristic(UUID.fromString("082b9438-e83c-11e8-9f32-f2801f1b9fd1"));
                BluetoothGattCharacteristic characteristic2 = gatt.getService(UUID.fromString("082b91ae-e83c-11e8-9f32-f2801f1b9fd1"))
                        .getCharacteristic(UUID.fromString("082b9622-e83c-11e8-9f32-f2801f1b9fd1"));
                BluetoothGattCharacteristic characteristic3 = gatt.getService(UUID.fromString("082b91ae-e83c-11e8-9f32-f2801f1b9fd1"))
                        .getCharacteristic(UUID.fromString("082b976c-e83c-11e8-9f32-f2801f1b9fd1"));

                if (characteristic1 != null && characteristic2 != null && characteristic3 != null) {
                    gatt.setCharacteristicNotification(characteristic1, true);
                    gatt.setCharacteristicNotification(characteristic2, true);
                    gatt.setCharacteristicNotification(characteristic3, true);

                    BluetoothGattDescriptor descriptor1 = characteristic1.getDescriptor(
                            UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
                    BluetoothGattDescriptor descriptor2 = characteristic2.getDescriptor(
                            UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
                    BluetoothGattDescriptor descriptor3 = characteristic3.getDescriptor(
                            UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));

                    if (descriptor1 != null && descriptor2 != null && descriptor3 != null) {
                        descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        descriptor2.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        descriptor3.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

                        gatt.writeDescriptor(descriptor1);
                        gatt.writeDescriptor(descriptor2);
                        gatt.writeDescriptor(descriptor3);
                    }
                }
            }
        }

如何显示这两个特征或者我的代码哪里错了?谢谢,对不起我的英语。

java android bluetooth bluetooth-lowenergy
1个回答
0
投票

您对 writeDescriptor 的调用不能像那样按顺序提交,因为每个 BluetoothGatt 对象一次只允许一个 GATT 操作。请等待 onDescriptorWrite 回调再写下一个。

© www.soinside.com 2019 - 2024. All rights reserved.