关于android:无法从BLE设备读取特征值
Not able to read a characteristic value from BLE device
我需要向 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);
|
如果我使用任何其他写入类型而不是
这是我阅读特征的方式:
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) { |
相关讨论
- 您错误地使用了 bitor 而不是 bitand。另外,不要睡觉,然后期望该值已被读取。而是等待通知读取何时完成的回调。
这是我的看法:
你需要弄清楚你将获得什么样的特征数据?其中一些需要先设置指示或通知!喜欢:
一个。获取特征的描述符
BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptors().get(0);
//Usually is Client_Characteristic_Configuration |
乙。根据特性,设置描述符的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
|
至少以上对我有用!
希望能帮到人~
相关讨论
- 你救了我的命