java - bluetoothGatt.writeCharacteristic 始终返回 false,BLE 特征类型为 write_no_reponse

标签 java android android-ble

我正在编写一个 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()));
}

我有这个错误很长时间了,非常感谢,如果有人能帮忙。

最佳答案

characteristics

如图所示,我的服务中有 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/

相关文章:

java - 如何在 Java 中将 UTC 时间戳转换为本地日、时、分?

java - 如何使单行(RowTable)中的所有小部件可见?

java - 如何通过Bundle将数据从 fragment 传输到 fragment

android - API 级别 22 中不推荐使用 stopLeScan() 和 startLeScan() - 我该如何用 stopScan() 和 startScan() 替换它?

java - 如何设置 Eclipse 工作区,为 Android 开发但在 Java 中测试?

java - 如何从 KNeighborhoodFilter 中提取样本

java - 在一帧中显示消息对话框警告和信息消息类型

javascript - 带有 javascript 的 HTML 未在 android webview 中加载

Android BluetoothLEGatt BLE 无法保持与设备的连接

android - 如何在以下代码中定义 SampleGattAttributes?