Android BLE特性getValue返回null

时间:2016-03-09 21:49:23

标签: android bluetooth-lowenergy beacon

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

MyCode:

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

      }

MyBeacon

Also check the issue in other thread too.

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

3 个答案:

答案 0 :(得分:3)

语法应如下,

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回调获得通知。

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

答案 1 :(得分:1)

我遇到了类似的问题,其中characteristic.getValue返回Null。我一直在关注BLE Gatt文档和其他博客中提到的内容,但是问题仍然存在,直到最终我明白我做错了什么。

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

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

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

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

答案 2 :(得分:0)

你过早读过吗?应在onCharacteristicWrite()被调用后阅读。