写特性属性 check is always false Android BluetoothGatt class

Writing characteristic property check is always false Android BluetoothGatt class

我想创建一个 android 应用程序,以便连接 ESP32 开发板并从中检索数据,以及使用低功耗蓝牙通信向开发板发送值的能力。

我有一个带 BLE 服务器的 ESP32 开发板inside.I已经实现了具有以下特征的自定义服务。

/* define the characteristic and it's propeties */
BLECharacteristic dataCharacteristic(
    BLEUUID((uint16_t)0x1A00),
    BLECharacteristic::PROPERTY_READ |
    BLECharacteristic::PROPERTY_WRITE |    
    BLECharacteristic::PROPERTY_NOTIFY);

我成功地在 android 应用程序中实现了所有扫描、读取和通知功能,但是在编写 BluetoothGatt.writeCharacteristic 时总是 returns false 第一个条件:

  if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
                && (characteristic.getProperties()
                & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) {
            return false;
        }

在调试 android 应用程序时,characteristic.getProperties() 始终为 18。

 public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
                && (characteristic.getProperties()
                & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) {
            return false;
        }

        if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
        if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;

        BluetoothGattService service = characteristic.getService();
        if (service == null) return false;

        BluetoothDevice device = service.getDevice();
        if (device == null) return false;

        synchronized (mDeviceBusy) {
            if (mDeviceBusy) return false;
            mDeviceBusy = true;
        }

        try {
            mService.writeCharacteristic(mClientIf, device.getAddress(),
                    characteristic.getInstanceId(), characteristic.getWriteType(),
                    AUTHENTICATION_NONE, characteristic.getValue());
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
            mDeviceBusy = false;
            return false;
        }

        return true;
    }

这里是使用上述函数的写特征代码:

            //Get BLE Service
            BluetoothGattService service = gatt.getService(SERVICE_UUID);

            //Get the characteristic
            BluetoothGattCharacteristic dataCharacteristic = 
            service.getCharacteristic(DATA_CHARACTERISTIC_UUID);

            //Pass value
            dataCharacteristic.setValue(value);

            //Write characteristic
            boolean success = gatt.writeCharacteristic(dataCharacteristic);

            if(success){
                Log.d(TAG, "Write characteristic successful");
            }
            else{
                Log.e(TAG, "Write characteristic failed");
            }

我已经试过在没有读或通知的情况下执行写操作,以防止多次请求,但结果是一样的。

我还尝试使用具有读写通知功能的外部 BLE 应用程序,它们都与我的 ESP32 设置完美配合。

经过大量搜索和尝试各种方法来实现 android 应用程序和内置 BLE 服务器的 ESP32 板的上述通信,我找到了解决方案。

在我的所有测试中,我都按以下方式使用 UUID:

在 ESP32 中声明特征 uuid

Characteristic_UUID = BLEUUID((uint16_t) 0x1A00))

并使用

从 android 搜索此 uuid
Characteristic _UUID = convertFromInteger (0x1A00)

convertFromInteger 函数:

    public UUID convertFromInteger (int i) 
    {
        final long MSB = 0x0000000000001000L;
        final long LSB = 0x800000805f9b34fbL;
        long value = i & 0xFFFFFFFF;
        return new UUID (MSB | (value << 32), LSB);
    }

当我按照一个教程找到解决方案时,我注意到了 uuid 以及与我 used.When 的区别我用随机生成的 uuid 替换了我的旧 uuid,例如 "cff6dbb0-996f-427b-9618-9e131a1d6d3f", BLE 服务器的整个 writeCharacteristic 过程顺利完成。