Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android getValue返回null_Android_Bluetooth Lowenergy_Beacon - Fatal编程技术网

Android getValue返回null

Android getValue返回null,android,bluetooth-lowenergy,beacon,Android,Bluetooth Lowenergy,Beacon,我正在尝试将文本数据写入我的BLE设备。所以,我遵循Android蓝牙GATT类来完成这项任务。但是我发现将文本写入特征是可以的,但是在尝试检索特征值时,它返回null for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { final byte[] data = gattCharacteristic.getValue(); // returns null

我正在尝试将文本数据写入我的BLE设备。所以,我遵循Android蓝牙GATT类来完成这项任务。但是我发现将文本写入特征是可以的,但是在尝试检索特征值时,它返回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")
                  }

      }
霉菌代码:

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

      }


请帮助我,向我建议一些解决方案,以便成功地将数据写入和读取到我的信标。

语法应如下所示

mBluetoothGatt.readCharacteristic(characteristic);
mBluetoothGatt.readDescriptor(ccc);
阅读特点: 您可以使用
mbluetothgatt.readCharacteristic(特征)读取特征

您可以读取特征的描述符,如下所示:

mBluetoothGatt.readCharacteristic(characteristic);
mBluetoothGatt.readDescriptor(ccc);
读取后,它应该通过调用onDescriptorRead回调返回数据。 在这里,您可以通过通知或通过调用以下命令来设置(订阅)CharacterSic:

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

完成后,每次特性更改时,都会通过onCharacteristicChanged回调收到通知


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

您是否阅读得太早了?应该在调用
onCharacteristicWrite()
后读取它。

我遇到了一个类似的问题,
characteristic.getValue
返回
Null
。我完全遵循了BLE Gatt文件和其他博客中提到的内容,但问题仍然存在,直到我终于明白我做错了什么

在客户端设备端,我们将
设置值
放入我们感兴趣使用的
特性

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


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

要读取特征,是否尝试mBluetoothGatt.readCharacteristic(特征),然后捕获onCharacteristicRead()回调?是的,但onCharacteristicRead未调用。感谢您的回复,我只想知道,writeDescriptor将在onServiceDiscovered回调中写入。您还可以告诉我,什么是characteristic.getDescriptor(CCC);什么是“CCC”。CCC-客户端特征配置描述符RIT定义了特定clientcharacteristic如何配置特征。getDescriptor(CCC)返回null,似乎CCC不支持我的设备。那我该怎么做呢?不,在onCharacteristicWrite only之后,我打电话给OnCharacteristicRead抱歉,我搞错了。gattCharacteristic.getValue()不会从设备读取值,它只返回“此特性的存储值”。所以它不应该是空的。顺便说一句,您已经在onCharacteristicWrite()中调用了getValue(),那么关于Log.w(标记“onCharacteristicWrite=“+text+”:“+status)”的消息是什么;?日志用于读取特征的存储值。“text”是包含字节值文本数据的变量,我认为“final byte[]data=gattCharacteristic.getValue();//返回null”将得到与“text=new String(characteristic.getValue(),“UTF-8”);”相同的结果。。。或者,gattCharacteristic.getValue()的定义是什么?