My app connect to a pc sever using sockets, and every 10 seconds sends a message, as "hello pc, I am connected"
I connect a socket
Dim tcpClient As Socket
[cut]
tcpClient.Connect(NameServerTCP.Text,NameServerTCPport.text,3000)
and send as
Dim tcpFlusso As AsyncStreams
[cut]
tcpFlusso.Write(MyMessage.GetBytes("UTF8"))
- it works
My app uses b4Xpages to work in background and keepalive to stay active
as:
(code taken from the gps locator example)
in my b4Xpage:
now:
this works good,
BUT only IF the app is in foreground (power supply connected is ininfluent)
If I send the app in background... after one minutes the device switch off the screen and then, after few minutes, my app disconnects - the ip address is still ok,but my app fails the message send and the re-connection request
The app "is still working", as it write logs and the logs are updated correctly... but the IP socket is not working, it doen't connect so it doesn't send messages
as soon as I "wake up" the device (e.g. double touch on the screen) and put my app in foregroud, it connect and start sending messages again
looking at the logs, I see that the IP addess was not lost, the wifi wasn't disconnected, so the socket should work
I am stuck here
any suggestion / solution?
I connect a socket
Dim tcpClient As Socket
[cut]
tcpClient.Connect(NameServerTCP.Text,NameServerTCPport.text,3000)
and send as
Dim tcpFlusso As AsyncStreams
[cut]
tcpFlusso.Write(MyMessage.GetBytes("UTF8"))
- it works
My app uses b4Xpages to work in background and keepalive to stay active
as:
(code taken from the gps locator example)
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
'KEEPALIVE SERVICE
'
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private nid As Int = 1
Private lock As PhoneWakeState
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
lock.KeepAlive(True)
lock.PartialLock
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("..."))
End Sub
Sub Service_Destroy
lock.ReleasePartialLock
End Sub
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Ping server", Body, Main)
Return notification
End Sub
in my b4Xpage:
B4X:
' in b4Xpage
Sub Activity_Resume
B4XPages.Delegate.Activity_Resume
'https://www.b4x.com/android/forum/threads/how-do-i-prevent-screen-timeout.11471/
Dim pw As PhoneWakeState
'pw.ReleaseKeepAlive ' NO, this will not work! my app will disconnect (the socket will not work) after 5 minutes
' I HAVE TO keep it active
pw.KeepAlive(True)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
B4XPages.Delegate.Activity_Pause
'https://www.b4x.com/android/forum/threads/how-do-i-prevent-screen-timeout.11471/
Dim pw As PhoneWakeState
pw.KeepAlive(True)
End Sub
now:
this works good,
BUT only IF the app is in foreground (power supply connected is ininfluent)
If I send the app in background... after one minutes the device switch off the screen and then, after few minutes, my app disconnects - the ip address is still ok,but my app fails the message send and the re-connection request
The app "is still working", as it write logs and the logs are updated correctly... but the IP socket is not working, it doen't connect so it doesn't send messages
as soon as I "wake up" the device (e.g. double touch on the screen) and put my app in foregroud, it connect and start sending messages again
looking at the logs, I see that the IP addess was not lost, the wifi wasn't disconnected, so the socket should work
I am stuck here
any suggestion / solution?
Last edited: