Android BLE 特性 getValue returns null

Android BLE characteristics getValue returns null

我正在尝试将文本数据写入我的 BLE 设备。所以,我正在关注 Android Bluetooth GATT 类 来完成这项任务。但我发现将文本写入特征很好,但在尝试检索特征值时,它 returns 为空。

我的代码:

public void writeCharacteristic(BluetoothGattCharacteristic characteristic,
                                String text) {

    String TAGS ="MyBeacon";

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAGS, "BluetoothAdapter not initialized");
        return;
    } else {
        Log.w(TAGS, "Writting ... ");
    }
    byte[] data = hexStringToByteArray(text);


    Log.w(TAGS, "Writting text = " + data);


    try {
        characteristic.setValue(URLEncoder.encode(text, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    boolean writeValue = mBluetoothGatt.writeCharacteristic(characteristic);

    Log.w(TAGS, "Writting Status = " + writeValue);

}

// onCharacteristicWrite 也成功调用 //

   @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);

        String TAGS ="MyBeacon";

        String text = null;
        try {
            text = new String(characteristic.getValue(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.w(TAGS, "onCharacteristicWrite = " + text+" :: "+status);

    }

但在尝试阅读特征时 returns 无效。

  for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {

                final byte[] data = gattCharacteristic.getValue(); // returns null

                  if (data != null && data.length > 0) {

                     Log.d("MyBeacon", " Read Data ")

                  } else {

                     Log.d("MyBeacon", " Data is null")
                  }

      }

MyBeacon

请帮帮我,建议我一些解决方案来成功地向我的 Beacon 写入和读取数据。

语法如下,

mBluetoothGatt.readCharacteristic(characteristic);

阅读特点: 您可以使用 mBluetoothGatt.readCharacteristic(characteristic);

读取特征

您可以按如下方式阅读特征的描述符,

mBluetoothGatt.readDescriptor(ccc);

阅读后,它应该 return 通过调用 onDescriptorRead 回调获取数据。 在这里您可以通过通知或指示来设置(订阅)特性,方法是调用:

mBluetoothGatt.setCharacteristicNotification(characteristic, true)

一旦return为真,您将需要再次写入描述符(通知或指示的值)

BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

//clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(clientConfig);

完成此操作后,您将在每次特征更改时通过 onCharacteristicChanged 回调收到通知。

如果您在实施过程中遇到任何问题,请随时更新我,

你是不是看得太早了?应在调用 onCharacteristicWrite() 后读取。

我遇到了类似的问题 characteristic.getValue returns Null。我完全按照 BLE Gatt 文档和其他博客中提到的内容进行操作,但问题仍然存在,直到我终于明白我做错了什么。

在客户端设备端,我们setValue进入我们感兴趣的characteristic使用

gatt.WriteCharacteristic(characteristic.setValue("Hello"));

在服务器端,请求被接收到 onCharacteristicWriteRequest(....) 回调。 通常我们希望我们在客户端设置的值由 characteristic 参数携带,但我们观察到 characteristic.getValue()null.

在同一个回调中,我们还有另一个名为 "Value" 的参数,它实际上带有我们在客户端设置的 characteristic 值。请参考此参数,这应该可以解决问题。