6

我正在尝试将文本数据写入我的 BLE 设备。所以,我正在关注 Android 蓝牙 GATT 课程来完成这项任务。但是我发现将文本写入特性很好,但是在尝试检索特性值时,它返回 null。

我的代码:

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);

    }

但是在尝试读取特征时,它返回 null。

  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")
                  }

      }

我的信标

还要检查其他线程中的问题。

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

4

3 回答 3

4

语法应该如下,

mBluetoothGatt.readCharacteristic(characteristic);

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

您可能必须按如下方式读取特征的描述符,

mBluetoothGatt.readDescriptor(ccc);

阅读后,它应该通过调用 onDescriptorRead 回调返回数据。在这里,您可以通过通知或调用指示来设置(订阅)特征:

mBluetoothGatt.setCharacteristicNotification(characteristic, true)

一旦它返回true,您将需要再次写入描述符(通知或指示的值)

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

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

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

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

于 2016-03-10T07:39:51.423 回答
2

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

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

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

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

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

于 2018-08-01T20:42:08.013 回答
0

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

于 2016-03-11T06:37:18.210 回答