I'm not sure I understand your problem. If you're setting the boolean in the connected and disconnected events then it doesn't matter how the connection is created or terminated the boolean value should represent the current connection state.
There are times when those events may not fire, so in my "send" methods I always try/catch and if it fails I call the classes ".Disconnect" method which also sets the boolean to false. Is this what you mean?
Hi, first of all I wanted to thank you for trying to help me. Secondly, sorry for my bad English.
I will explain everything. I made a machine and who controls this machine is an esp3 with grbl. I connect to esp32 via bluetooth and send a command and it responds with an OK when it has finished executing the command.
But I can only send commands if the connection is active and I don't know how to know if it's active because the serial class doesn't return the status of the connection. It could also be that I didn't understand how a serial connection works, I'm starting now with this subject.
If you can explain to me or send me an example of something that shows how this connection dynamic works, I would be very grateful.
Thank you very much
Fernando