简体   繁体   English

无法读取特征。 Android BLE

[英]Cannot read characteristic. Android BLE

I'd like to read the data from a specific characteristic of my remote BLE device to my Android tablet Nexus 7. 我想将数据从我的远程BLE设备的特定特征读取到我的Android平板电脑Nexus 7。

The problem is that, I can receive the data by enabling the notification of that characteristic even without calling readCharacteristic . 问题是,即使不调用readCharacteristic ,也可以通过启用该特性的通知来接收数据。 But I cannot successfully read characteristic by calling readCharacteristic without enabling the notification. 但是我无法在不启用通知的情况下调用readCharacteristic来成功读取特征。

mBluetoothGatt.readCharacteristic(characteristic) returns false. mBluetoothGatt.readCharacteristic(characteristic)返回false。 Thus the function onCharacteristicRead has never been triggered. 因此,函数onCharacteristicRead从未被触发。 I also checked the property value BluetoothGattCharacteristic.PROPERTY_READ , it is 30. 我还检查了属性值BluetoothGattCharacteristic.PROPERTY_READ ,它是30。

Does anyone have some ideas about what is going on here? 有人对这里发生的事情有任何想法吗? I really need to read the characteristic separately. 我真的需要分别阅读特性。 Because if I only analysis data based on notification, I can not figure out where the data begins. 因为如果仅基于通知分析数据,则无法确定数据从何处开始。 This is because that my device will send 12bytes each time. 这是因为我的设备每次都会发送12个字节。 And it will continuously sending the byte array. 并且它将连续发送字节数组。 However, the notification would bring me the data one byte a time. 但是,通知会将数据一次带给我一个字节。 So I don't know which is the beginning byte of the byte array. 所以我不知道哪个是字节数组的开始字节。

I'm using the sample code provided by Android right now. 我正在使用Android现在提供的示例代码。

Here is the snippet: 这是代码段:

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    boolean status = mBluetoothGatt.readCharacteristic(characteristic);
    System.out.println("Initialize reading process status:" + status);

}



public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 

        mBluetoothGatt.writeDescriptor(descriptor);
    }
}

The code in callback is: 回调中的代码是:

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {

        System.out.println("In onCharacteristicRead!!!!!!!");
        if (status == BluetoothGatt.GATT_SUCCESS) {

            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            System.out.println("Received Data Success!!!!!!");
        }
    }

I have read through the document of reading characteristic, but nothing helps. 我已经阅读了阅读文档,但是没有帮助。 Can anyone help me? 谁能帮我? Thank you very much! 非常感谢你!

you need first enable notifications for characteristic and then try to read it's value and remember to get the appropiate format of the byte array that return characteristic.getValue() method. 您首先需要为特征启用通知,然后尝试读取其值,并记住获取返回特征.getValue()方法的字节数组的适当格式。 Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM