Android的BLE特性的getValue返回null [英] Android BLE characteristics getValue returns null
问题描述
我想文本数据写信给我的BLE设备。所以,我下面的Android蓝牙GATT类做任务。但我发现写作文的特点是好的,但在试图检索特性值,则返回null。
I am trying to write text data to my BLE device. So , i am following Android Bluetooth GATT classes to do the task. But i found writing the text to the Characteristics is fine but while trying to retrieve the Characteristics value , it returns null.
我的code:
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被调用//
// Successfully onCharacteristicWrite also gets called //
@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。
but while trying to read the Characteristics it returns 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")
}
}
<一个href=\"http://www.amazon.in/gp/product/B019U6O0IM?psc=1&redirect=true&ref_=oh_aui_detailpage_o00_s00\"相对=nofollow> MyBeacon
<一个href=\"http://stackoverflow.com/questions/35937529/how-to-transfer-data-between-two-android-devices-using-beacon\">Also检查其他线程太多的问题。
请帮助我,建议我一些解决编写和成功读取数据到我的灯塔。
Please help me out , suggest me some solution to write and read data successfully to my Beacon.
推荐答案
语法应该如下,
mBluetoothGatt.readCharacteristic(characteristic);
阅读特点:
您可以通过阅读特征 mBluetoothGatt.readCharacteristic(特性);
您可以阅读特点的描述如下,
You can have to read the characteristic's descriptor as follows,
mBluetoothGatt.readDescriptor(ccc);
在你读它,它应该通过调用onDescriptorRead回调返回数据。
在这里,您可以通过调用设置(订阅)通过任何通知或指示charactersitic:
Once you read it, it should return data by calling the onDescriptorRead callback. Here you can set up (subscribe) to the charactersitic through either notification or indication by calling:
mBluetoothGatt.setCharacteristicNotification(characteristic, true)
一旦返回true,则需要再次写入描述符(的通知或指示的值)
once it returns true you will need to write to the descriptor again (the value of notification or indication)
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(clientConfig);
一旦做到这一点,你将通过onCharacteristicChanged回调每次都获得通知的特征性改变。
Once this is done you will get notifications through onCharacteristicChanged callback every time the characteristic changes.
不要更新我,如果你有任何问题,同时实施,
Do update me , if you have any problems while implementing,
这篇关于Android的BLE特性的getValue返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!