Android Question socket, please don't sleep (keepalive)

vangogh

Active Member
Licensed User
Longtime User
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)

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:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

vangogh

Active Member
Licensed User
Longtime User
Try to acquire a wifi lock: https://www.b4x.com/android/forum/t...k-with-the-following-rules.128726/post-807643

The OS will not let your app "waste" too much resources while it is in the background. You will not be able to maintain a network connection for a long time. The recommended approach is to use push notifications.

wifi lock e socket lock actived --- no effect :-( :-(
tried to close and reopen the connection ecvery minute - no effect
tried to send a message every second (not every 10, but 1) - MAYBE OK - I have to test further


> "The recommended approach is to use push notifications."

I don't know what is this... what do you mean?
 
Last edited:
Upvote 0
Top