为了账号安全,请及时绑定邮箱和手机立即绑定

Android BluetoothGatt 类编写特性属性检查始终为 false

Android BluetoothGatt 类编写特性属性检查始终为 false

当年话下 2023-06-04 10:29:04
我想创建一个 android 应用程序,以便连接 ESP32 板并从中检索数据,以及使用低功耗蓝牙通信将值发送到板的能力。我有一个内置 BLE 服务器的 ESP32 板。我已经实现了具有以下特征的自定义服务。/* 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 时总是在第一个条件中返回 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;    }
查看完整描述

1 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

经过大量搜索和尝试各种方法来实现 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 以及与我使用的 uuid 的区别。当我用随机生成的 uuid 替换我的旧 uuid 时,例如“cff6dbb0-996f-427b-9618-9e131a1d6d3f”,整个BLE 服务器的 writeCharacteristic 过程没有任何问题。


查看完整回答
反对 回复 2023-06-04
  • 1 回答
  • 0 关注
  • 120 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信