You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino HC08发送BLE数据,Android端读取特征值始终为0

问题描述

我在Arduino Nano上通过SoftwareSerial向HC08模块每秒发送一个8字节的数组,但在Android应用中每秒读取对应BLE特征值时,onCharacteristicRead回调每次都会触发,不过接收的数据始终为0。使用的SERVICE_UUID为UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb"),CHARACTERISTIC_UUID为UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"),已通过nRF Connect确认HC08确实在发送数据。

两端代码

Android端代码

override fun onCharacteristicRead(
            gatt: BluetoothGatt,
            characteristic: BluetoothGattCharacteristic,
            status: Int
        ) {
            super.onCharacteristicRead(gatt, characteristic, status)

            if (status == BluetoothGatt.GATT_SUCCESS) {
                // Read the value from the characteristic
                // Update the readValue variable
                readValue = characteristic.value
                val s = characteristic.value.size
                // Log the read status
                Log.v("bluetooth", "onCharacteristicRead size: $s")
            } else {
                // Log if the read operation failed
                Log.e("bluetooth", "onCharacteristicRead: failed")
            }
        }

Arduino端代码

#include <SoftwareSerial.h>

SoftwareSerial HC08(2,3);

void setup() {
  Serial.begin(9600);
  HC08.begin(9600);
}
  float value1 = 0;
  float value2 = 10;
void loop() {  
  byte bytes[8]; // Array to hold bytes for two floats
  
  byte *bytePtr = (byte *)&value1;
  for (int i = 0; i < 4; i++) {
      bytes[i] = bytePtr[i];
  }
  
  bytePtr = (byte *)&value2;
  for (int i = 0; i < 4; i++) {
      bytes[i + 4] = bytePtr[i];
  }
  
  // Now, send the bytes array over Bluetooth
  Serial.print("Sent data\n");
 HC08.write(bytes, sizeof(bytes));
//HC08.write("abc");
  value1++;
  value2--;
  delay(1000);
}
排查与解决方案
  • 确认BLE特征的读写属性:HC08的FFE1特征默认可能仅支持写操作,不允许主动读取。用nRF Connect查看该特征的Properties,必须包含Read标识。如果没有,需要通过AT指令修改HC08的特征配置(具体指令参考对应型号手册,比如部分HC08可用AT+CHAR=0,1开启读权限)。
  • 切换到BLE通知模式接收数据:HC08串口转发的数据是通过BLE通知/指示通道推送的,而非存储在特征缓存中供主动读取。你需要在Android端注册特征通知,而非调用readCharacteristic:
    1. 开启特征通知并配置描述符:
      val targetService = gatt.getService(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb"))
      val targetChar = targetService?.getCharacteristic(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"))
      targetChar?.let {
          gatt.setCharacteristicNotification(it, true)
          val desc = it.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"))
          desc.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
          gatt.writeDescriptor(desc)
      }
      
    2. 重写onCharacteristicChanged回调接收推送的数据:
      override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
          super.onCharacteristicChanged(gatt, characteristic)
          val receivedData = characteristic.value
          Log.v("bluetooth", "Received data size: ${receivedData.size}")
          // 后续可按小端字节序解析float数据
          val value1 = ByteBuffer.wrap(receivedData, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getFloat()
          val value2 = ByteBuffer.wrap(receivedData, 4, 4).order(ByteOrder.LITTLE_ENDIAN).getFloat()
      }
      
  • 理解HC08的串口-BLE映射逻辑:HC08的串口输入数据会直接通过BLE通知推送给订阅设备,不会将数据持久化到特征的可读取缓存中,因此主动调用readCharacteristic只能获取特征的初始默认值(即全0)。
  • 后续数据解析注意字节序:Arduino的float采用小端字节序,Android解析时需保持一致,避免出现数值解析错误。

内容的提问来源于stack exchange,提问作者蔡茗鈞

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 21:05:00