蔡茗鈞
蔡茗鈞

Reputation: 1

Problems reading BLE characteristic

I am sending a byte array of size 8 every one second from HC08 module using software serial on arduino nano.However, when i try to read characteristic from my android app, where i also read every one second, onChracteristicread gets invokes every time but the received data is always 0. I am reading from SERVICE_UUID: UUID = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb") CHARACTERISTIC_UUID: UUID = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb") , which I think are the complete form of FFE0 and FFE1. Did i miss any step?

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")
            }
        }

Below is the code for 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);
}

I am pretty sure it actually sends data because the received data can be found under service id FFE0 and characteristic id FFE1(I used "nRF connect" to confirm this)

Upvotes: 0

Views: 56

Answers (1)

dev.tejasb
dev.tejasb

Reputation: 578

@蔡茗鈞 you are correct.

For anyone still having this problem, I'm writing this answer.

When reading a BLE characteristic, we've this method:

onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)

This method still works for API level below 33 but, is deprecated in API level 33, so we have to use:

onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value, int status)

Upvotes: 0

Related Questions