Android Question wifi disconnected when screen off

Mrphone

Member
hi
I'm created a program that connects to a Wi-Fi using the "network" library , Everything works fine, but when the mobile screen turns off, the connection is lost
What should be done so that when the mobile screen is turned off, the connection is still in place and not disconnected?

Code:
#Region  Service Attributes
    #StartAtBoot: true
#End Region

Sub Process_Globals
    Dim WiFi_Socket As Socket
    Dim TcpStreams As AsyncStreams
    Dim ServerSocket1 As ServerSocket
    Dim ServerIp As String = "192.168.4.1" 'IP Access point
    Dim Port As Int = 80 'port
End Sub

Sub Service_Start (StartingIntent As Intent)   
    WiFi_Socket.Initialize("WiFi_Socket")
    WiFi_Socket.Connect(ServerIp,Port,0)
    ServerSocket1.Initialize(0,"")
End Sub

Sub WiFi_Socket_Connected (Connected As Boolean)   
    If Connected = True Then
        'connected'
        TcpStreams.Initialize(WiFi_Socket.InputStream,WiFi_Socket.OutputStream,"tcpStreams")
    Else
        'disconnected'
    End If      
End Sub

Sub TcpStreams_NewData (Buffer() As Byte)
    .
    .
    .   
End Sub

Sub Service_Destroy

End Sub
 

f0raster0

Well-Known Member
Licensed User
Longtime User
don't know if it helps, but if it's not a problem for you to keep the screen on then:
B4X:
Sub Process_Globals
    Dim screen1 As PhoneWakeState   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    screen1.ReleaseKeepAlive
End Sub
 
Upvote 0

Mrphone

Member
Thanks @f0raster0

Yes, this is a problem because the connection may take too long and the mobile screen may turn off for any reason.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…