Android Question TCP/IP Connection without Interrupt

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Hi all,
I am looking for an example for TCP/IP Connection without Interrupt.

I have try some exanples what I found here but the connection is not stabil.

I have add also keepAlive but without success.
StartService every 10 sec. And set Foreground set Notification but doesn’t works.

Who can help me?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
if keep-alive is supported by the other side, then a connection stay forever until one side close it for any reason.
exceptional case: unplug cable, power cut , phone go to sleep mode, phone change from one access point to other or phone change from wlan to mobile data, service at phone was killed by energy-saving mode (optional in newer android), ...

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive

a indicator for a running foreground service is the notification icon or debug logs.

Example with Phone Events in Starter Service:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public PhoneEvent1 As PhoneEvents
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

    'https://www.b4x.com/android/help/phone.html#phoneevents
    PhoneEvent1.Initialize("PhoneEvent")
  
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub PhoneEvent_ConnectivityChanged(NetworkType As String, State As String, Intent As Intent)
  
    'NetworkType - WIFI or MOBILE.
    'State - One of the following values: CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN.
  
    If State = "CONNECTED" Then
    Else
        ToastMessageShow("no network connection",True)
        Log(NetworkType)
        Log(State)
      
        'umleiten auf anderes Activity?
      
    End If
  
End Sub
 
Last edited:
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
if keep-alive is supported by the other side, then a connection stay forever until one side close it for any reason.
phone go to sleep mode
I think for this issue, I use PhoneWakeState

B4X:
Sub Process_Globals
   
    Private lock As PhoneWakeState
   
End Sub

Sub Service_Create
    lock.PartialLock            'damit es auch bei Standby die Verbindung nicht unterbrochen wird.
End Sub

Sub Service_Destroy
    lock.ReleasePartialLock
    Service.StopAutomaticForeground
    CancelScheduledService(Me)
End Sub
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i used this for a permanent service
B4X:
Sub Service_Start(StartingIntent As Intent)
  
    Log("Connection.Service_Start")

    Dim n As Notification
    n.Initialize
    n.Icon="icon"
    n.Sound=False
    n.Light = False
    n.Vibrate = False
    n.SetInfo("Alarm","Listening for a Alarm Message",Main)
    Service.StartForeground(1,n)

    PhoneWakeState1.PartialLock

    'kommt schon über das phone event Connect
  
    PhoneEvents1.Initialize("PhoneEvents1")
  
End Sub

i think this rows in red make no sense because the service is running, but make sure the energy saving settings at your device did not kill it.
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(1, CreateNotification("..."))
StartServiceAt(Me, DateTime.Now + 10 * 10000, True)
End Sub

Sub Service_Destroy
lock.ReleasePartialLock
Service.StopAutomaticForeground
CancelScheduledService(Me)

End Sub

in Starter Service i started my Connection Service or it starts itself with #StartAtBoot: True at Service Attributes
B4X:
Sub Service_Start(StartingIntent As Intent)
  
    Log("Starter.Service_Start")
      
    StartService(Connection)
  
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Is there a list of what possible errors the Astream_Error, and Astream_Terminated can deliver?
No. You can check Google Documentation. Maybe you find one.

But it does not matter. What matters is that an Event with the error is raised and you need to react on it.
 
Upvote 0
Top