无法从 BLE 设备读取特征值
Posted
技术标签:
【中文标题】无法从 BLE 设备读取特征值【英文标题】:Not able to read a characteristic value from BLE device 【发布时间】:2016-08-08 12:18:31 【问题描述】:我需要向 android BLE 设备写入和读取特征值。我会写但不会读。这是我的写作方式:
public void writeCustomCharacteristic(int value)
if (mBluetoothAdapter == null || mBluetoothGatt == null)
Log.w(TAG, "BluetoothAdapter not initialized");
return;
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("<MY SERVICE UUID>"));
if(mCustomService == null)
Log.w(TAG, "Custom BLE Service not found");
return;
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("<MY CHARACTERSTIC UUID>"));
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
mWriteCharacteristic.setValue(value,BluetoothGattCharacteristic.FORMAT_UINT8,0);
if(!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic))
Log.w(TAG, "Failed to write characteristic");
else
Log.w(TAG, "Success to write characteristic");
如果我使用这个操作是成功的
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
如果我使用任何其他写入类型而不是WRITE_TYPE_NO_RESPONSE
,那么它不会触发onCharacteristicWrite()
回调方法。
这是我阅读特征的方式:
private boolean readCharacteristics(int groupPosition,int childPosition)
try
if (mGattCharacteristics != null)
final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(2).get(2);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0)
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null)
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
mBluetoothLeService.readCharacteristic(characteristic);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(characteristic, true);
waitSometime(100);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0)
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicIndication(characteristic, true);
waitSometime(100);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0)
byte[] value = characteristic.getValue(); //this is being NULL always
if (mNotifyCharacteristic != null)
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
mNotifyCharacteristic = null;
mBluetoothLeService.writeCharacteristic(characteristic, value);
if(value!=null&&value.length>0)
Toast.makeText(DeviceControlActivity.this,"success reading data",Toast.LENGTH_LONG).show();
else
Toast.makeText(DeviceControlActivity.this,"error reading data",Toast.LENGTH_LONG).show();
/* if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0)
mNotifyCharacteristic = characteristic;
//characteristic.setWriteType();
mBluetoothLeService.setCharacteristicIndication(
characteristic, true);
byte[] value=characteristic.getValue();
*/
return true;
catch (Exception e)
e.printStackTrace();
return false;
characteristic.getValue()
始终返回 null。我哪里错了?
【问题讨论】:
您错误地使用了 bitor 而不是 bitand。另外,不要睡觉,然后期望该值已被读取。而是等待通知读取何时完成的回调。 【参考方案1】:这是我的看法:
您需要弄清楚您将获得什么样的特征数据?其中一些需要先设置指示或通知!喜欢:
一个。获取特征描述符
BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptors().get(0);
//Usually is Client_Characteristic_Configuration
b.根据特性,设置描述符的property()为true。
if ((gattCharacteristic.PROPERTY_NOTIFY)> 0 )
if (descriptor != null)
descriptor.setValue((BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE));
else if((gattCharacteristic.PROPERTY_INDICATE) >0 )
if(descriptor != null)
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
c。将值发送到远程 BLE 设备。
gatt.writeDescriptor(descriptor);
之后,你可以在回调函数中获取数据:
onCharacteristicChanged
至少以上对我有用! 希望能帮到人~
【讨论】:
你救了我的命以上是关于无法从 BLE 设备读取特征值的主要内容,如果未能解决你的问题,请参考以下文章
samsung ble api 无法从多个 GATT 特征中获取通知