Android Question Server connection lost ,ServerSocket+SQL


Licensed User
Longtime User
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:
Sub Class_Globals
    Dim defaultHostIP As String = ""
    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")
    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
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)
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")
         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
        countBuffer = countBuffer + 1
        If emptyBuffer Then
        Else If(countBuffer>bufferToHold) Then
            countBuffer = 0
        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")
End Sub
For any fixes within this class - I am open to suggestions :D


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