Android Question serial.connect Delay ?

SoyEli

Active Member
Licensed User
Longtime User
Hello:
Is it possible to have a delay on serial.connected ?
I tried "callsubdelayed", but it does not work


Thank you:

Exp:
Serial1.Connect3(##########,1)
Delay before next line or until return true or false
Do something with true or false............
end sub

Private Sub Serial1_Connected (Success As Boolean) As Boolean
If Success Then
Log("Bt_Connected ")
Return True
Else
Log("Bt_Connected")
Return False
End If
End Sub
 

SoyEli

Active Member
Licensed User
Longtime User
Thank you:

I need to know if connected or not before going to the next line:
Serial1.Connect3(##########,1)
Wait for true or false
Do something with true\false
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
It's better you change a bit the logic of your code.
Now your logic is connect and stay put until connection established, all in the same sub.

It is much better something like this
B4X:
Sub OpenSerial
   Serial1.initialize("serial1")
   Serial1.connect(......)
End sub

Sub serial1_connected(success as Boolean) as Boolean
   If success then
       Call GoOnWithYouHaveToDo
       'Or callSubDelayed if the serial I/O code is in a service module
       'and you're calling a sub in an activity (normally is better have
       'serial code in a service)
   Else
       Log("Connection problems")
   End if 
End sub
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Thank you for your response:

I tried just like above but the next line fires before the "GoOnWithYouHaveToDo"
then the next line.

Sub OpenSerial
Dim Y as boolean
Serial1.initialize("serial1")
Serial1.connect(......)
log("Connected or not")' I need this to wait until connected or not

End sub
Sub serial1_connected(success asBoolean) as Boolean
If success then
Call GoOnWithYouHaveToDo
Else
Log("Connection problems")
End if
End sub
 
Last edited:
Upvote 0

Straker

Active Member
Licensed User
Longtime User
You don't need any code after serial1.connect
Look at my code. There is an End Sub after the connect.
You can place your log in the sub Serial1_Connected

Think this way:
The code performs .connect
After that line the code ends (End Sub). Your program at that point do nothing. Is just idling. When the connection is established your code restart automatically from the sub Serial1_Connected. If the connection is ok, then success=true! otherwise success will be false.

You can think your serial1 as a button. When you show a button on the screen you don't have to wait the user to press the button. You don't need a code 'wait until someone press'. When (and if) the user press the button your code will automatically jump to the Sub button_click. No need to wait.

With your serial1 is even better. There is also a timeout. The _Connected event will fire when the connection has been established (success=True) or has bene refused or ti ed out (success=false).

If don't understand, just ask for more.
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
You can also imagine that the code, after Serial1.connect simply jumps to the line 'if success then' in the sub seial1_connect
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
I remember I encountered this problem but no the details.. I solved it in unusual way by calling an external delay sub.. (has to be external and does nothing but waste of time) about 1 second delay.
 
Upvote 0
Top