本文介绍了无法读取的特点。 Android的BLE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的远程BLE设备的特定特征的数据读取到我的Andr​​oid平板电脑的Nexus 7。

I'd like to read the data from a specific characteristic of my remote BLE device to my Android tablet Nexus 7.

问题是,我可以通过启用该特性的通知,即使没有调用 readCharacteristic 接收数据。但我不能成功地通过调用 readCharacteristic 不启用通知写特性。

The problem is that, I can receive the data by enabling the notification of that characteristic even without calling readCharacteristic. But I cannot successfully read characteristic by calling readCharacteristicwithout enabling the notification.

mBluetoothGatt.readCharacteristic(特性)返回false。因此,功能 onCharacteristicRead 从未被触发。我还检查了属性值 BluetoothGattCharacteristic.PROPERTY_READ ,它是30。

mBluetoothGatt.readCharacteristic(characteristic) returns false. Thus the function onCharacteristicRead has never been triggered. I also checked the property value BluetoothGattCharacteristic.PROPERTY_READ, it is 30.

有没有人有什么是怎么回事一些想法?我真的需要分别读取的特点。因为如果根据通知我只分析数据,将数据从何处开始我想不通。这是因为我的设备将每次发送12bytes。它将连续发送的字节数组。然而,通知会给我带来数据一个字节一次。因此,我不知道这是字节数组的开头字节。

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. 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.

我现在使用的Andr​​oid所提供的样品code。

I'm using the sample code provided by Android right now.

下面是片段:

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

在回调code是:

    @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!

推荐答案

您需要首先启用特征通知,然后尝试读取它的值,并记住得到字节数组此时,相应的格式返回characteristic.getValue()方法。问候

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. Regards

这篇关于无法读取的特点。 Android的BLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:25