【发布时间】:2018-03-24 17:17:21
【问题描述】:
我对 BLE、android 开发和 java 比较陌生。我正在尝试通过 BLE 从微控制器向我的安卓手机发送 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 个值,然后将它们作为浮点值存储在一个数组中,以便最终将它们用于图像重建。
非常感谢任何帮助。
谢谢!
【问题讨论】:
-
顺便提一个重要说明:
readCharacteristic是异步的,因此在某些情况下您可能无法立即获得值。结果将在BluetoothGattCallback.onCharacteristicRead中报告,并应从那里获取值。 -
感谢您的回答,实际上我做了相同的代码,但在我的情况下,我希望每 10 秒读取一次相同的特征,并且我使用相同的处理程序,但每次都会返回相同的值,为什么?
标签: android bluetooth bluetooth-lowenergy characteristics