Android Question Dynamic variables in Starter Service

samikinikar

Member
Licensed User
Longtime User
I need to store the IP of the DB and the printer during the first run of the app, so that the DB connectivity depends on the IP, I cannot use

Private jdbcUrl As String = "jdbc:jtds:sqlserver://192.168.1.25/db" in starter service,

Private printerip as string ="192.168.1.26:9100"

because the local IP address is not known(192.168.1.25), or whenever there is modem reset, the IP changes. So I need to store the above IP(192.168.1.25) somewhere in text file or DB during the first run.

Can someone please guide me ?
 

OliverA

Expert
Licensed User
Longtime User
modem reset, the IP changes
1) Modem or router? Or is it a combo. Let's assume it's a router. You'll have to go into the router and assign static IP's.
or
2) If the setup is always the same (an SQL server and a printer), you could run Window Server's DNS service, give proper names to your Windows Server and printer and then make the Windows server the primary DNS server for your devices on the network (BTW, in a Windows Server environment, that's how it is supposed to be - Your Domain Controller should be the primary DNS server). BTW, that means the Windows server should have a static IP address (it's address should not change at every server reboot/modem(router) reboot)
 
Upvote 0

samikinikar

Member
Licensed User
Longtime User
Thank you so much for the update.

Yes .... that option is related to the hardware/router and that it already implemented. The issue is, app used in multiple locations, so setting each time the IP address hardcoded in the app and compile back is a bit difficult. Some networks are using a subnet mask of 192.168.1.1, and few 192.168.0.1 and so on, so it becomes very difficult to have a common IP range, So I was looking for an alternative like during the first run, setup the parameters like IP, printer name or port. once stored they can use the app at any location with local Mysql/mssql db.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i would create a small json with the needed data (maybe with encrypted data (and base64 encoded to savely transport the file).
I would put the file on my webserver (or B4J Server-app).
In starter i would download the file, setting the global vars based on the json content and only if the data is available using any db acess.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
also a basic example if any would be appreciated or link to such methods ?

What do you need help with? Downloading a json file from your server?
Parsing the json File and setting up Global variables based on the Data?
For any of them you can find a lot of threads in the Forum.
 
Last edited:
Upvote 0

samikinikar

Member
Licensed User
Longtime User
Thank you everyone who tried to help me... its was so simple I could do it with the help of great examples... here is my code.


B4X:
Sub Activity_Create(FirstTime As Boolean)
If File.Exists(testfolder, "dip.txt") = False Then
            File.WriteString(testfolder,"dip.txt","")
            StartActivity("setip")
Else       
jdbcurlnew=File.ReadString(testfolder,"dip.txt")       
End If
End Sub

Start Activity
B4X:
Sub Connect As ResumableSub
    Dim testfolder As String = rpnew.GetSafeDirDefaultExternal("test")
    Dim ip As String=File.ReadString(testfolder,"dip.txt")
    
    Dim jdbcUrlold As String = "jdbc:jtds:sqlserver://"
    Dim jdbcurlnew As String=jdbcUrlold&ip&"/mydb"
    mysql.InitializeAsync("mysql", driver, jdbcurlnew, Username, Password)
    Wait For MySQL_Ready (Success As Boolean)
    If Success = False Then
        Log("Check unfiltered logs for JDBC errors.")
    End If
    Return Success
End Sub


“I am thankful for all of those who said NO to me. It’s because of them I’m doing it myself.” ~Albert Einstein :)
 
Upvote 0
Top