- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 Android 应用程序来与 BLE 仪表通信。我已经能够扫描设备、连接到目标、发现服务、获取特征。然而,当我尝试编写 write_no_reponse 特性时,该方法总是返回 false。我确信这些特性是可写的,我使用其他应用程序来做到这一点。当我调试到android.bluetooth代码时,出现以下序列失败,可能它返回一个空服务,调试运行到这一步,然后单步跳出该方法:
BluetoothGattService service = characteristic.getService();
这会导致 writeCharacteristic 返回 false。但在 writeCharacteristic 之前,我有一个日志
Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
bluetoothGatt.writeCharacteristic(mCharacteristic);
且日志正确,不返回null;
非常感谢任何帮助!
我使用相同的代码来写入 WRITE 特性,它起作用了。我尝试使用
mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
设置写入类型,但也失败。
写入数据代码:
public void writeReadDataCommand() {
if (null != bluetoothGatt) {
mCharacteristic.setValue("123123");
bluetoothGatt.setCharacteristicNotification(mCharacteristic, true);
mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
boolean isWrite = bluetoothGatt.writeCharacteristic(mCharacteristic);
if (isWrite) {
Log.d(TAG, "writeReadDataCommand: write data to BLE");
}
}
}
onServiceDiscovered 代码:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
bluetoothGatt = gatt;
BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID));
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));
Log.d(TAG, "onServicesDiscovered: writeType" + characteristic.getWriteType());
boolean isSuccess = gatt.setCharacteristicNotification(characteristic, true);
if (isSuccess) {
mCharacteristic = characteristic;
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
if (null != descriptors && descriptors.size() > 0) {
for (BluetoothGattDescriptor descriptor : descriptors) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
connectCallback.findService();
} else {
Log.d(TAG, "onServicesDiscovered received: " + status);
}
}
并且 onCharacteristicWrite 回调不会触发
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "onCharacteristicWrite: " + Arrays.toString(characteristic.getValue()));
}
我有这个错误很长时间了,非常感谢,如果有人能帮忙。
最佳答案
如图所示,我的服务中有 2 个特征 - 0 和 1。这些特征具有相同的 uuid。我做的第一件事是
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));
这段代码找到了0特征,然后我向其中写入数据。显然,我失败了。因为0特性只能READ和NOTIFY。所以我执行以下步骤将日期写入 BLE
characteristics = service.getCharacteristics();
for(BluetoothGattCharacteristic characteristic : characteristics){
if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
enableNotification(characteristic);
}
}
for(BluetoothGattCharacteristic characteristic : characteristics){
if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
characteristic.setValue("123123");
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
boolean isWrite = bluetoothGatt.writeCharacteristic(characteristic);
if (isWrite) {
Log.d(TAG, "writeReadDataCommand: write data to ble");
}
}
}
我尝试通知这两个特征并向这两个特征写入数据,尽管它们具有相同的UUID,但它们具有不同的功能。显然,一种特性将通知失败,一种将写入失败。但最终我成功写入了我的数据。
关于java - bluetoothGatt.writeCharacteristic 始终返回 false,BLE 特征类型为 write_no_reponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57386072/
从文档中我看到如何在没有响应的情况下这样写: BluetoothGattCharacteristic characteristic = ... characteristic.setValue(byte
我正在尝试在 BLE 外围设备上写入特征值。我正在使用下面提到的代码(writeCharacteristics 方法)。我可以通过单击按钮在外围设备上写入特性,但此操作仅成功一次。随后的写入不会发送到
我正在尝试通过低功耗蓝牙连接发送多个数据段。正如我在这篇文章 (link) 中所读到的,在触发 onCharacteristicWrite 回调之前,不会发送更多数据。我已经确认了这一点,但是直到我写
实际上我是通过蓝牙进行更新的。第一次我删除存储体,然后在上面写入一个十六进制文件。 但是有一半的时间更新无法正常工作,对于我传输的每个数据,第一个 writeCharacteristic 将返回 fa
我创建了一个具有 WRITE_TYPE_NO_RESPONSE 特性的外设。对于另一个应用程序,我写了特性,但我偶尔会遇到带有 lollipop 5.1.1 的 samsung galaxy nexu
我正在编写一个 Android 应用程序以使用 BLE 与 Arduino 对话。我已经能够扫描设备,连接到目标,发现服务,获取特征,并读取那些可读的。但是,当我尝试编写可写特性时,该方法总是返回 f
我正在开发 Android BLE 应用程序。我面临着,在某些 Android 设备中,当我尝试使用 writeCharacteristic() 方法在特征中写入数据时,它会返回 false 并且不会
我想读取存储在设备中的特征值,修改该值,然后将其写入设备。出于某种原因,writeCharacteristic() 返回 true,但内部设备值没有改变并且 onCharacteristicWrite
我正在编写一个 Android 应用程序来与 BLE 仪表通信。我已经能够扫描设备、连接到目标、发现服务、获取特征。然而,当我尝试编写 write_no_reponse 特性时,该方法总是返回 fal
我是一名优秀的程序员,十分优秀!