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
0C715D2A6DE664A3CB3C06B7
0C715D2A6DE6Scanning...
Found 3
Inquiry, COD=0
0C715D2A6DE6,5A020C
000FE477F066,520xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0C715D2A6DE6
000FE477F0660C715D2A6DE6
000FE477F066Scanning...
Found 3
Inquiry, COD=0
64A3CB3C06B7,7A020C
0C715D2A6DE6,5A0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx64A3CB3C06B7
0C715D2A6DE664A3CB3C06B7
0C715D2A6DE6