gpt4 book ai didi

android - 在没有发现服务的情况下编写 BLE 特性 Android

转载 作者:行者123 更新时间:2023-11-29 18:36:41 28 4
gpt4 key购买 nike

是否可以在不事先发现 ble 服务的情况下编写 ble 特性?我想实现连接到外围 ble 设备的应用程序,写入特性并断开连接,然后连接到另一个设备等等......断开连接后,我调用 gatt.close() 命令释放所有资源。在第一次连接时,我建立了联系。如果我等待回调 onServicesDiscovered 并写入特征:

BluetoothGattService mCustomService = mBluetoothGatt.getService(myUUID));
byte[] values = new byte[]{0x00, 0x01};
mWriteCharacteristic.setValue(values);
if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false) {
Log.w(TAG, "Failed to write characteristic");
}

一切正常。但我想改进并加快整个过程。有什么方法可以在连接到绑定(bind)设备后跳过发现服务并写入 ble char 吗?

BluetoothGattCharacteristic mCharacteristic = new BluetoothGattCharacteristic(UUID.fromString(GattAttributes.P1_MINI_POWER_CHARACTERISTIC), (BluetoothGattCharacteristic.PROPERTY_WRITE |BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE | BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS), 0);
mCustomService.addCharacteristic(mCharacteristic);
byte[] values = new byte[]{0x00, 0x01};
mWriteCharacteristic.setValue(values);
if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false) {
Log.w(TAG, "Failed to write characteristic");
}

但没有任何成功。

最佳答案

不,您必须使用 discoverService。你不能自己写定义。许多原因之一是 BLE 堆栈需要知道特征值的 ATT 句柄,这不会通过 BluetoothGattCharacteristic 公开。

关于android - 在没有发现服务的情况下编写 BLE 特性 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54072711/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com