首页 > 解决方案 > Gatt 描述符写作只返回一次成功

问题描述

在 Android Bluetooth Low energy 中,Gatt Descriptor 写入仅返回一次成功。

我有一个具有 4 个服务的外围设备,分别具有 1、3、2、4 个特性,其中一些是可通知的,有些是可指示的。

我编写了下面的代码以通过编写描述符来启用通知,但它只在编写描述符(ABC 行)一次时返回成功,而对于其余情况,它返回 false。

更多关于我的 gatt 写入特性总是返回错误。代码有什么问题

            for(BluetoothGattService service : services) {

                            for(BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                                for (BluetoothGattDescriptor bluetoothGattDescriptor : characteristic.getDescriptors()) {


                                    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0) {
                                        bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);//0x02,0x00
                                        gatt.setCharacteristicNotification(characteristic, true);
                                    }
                                    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) {
                                        bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);//0x01,0x00
                                      //  gatt.setCharacteristicNotification(characteristic, true);
                                    }
                                    if(gatt.writeDescriptor(bluetoothGattDescriptor)) //line ABC
                                    {
                                        Log.i(TAG, "Characteristics Descriptor Written Successfully");

                                    }
                                    else
                                    {
                                        Log.i(TAG, "Characteristics Descriptor Writing Failure");

                                    }

标签: androidbluetooth-lowenergyandroid-bluetoothbluetooth-gatt

解决方案


推荐阅读