Issues With RN-42 Bluetooth Module Inquiry Scan Output

Hey all,

So I'm using a RN-42 Bluetooth module with my Mega 2560, and i'm simply trying to scan for surrounding Bluetooth devices. I am able to get a some output, but it always seem to cut off after certain point, not matter how many devices are present (usually 2 devices or around 59 bytes of Serial Output).

Ideally, I want to scan and output all the ID's within range of the device. The attached code contains some parsing to narrow the output down to just the received ID's

Any help here? I have tried a fair number of solutions but nothing seems to work.

My code:

const int BUFFER_SIZE = 160;
byte incomingByte=-1;
byte index=0;
char macAddr[BUFFER_SIZE];
char validAddr[BUFFER_SIZE];


//macIterator iterates through buffered MAC data.
int macIterator, validIterator;

void setup(){


  Serial.begin(9600);
  Serial.println("Starting...");
  Serial1.begin(115200);
  delay(1500);
  Serial1.print("$$");
  delay(1000);

  // initialize our macAddr with a terminating char
  // x is used to determine when the last MAC ID
  // has been detected.
  clearBuffers();

  macIterator=0;
  validIterator=0;

  
  Serial.println("Ready.");
}


void getValidAddr(){
  char c='-';
  int cCount=0;

  while (c!='0')
  {
    c = macAddr[cCount];
    cCount++;
  }

  cCount+=2;
  while(macAddr[cCount]!='x')
  {
    if(macAddr[cCount]==',')
    {
      cCount+=7;
    }
    else
    {
      validAddr[validIterator]=macAddr[cCount];
      cCount++;
      validIterator++;

    }
  }

}  

void clearBuffers(){
  for(int i =0; i <BUFFER_SIZE; i++){
    macAddr[i] = 'x';
    validAddr[i] = '\0'; 
  }
}


void loop(){
  Serial.println("Scanning...");
  Serial1.println("IN6");
  delay(7500);


 

    
  if (Serial1.available() > 0) {


    // read the incoming byte:
    incomingByte = Serial1.read();

    // while the incoming byte has data
    // and the macAddr buffer is not full.
    while(index < BUFFER_SIZE && Serial1.available() > 0)
    {
      // read in the incoming byte to the buffer and a char.
      macAddr[index]=(char)incomingByte;
      // increment position in the buffer
      index++;
      // read in the next byte
      incomingByte=Serial1.read();

    }

  }
  Serial1.end();
  Serial1.begin(115200);
  getValidAddr();

  Serial.println("-------------");
  Serial.println(macAddr);  
  Serial.println("");
  Serial.println(validAddr);
  Serial.println("----------");

  index=0;
  macIterator=0;
  validIterator=0;
  delay(500);
  clearBuffers();


}

My Output:

Starting...
Ready.
Scanning...-------------
Found 3
Inquiry, COD=0
64A3CB3C06B7,7A020C
0C715D2A6DE6,5A0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx64A3CB3C06B7
0C715D2A6DE6

64A3CB3C06B7
0C715D2A6DE6

Scanning...

Found 3
Inquiry, COD=0
0C715D2A6DE6,5A020C
000FE477F066,520xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0C715D2A6DE6
000FE477F066

0C715D2A6DE6
000FE477F066

Scanning...

Found 3
Inquiry, COD=0
64A3CB3C06B7,7A020C
0C715D2A6DE6,5A0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx64A3CB3C06B7
0C715D2A6DE6

64A3CB3C06B7
0C715D2A6DE6

char macAddr[BUFFER_SIZE];
char validAddr[BUFFER_SIZE];

How many 160 character mac addresses have you seen?

  Serial1.end();
  Serial1.begin(115200);

Why?

void clearBuffers(){
  for(int i =0; i <BUFFER_SIZE; i++){
    macAddr[i] = 'x';
    validAddr[i] = '\0'; 
  }
}

Non-conventional is not necessarily better. There is a reason that strings are NULL terminated. And, that requires ONE NULL, not 160 of them.

      macAddr[index]=(char)incomingByte;
      // increment position in the buffer
      index++;

macAddr should be NULL terminated every time a character is added.

  Serial.println("-------------");
  Serial.println(macAddr);  
  Serial.println("");
  Serial.println(validAddr);
  Serial.println("----------");

Passing a char array to a function that expects a string is a recipe for disaster. A string is a NULL terminated array of chars. macAddr is NOT a string.

  while (c!='0')
  {
    c = macAddr[cCount];
    cCount++;
  }

'0' is not NULL. '\0' is. Why, when you stuffed the array full of 'x's are you now measuring the length by locating a '0'?

I have tried a fair number of solutions but nothing seems to work.

I'm willing to bet that reasonable sized arrays, conventionally handled would.

Thanks Paul.
I feel like an idiot for not taking these things into consideration as I've been working on this code.
I'll definitely try these fixes out.