Read multiple data via Bluetooth with Ionic and Arduino

Hi, I’ve a very simple application, I need to send measurement data via Bluetooth from my Arduino to my Ionic application.
But I have questions about the correct way to do this on Ionic.

Arduino Code

char BTString;

if (serial1.available()) {
BTString = serial1.read();

/* S is the command that my Ionic application sends to start the measurement */
if (BTString == 'S') {
  		BTString = "";

	/* Here is where I will do the calculations of the measurements and send the results via Bluetooth, but in this example I will only send hypothetical results */
	
 		Serial.write("D"); // This indicates Start
  		Serial.write("45.55"); // measurement 1
  		Serial.write("79.80"); // measurement 2
  		Serial.write("67.20"); // measurement 3
  		Serial.write("F"); // This indicates the end
}

}

Ionic Code

/* */
this._bluetoothSerial.write(‘S’) // Start the measurement
.then((data: any) => {
this.measuring(); // Wait for results
})
.catch((e) => {
this.errorCommBluetooth(); // Error alert
});

private measuring(): void {
this.readOk = false;

do {
  this._bluetoothSerial.available()
  .then((data: any) => {
    this._bluetoothSerial.readUntil('F')
    .then((data: any) => {

      if (data[0] == 'D') {
        this.measurement1 = data[1] + data[2] + data[3] + data[4] + data[5];
        this.measurement2 =  data[6] + data[7] + data[8] + data[9] + data[10];
        this.measurement3 =  data[11] + data[12] + data[13] + data[14] + data[15];
        
        this.readOk = true;
      }

    });
  });
}while (this.readOk == false);

}

This code does not work.
Please, what would be the best way to do this and store the measurements in the 3 variables, using the readUntil function or not.

Solved! This code worked for me:

this._bluetoothSerial.available()
.then((number: any) => {
    this._bluetoothSerial.read()
    .then((data: any) => {
      if (data[0] == "D" && data[16] == "F") {
        this.measure1 = parseFloat(data[1]+data[2]+data[3]+data[4]+data[5]);
        this.measure2 = parseFloat(data[6]+data[7]+data[8]+data[9]+data[10]);
        this.measure3 = parseFloat(data[11]+data[12]+data[13]+data[14]+data[15]);

        this._bluetoothSerial.clear();
      }
    });
});
2 Likes