如何在Android Studio中正确读取浮动特征

问题描述 投票:0回答:1

我正在使用ArduinoBLE库创建服务和特征:

BLEService angleService("1826");
BLEFloatCharacteristic pitchBLE("2A57", BLERead | BLENotify);

我添加了服务和特性并公布了我的设备:

  BLE.setLocalName("acsAssist");
  BLE.setAdvertisedService(angleService);
  angleService.addCharacteristic(pitchBLE);
  BLE.addService(angleService);
  pitchBLE.writeValue(0);
  BLE.advertise();

我先进行一些计算,然后将计算出的值写入服务:

 pitchBLE.writeValue(posiPitch);

posiPitch是浮点值,例如1.96。它可以从-90.00到90.00

我尝试从我的Android应用读取此值:

(characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_SFLOAT,0))

我得到疯狂的数字,例如-1.10300006E9

如何读取我的float值,以便我的android应用程序值与arduino值相匹配?

android arduino bluetooth-lowenergy gatt bluetooth-gatt
1个回答
0
投票

我通过利用字符串特性极大地简化了这种情况。

尽管这可能会占用更多资源,但它减少了必须解析字节数组以将数据转换为所需数据的麻烦。

我的字符串特征是通过自定义UUID制作的(以避免Bluetooth GATT标准冲突):

BLEStringCharacteristic pitchBLE("78c5307a-6715-4040-bd50-d64db33e2e9e", BLERead | BLENotify, 20);

[按照我的原始帖子中的说明进行广告并执行计算后,我只写了我的特征并将值作为字符串传递:

pitchBLE.writeValue(String(posiPitch));

在我的Android应用程序中,我只是获得特征的字符串值:

characteristic.getStringValue(0)

我希望这将有助于像我这样的未来开发人员,他们正在努力寻找有关此类信息的优质清晰资源:)

© www.soinside.com 2019 - 2024. All rights reserved.