小编ste*_* 34的帖子

使用 Android BLE 读取特征值

我对 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
}
Run Code Online (Sandbox Code Playgroud)

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

任何帮助将不胜感激。

谢谢!

android bluetooth characteristics bluetooth-lowenergy

4
推荐指数
1
解决办法
9136
查看次数