Bluetooth AT+INQ command- Detect If device is available to connect?

Hello there,

Im working on a proyect where I have saved the addresses of devices that are allowed to connect to the arduino on a SD file. Im using HC05 module

So, I start reading the file, take the device address on a variable and I need to know if the device is available, if not i keep reading the file to try out the next listed device. If it is available, i need to link to it and start working foward.

My problem is, with the AT+INQ command, it returns a list of devices only to the serial monitor? Cannot be storage in a local variable?

How can I scann the devices found by HC05 and see if one of them matches with the one I want to link to it?

This is an example of my code;

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

#define BT_SERIAL_RX 0 /* to communicate with the Bluetooth module's TX pin */
#define BT_SERIAL_TX 1 /* to communicate with the Bluetooth module's RX pin */

/* Initialize the software serial port */
SoftwareSerial BTserial(BT_SERIAL_RX, BT_SERIAL_TX);
char c = ' ';

void delayAndRead()
{
	delay(50);
	while(BTserial.available())
	{
		c = BTserial.read();
	}
	delay(800);
}

// Open serial communications and wait for port to open:
	Serial.begin(9600);
	while (!Serial) 
	{
	; // wait for serial port to connect. Needed for native USB port only
	}
	Serial.print("Initializing SD card...");

	if (!SD.begin(4)) 
	{
	Serial.println("Initialization of SD failed!");
	while (1);
	}

	Serial.println("Initialization Done.");
	
void initHC05ToInq()
{	
	string deviceAddress = ReadDeviceAddressAndSearch();
	
    BTserial.println("AT+CMODE=1");// Enable connect to any device
    delayAndRead();
    BTserial.println("AT+ROLE=1");// Set to master in order to enable scanning
    delayAndRead();
    BTserial.println("AT+INQM=1,2,24");//RSSI, Max 2 devices, ~30s
    delayAndRead();
    BTserial.println("AT+CLASS=0");// Disable COD filter
    delayAndRead();
    BTserial.println("AT+INIT");// Init.
    delayAndRead();
	
	if (deviceAddress == "") 
	{
		Serial.println("There is no device available to connect...");
		//switch on a buzzer
	}
	else
	{
		BTserial.println("AT+LINK=" + deviceAddress);// Link to known device 
		delayAndRead();
	}
}	

string ReadDeviceAddressAndSearch()
{
	File deviceFile = SD.open("devices.txt", FILE_READ);
	string device="";
	bool deviceAvailable = false;
	
	while(deviceFile.available()) 
		{
			device = deviceFile.readStringUntil('\r');
			deviceAvailable = IsDeviceAvailable(device);
			if(deviceAvailable)
				break;			 
		}
	return device;
}

bool IsDeviceAvailable(string device)
{
	/* 
        ??????????????????????????????
        BTserial.println("AT+INQ");
        if(device == BTserial.read())
        return true;
            else
            return false;
        */
}
//These are not important right now
void setup(){}
void loop(){}

[UPDATE]

I made some changes on the code, but im not sure if its the right way...

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

#define BT_SERIAL_RX 0 /* to communicate with the Bluetooth module's RXD pin */
#define BT_SERIAL_TX 1 /* to communicate with the Bluetooth module's TXD pin */

/* Initialize the software serial port */
SoftwareSerial BTserial(BT_SERIAL_RX, BT_SERIAL_TX);
char c = ' ';
bool succesful = false;

void delayAndRead()
{
	delay(50);
	while(BTserial.available())
	{
		c = BTserial.read();
	}
	delay(800);
}
	
void initHC05ToInq()
{	
    BTserial.println("AT+CMODE=1");// Enable connect to any device
    delayAndRead();
    BTserial.println("AT+ROLE=1");// Set to master in order to enable scanning
    delayAndRead();
    BTserial.println("AT+INQM=1,2,24");//RSSI, Max 2 devices, ~30s
    delayAndRead();
    BTserial.println("AT+CLASS=0");// Disable COD filter
    delayAndRead();
    BTserial.println("AT+INIT");// Init.
    delayAndRead();	
}	

string ReadDeviceAddressAndSearch()
{
	File deviceFile = SD.open("devices.txt", FILE_READ);
	string device="";
	bool deviceAvailable = false;
	
	while(logFile.available()) 
		{
			device = deviceFile.readStringUntil('\r');
			deviceAvailable = IsDeviceAvailable(device);
			if(deviceAvailable)
				break;			 
		}
	return device;
}

bool IsDeviceAvailable(string device)
{
	static char input[64];
    static uint8_t i;
    char data = '';
	bool result = false;
	
	while (BTSerial.available >0)
	{
		BTserial.println("AT+INQ");
		data = BTSerial.read();

		if ( data != '\r' && data != '\n' && i < sizeof( input ) - 1 )
		  input[i++] = data;
	   
		else
		{
		  input[i] = '\0';
		  i = 0;

		  if ( strcmp( input, device ) )
		  {
			Serial.println("Device Found");
			result = true;
			break;
		  }
		  else 
			  result = false;
		}
	}
	if (!result)
		Serial.println("Device not found");
	
	return result;
}

void setup() 
{	
	// Open serial communications and wait for port to open:
	Serial.begin(9600);
	while (!Serial) 
	{
	; // wait for serial port to connect. Needed for native USB port only
	}
	Serial.print("Initializing SD card...");

	if (!SD.begin(4)) 
	{
	Serial.println("Initialization of SD failed!");
	while (1);
	}
	Serial.println("Initialization Done.");
	// HC-05 default serial speed for AT mode is 38400
    BTserial.begin(38400); 
	initHC05ToInq();
}

void loop()
{
	string deviceAddress = ReadDeviceAddressAndSearch();
	
	if (!succesful)
	{
		if (deviceAddress == "") 
		{
			Serial.println("There is no device available to connect...");
			Serial.end();
			BTserial.end();
			setup(); // Try again
		}
		else
		{
			BTserial.println("AT+LINK=" + deviceAddress);// Link to known device 
			delayAndRead();
			succesfull = true;
		}	
	}
	else
	{
		//Do Stuff here
	}
}