Arduino Leonardo Bluetooth Serial Problem

Hello, I am using hc06 bluetooth with arduino leonardo, I installed bluetooth serial port program to my android mobile, If arduino ide serial monitor open, I can send data to arduino, and relay opens, but when arduino ide serial monitor is not open, I am sending data from my mobile to arduino but nothing happens, what is the problem and how can i fix it ? please help, source code below

char z ;
if(Serial1.available())
{
z=Serial1.read();

if(z=='1')
{
digitalWrite(relay, HIGH);
Serial.println("relay opened");

}

please help, source code below

Not all of it, so help is not possible.

Do yo have while (!Serial); in setup()?

Ok i fixed, problem is

while (!Serial);

thank you so much oqibidipo