8

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

问题是,即使不调用,我也可以通过启用该特征的通知来接收数据readCharacteristic。但是我无法在readCharacteristic不启用通知的情况下通过调用成功读取特征。

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

有人对这里发生的事情有一些想法吗?我真的需要单独阅读特征。因为如果我只根据通知分析数据,我无法弄清楚数据从哪里开始。这是因为我的设备每次都会发送 12 个字节。它会不断发送字节数组。但是,通知会一次给我带来一个字节的数据。所以我不知道哪个是字节数组的开始字节。

我现在正在使用 Android 提供的示例代码。

这是片段:

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

回调中的代码是:

    @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!!!!!!");
        }
    }

我已经阅读了阅读特性的文件,但没有任何帮助。谁能帮我?非常感谢!

4

1 回答 1

2

您需要首先为特征启用通知,然后尝试读取它的值并记住获取返回特征.getValue() 方法的字节数组的适当格式。问候

于 2014-04-29T00:04:09.030 回答