Fill unsigned char array with data from bluetooth

I have following variables and I would like to fill incomingData array with bluetooth serial data. The data I am expecting from bluetooth is stream of numbers between 0-256. This is for ESP32 using BluetoothSerial.h library.

#define DATA_SIZE 40
unsigned char incomingData[DATA_SIZE];

The following for loop give me all 0 values for incomingData array although SerialBT.read() has values ranging between 0-256.

for (int i = 0; i < DATA_SIZE; i += 8) {

incomingData[DATA_SIZE] = SerialBT.read();

}

Obviously I am doing it wrong. Can someone please show right way?

Thanks.

Oops.
Off the end of the array

Sorry I did not get it.

#define DATA_SIZE 40
unsigned char incomingData[DATA_SIZE];

The elements of that array have index numbers from 0 to 39.
incomingData[0] to incomingData[39]. Forty items in total.

incomingData[40] is not in a memory space you have under control, and writing to it can create all kinds of havoc.

for (int i = 0; i < DATA_SIZE; i += 8) {

incomingData[DATA_SIZE] = SerialBT.read();

}

What are you trying to do here? As said, you are placing every read in incomingData[40] which is incorrect.

What's the reason for the +=8? How many elements do you want to read into the array, and at what index positions? How so you know that all the data is available to read?

Followiing is original code which decode audio data with codec2 protocol and make it available for ESP32's internal DAC.

unsigned char rx_encode_frame[ENCODE_FRAME_SIZE];
#define ADC_BUFFER_SIZE 320
#define ENCODE_FRAME_SIZE 40           
#define ENCODE_CODEC2_FRAME_SIZE  8

//Make a cycle to get each codec2 frame from the received frame
				
for (int i = 0; i < ENCODE_FRAME_SIZE; i += ENCODE_CODEC2_FRAME_SIZE)
{
 //Decode the codec2 frame
codec2_decode(codec2_state, output_buffer, rx_encode_frame + i);
					
// Add to the audio buffer the 320 samples resulting of the decode of the codec2 frame.
	for (int g = 0; g < ADC_BUFFER_SIZE; g++)
	audio_fifo.put(output_buffer[g]);
}

Thanks

This is how data shows up from blutooth with Serial.println(SerialBT.read());

18:45:45.636 -> 3
18:45:45.670 -> 0
18:45:45.704 -> 92
18:45:45.704 -> 178
18:45:45.738 -> 130
18:45:45.772 -> 0
18:45:45.806 -> 76
18:45:45.806 -> 178
18:45:45.840 -> 3
18:45:45.874 -> 96
18:45:45.874 -> 117
18:45:45.908 -> 83
18:45:45.942 -> 31
18:45:45.942 -> 112
18:45:45.976 -> 117
18:45:46.010 -> 223
18:45:46.010 -> 155
18:45:46.044 -> 32
18:45:46.078 -> 84
18:45:46.078 -> 95
18:45:46.111 -> 153
18:45:46.145 -> 96
18:45:46.179 -> 84
18:45:46.179 -> 91
18:45:46.213 -> 8
18:45:46.247 -> 97
18:45:46.247 -> 84
18:45:46.281 -> 95
18:45:46.315 -> 25
18:45:46.315 -> 96
18:45:46.349 -> 84
18:45:46.383 -> 215
18:45:46.383 -> 153
18:45:46.417 -> 99
18:45:46.451 -> 84
18:45:46.451 -> 223
18:45:46.486 -> 27
18:45:46.519 -> 97
18:45:46.519 -> 84
18:45:46.553 -> 87
18:45:46.587 -> 152
18:45:46.621 -> 97
18:45:46.621 -> 84
18:45:46.655 -> 95
18:45:46.689 -> 24
18:45:46.689 -> 32
18:45:46.723 -> 84
18:45:46.757 -> 91
18:45:46.757 -> 24
18:45:46.791 -> 97
18:45:46.825 -> 84
18:45:46.825 -> 95
18:45:46.859 -> 152
18:45:46.894 -> 96
18:45:46.894 -> 84
18:45:46.928 -> 95
18:45:46.962 -> 152
18:45:46.962 -> 96
18:45:46.996 -> 84
18:45:47.030 -> 95
18:45:47.064 -> 136
18:45:47.064 -> 96
18:45:47.098 -> 84
18:45:47.132 -> 119
18:45:47.132 -> 153
18:45:47.166 -> 32
18:45:47.200 -> 117
18:45:47.200 -> 223
18:45:47.234 -> 24
18:45:47.268 -> 96
18:45:47.268 -> 117
18:45:47.302 -> 95
18:45:47.336 -> 136
18:45:47.336 -> 97
18:45:47.370 -> 84
18:45:47.404 -> 95
18:45:47.404 -> 152
18:45:47.438 -> 97
18:45:47.471 -> 85
18:45:47.505 -> 87
18:45:47.505 -> 8
18:45:47.539 -> 97
18:45:47.573 -> 84
18:45:47.573 -> 95
18:45:47.608 -> 27
18:45:47.642 -> 96
18:45:47.642 -> 84
18:45:47.676 -> 95
18:45:47.710 -> 154
18:45:47.710 -> 97
18:45:47.744 -> 92
18:45:47.778 -> 95
18:45:47.778 -> 152
18:45:47.812 -> 96
18:45:47.846 -> 85
18:45:47.846 -> 223
18:45:47.880 -> 10
18:45:47.914 -> 32
18:45:47.948 -> 116
18:45:47.948 -> 87
18:45:47.982 -> 27
18:45:48.016 -> 99
18:45:48.016 -> 117
18:45:48.050 -> 223
18:45:48.084 -> 155
18:45:48.084 -> 97
18:45:48.118 -> 116
18:45:48.152 -> 223
18:45:48.152 -> 154
18:45:48.186 -> 112
18:45:48.220 -> 117
18:45:48.220 -> 223
18:45:48.254 -> 154
18:45:48.288 -> 99
18:45:48.288 -> 85
18:45:48.322 -> 95
18:45:48.356 -> 155
18:45:48.390 -> 32
18:45:48.390 -> 85
18:45:48.424 -> 223
18:45:48.458 -> 8
18:45:48.458 -> 97
18:45:48.492 -> 85
18:45:48.526 -> 95
18:45:48.526 -> 24
18:45:48.560 -> 97
18:45:48.594 -> 84
18:45:48.594 -> 95
18:45:48.628 -> 154
18:45:48.662 -> 96
18:45:48.662 -> 85
18:45:48.696 -> 95
18:45:48.730 -> 25
18:45:48.730 -> 97
18:45:48.764 -> 85
18:45:48.798 -> 93
18:45:48.832 -> 152
18:45:48.832 -> 96
18:45:48.866 -> 117
18:45:48.900 -> 95
18:45:48.900 -> 154
18:45:48.934 -> 96
18:45:48.968 -> 117
18:45:48.968 -> 95
18:45:49.002 -> 152
18:45:49.036 -> 97
18:45:49.036 -> 85
18:45:49.070 -> 95
18:45:49.104 -> 152
18:45:49.104 -> 99
18:45:49.138 -> 84
18:45:49.172 -> 95
18:45:49.172 -> 27
18:45:49.206 -> 33
18:45:49.240 -> 84
18:45:49.274 -> 87
18:45:49.274 -> 25
18:45:49.308 -> 98
18:45:49.342 -> 85
18:45:49.342 -> 119
18:45:49.376 -> 24
18:45:49.410 -> 35
18:45:49.410 -> 85
18:45:49.444 -> 95
18:45:49.478 -> 153
18:45:49.478 -> 38
18:45:49.512 -> 119
18:45:49.546 -> 93
18:45:49.546 -> 27
18:45:49.580 -> 114
18:45:49.613 -> 85
18:45:49.613 -> 95
18:45:49.647 -> 155
18:45:49.681 -> 98
18:45:49.715 -> 117
18:45:49.715 -> 95
18:45:49.749 -> 24
18:45:49.783 -> 102
18:45:49.783 -> 84
18:45:49.817 -> 95
18:45:49.851 -> 159
18:45:49.851 -> 99
18:45:49.885 -> 117
18:45:49.919 -> 223
18:45:49.919 -> 26
18:45:49.953 -> 97
18:45:49.987 -> 117
18:45:49.987 -> 215

Thanks.

I think you want to do this :

for (int i = 0; i < DATA_SIZE; i++) {

  incomingData[i] = SerialBT.read();

}
1 Like

Thanks. That works.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.