5

我对 BLE、android 开发和 java 比较陌生。我正在尝试通过 BLE 从微控制器向我的 android 手机发送 20 条数据。使用 nRF 连接应用程序,我知道服务和值是从微控制器正确创建的。

为了在 android 应用程序上阅读它们,我使用了 Android Studio 中提供的模板 BluetoothLeGatt 应用程序。它向我展示了服务和特征(由于某种原因,它展示了比我实际创建的更多的服务),但是只有一个服务最终向我展示了一个不为 NULL 的值。我已经在我的微控制器中编码了值 0x22,Android 日志说给了我以下信息

03-24 17:13:29.498 23696-23696/com.example.android.bluetoothlegatt D/BLE_VALUE_READ: [B@b95cc24

而不是 0x22。这是使用功能

Log.d("BLE_VALUE_READ", String.valueOf(characteristic.getValue()));

我已将命令放在模板应用程序的此函数中。

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.readCharacteristic(characteristic);
    Log.d("CharacteristicRead", String.valueOf(characteristic)); // try to get characteristic uuid?
    Log.d("BLE_VALUE_READ", String.valueOf(characteristic.getValue())); // Try to read value
}

我怎样才能得到服务/特征中的实际值,即 0x22?我想总共读取 20 个值,然后将它们作为浮点值存储在一个数组中,以便最终将它们用于图像重建。

任何帮助将不胜感激。

谢谢!

4

1 回答 1

8

characteristic.getValue()返回一个字节数组。您需要将字节转换为所需的格式。

但是在你调用之后readCharacteristic,你需要等待onCharacteristicRead回调才能提取值。

于 2018-03-24T17:57:34.143 回答