无法从BLE设备读取特征值

时间:2016-08-08 12:18:31

标签: android bluetooth-lowenergy android-bluetooth gatt

我需要为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。我哪里出错了?

1 个答案:

答案 0 :(得分:0)

这是我的意见:

您需要弄清楚要获取数据的什么样的特征?其中一些需要设置指示或先通知!喜欢:

a。获取特征的描述符

T

b。根据特性,将描述符的property()设置为true。

   BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptors().get(0); 
     //Usually is Client_Characteristic_Configuration

c。将值发送到远程BLE设备。

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

之后,您可以在回调函数中获取数据:

gatt.writeDescriptor(descriptor);

至少对我来说工作以上! 希望可以帮助到某人〜