Android Question Server connection lost ,ServerSocket+SQL

beeblomarv

Member
Licensed User
Longtime User
Hello,
I am gonna put this really simple here:
1) I get data from bluetooth,
2) I save that data (SD Card) + send it to server (Running on PC, programmed in Java). This happens simultaneously. So in short the database (sqlite form) within smartphone and on server are almost identical.
3) THE PROBLEM : When connection to the server is lost (Connection lost means : No Internet ), how do I restore the data that's being sent to smartphone via bluetooth module from microcontroller? It's precisely a networking problem !!!
What is the solution to this problem- I don't want to lose any data packets and instead want to save them so as soon as the server connection is restored I get those packets

Here is what my Networkconnectionmanger class looks alike:
B4X:
Sub Class_Globals
    Dim defaultHostIP As String = "172.30.2.103"
    Dim defaultHostPort As Int = 40000
    Dim selectedHostIP As String
    Dim selectedHostPort As Int
    Dim defaultServerPort As Int = 40000
    Dim bufferToHold As Int = 100
    Private countBuffer As Int
    Private socket1 As Socket
    Private serverSocket1 As ServerSocket
    Private sb As StringBuilder
    Private sf As StringFunctions
    Private os As OutputStream
End Sub
'the flag determines whether the phone shall be used only as client or also as server
Public Sub Initialize(asClientOnly As Boolean)
    If Not(asClientOnly) Then serverSocket1.Initialize(defaultServerPort,"server")
    socket1.Initialize("hostFound")
    If File.Exists(File.DirInternal,"defaults.txt") Then
        Dim s As String=File.ReadString(File.DirInternal,"defaults.txt")
        defaultHostIP = sf.Split(s,":").Get(0)
        defaultHostPort = sf.Split(s,":").Get(1)
    End If
    sb.Initialize
    sf.Initialize
End Sub

Public Sub isInternetAvailable As Boolean
Return Main.myDevice.GetDataState=="CONNECTED" Or Main.myDevice.GetSettings("wifi_on")<>0
End Sub
'check whether the scocket has connected to a server
Sub hostFound As Boolean
    Return socket1.Connected
End Sub
'connect socket to a server of given host:port; timeout=0 disable connection timeout
Sub connectTo(host As String,port As Int,timeout As Int)
    ToastMessageShow("trying to connect to "&host&":"&port,True)
    socket1.Connect(host,port,timeout)
End Sub
'event triggered from a incoming server connection demand
Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
End Sub
'triggered when the socket connects to a server
Sub hostFound_Connected(Successful As Boolean)
     If Successful = False Then
        Msgbox(LastException.Message, "Error while connecting")
        Return
    Else
         ToastMessageShow("Connected to "&selectedHostIP&":"&selectedHostPort,False)
    End If
    os = socket1.OutputStream
End Sub
'sends a string as a new line to the connected server
Sub sendLineToHost(toSend As String,emptyBuffer As Boolean)
    If os.IsInitialized Then
        toSend = toSend&CRLF
        os.WriteBytes(toSend.GetBytes("UTF-8"),0,toSend.GetBytes("UTF-8").Length)
        countBuffer = countBuffer + 1
        If emptyBuffer Then
            os.Flush
        Else If(countBuffer>bufferToHold) Then
            countBuffer = 0
            os.Flush
        End If
    End If
End Sub
'launches the android.settings.WIFI_SETTINGS activity
Sub popUpWifiSettings
    Dim i As Intent
    i.Initialize("android.settings.WIFI_SETTINGS", "")
    ProgressDialogShow("Activating WiFi")
    StartActivity(i)
    ProgressDialogHide
End Sub
[/CODE]
For any fixes within this class - I am open to suggestions :D
 

beeblomarv

Member
Licensed User
Longtime User
I have seen the cloudkvs code for b4a - doesn't it puts the data in a list and then puts in Queue and empty it as soon as the connection happens via timer? Did i miss anything here?
 
Upvote 0
Top