B4J Question Check WiFi connection in realtime

martinstealth

Member
Licensed User
Hello,

I'm trying to check the internet connection in real time but I haven't figured out how to do it. I've searched the forum, looked at some posts but they were all for b4a. I also tried some of them but they didn't work. Any help is appreciated.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
What do you want to do, measure upload and download speed?

Here is some code. You will need three files which I cannot upload: 10mb.zip, 100mb.zip and 256mb.zip

You can make them yourself or download them here: Link

B4X:
AppType=JavaFX
Build1=Default,b4j.example
File1=100MB.zip
File2=10MB.zip
File3=256MB.zip
File4=Layout1.bjl
FileGroup1=Default Group
FileGroup2=Default Group
FileGroup3=Default Group
FileGroup4=Default Group
Group=Default Group
Library1=jcore
Library2=jfx
Library3=jnet
NumberOfFiles=4
NumberOfLibraries=3
NumberOfModules=0
Version=7.51
@EndOfDesignText@
#Region  Project Attributes
    #MainFormWidth: 400
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private FTP As FTP                                                                'needs JNet library
    Public FileDirectory, FileName, ServerFTPPath, ServerFTPAddress, ServerFTPUser,ServerFTPPw As String
    Private btn_Start As Button
   
    Dim StartTime, TestTime As Long
    Private btn_Quit As Button
    Private ComboBox1 As ComboBox
    Private Label1 As Label
    Private lbl_Progress As Label
    Private lbl_UploadResult As Label
    Private FileSize As Double
    Dim Writer As TextWriter
    Private lbl_DownloadResult As Label
    Dim d, t As String
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
   
    ServerFTPAddress="speedtest.tele2.net"
    ServerFTPUser="anonymous"
    ServerFTPPw=""
    ServerFTPPath="upload/" ' note: simple directory UNDER the FTP path where you get to when you login with the user. Example: After login you are in "MyServer/somefolder" so with "xxxxx/" you store it in "MyServer/somefolder/xxxxx/"
   
    ComboBox1.Items.Add("10MB")
    ComboBox1.Items.Add("100MB")
    ComboBox1.Items.Add("256MB")
   
    lbl_Progress.Text=""
    lbl_UploadResult.Text=""
   
    If File.IsDirectory("d:/","speedtest/") = False Then
        File.MakeDir("d:/","speedtest/")
        Log("Directory created!")
    End If
    FileDirectory="d:/speedtest/"
   
    Writer.Initialize(File.OpenOutput(FileDirectory, "SpeedTestResults.txt", True))
    DateTime.DateFormat = "yyyy/MM/dd"
    DateTime.TimeFormat="HH:mm"
   
End Sub

Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
    Dim s As String
    s = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
    If FileSize > 0 Then
        s = s & " out of " & (FileSize*1000) & "KB"
    End If
   
    'Log(s)
    lbl_Progress.Text=s
End Sub

Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
    Log("FTP-Upload: OK")
    If Success = False Then Log(LastException.Message)
    FTP.Close
    TestTime=DateTime.Now-StartTime
    TestTime = (TestTime Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    'Log("Time taken: " & NumberFormat(TestTime, 2, 0) & " seconds")
    lbl_Progress.Text=FileSize & "MB uploaded in " & NumberFormat(TestTime, 2, 0) & " seconds"
   
    Dim UpLoad As Double
    UpLoad=FileSize*8/TestTime
    'Log("UpLoad: " & UpLoad)
    lbl_UploadResult.Text="Upload Speed: " & NumberFormat(UpLoad, 0, 3) & " Mbit/s"
   
    d = DateTime.Date(DateTime.Now)
    t = DateTime.Time(DateTime.Now)
   
    Writer.WriteLine(d & " " & t &": Testfile: " & FileSize & "MB, uploaded in " & TestTime & " seconds. Speed = " & NumberFormat(UpLoad, 0, 3) & " Mbit/s")
   
   
    ' Reset variables for download
    ServerFTPUser="anonymous"
    ServerFTPPw=""
    ServerFTPPath=""
    FileSize=10.24
    FileName="10MB.zip"
 
    FTP.Initialize("FTP", "speedtest.tele2.net", 21, ServerFTPUser, ServerFTPPw)
    StartTime=DateTime.Now
    FTP.DownloadFile( ServerFTPPath & FileName, False, FileDirectory, "download.zip")
End Sub

Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    Dim s As String
    s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
    If FileSize > 0 Then
        s = s & " out of " & (FileSize*1000) & "KB"
    End If
   
    'Log(s)
    lbl_Progress.Text=s
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Log("FTP-Download: OK")
    If Success = False Then Log(LastException.Message)
    FTP.Close
    TestTime=DateTime.Now-StartTime
    TestTime = (TestTime Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    'Log("Time taken: " & NumberFormat(TestTime, 2, 0) & " seconds")
    lbl_Progress.Text=FileSize & "MB downloaded in " & NumberFormat(TestTime, 2, 0) & " seconds"
    lbl_Progress.WrapText=True
   
    Dim DLoad As Double
    DLoad=FileSize*8/TestTime
    'Log("UpLoad: " & UpLoad)
    lbl_DownloadResult.Text="Download Speed: " & NumberFormat(DLoad, 0, 3) & " Mbit/s"
   
    d = DateTime.Date(DateTime.Now)
    t = DateTime.Time(DateTime.Now)
   
    Writer.WriteLine(d & " " & t &": Testfile: " & FileSize & "MB, downloaded in " & TestTime & " seconds. Speed = " & NumberFormat(DLoad, 0, 3) & " Mbit/s")
    Writer.Close
   
    File.Delete(FileDirectory, "download.zip")
End Sub




Sub btn_Start_MouseClicked (EventData As MouseEvent)
    FTP.Initialize("FTP", ServerFTPAddress, 21, ServerFTPUser, ServerFTPPw)
    FTP.PassiveMode=True
                                                            'start timer
    DateTime.TimeFormat="HH:mm:ss"
    StartTime=DateTime.Now      
   
   
    FTP.UploadFile(FileDirectory, FileName, False, ServerFTPPath & FileName)
End Sub

Sub ComboBox1_SelectedIndexChanged(Index As Int, Value As Object)
    FileName=Value & ".zip"
    Select Index
        Case 0
            FileSize=10.24
        Case 1
            FileSize=102.4
        Case 2
            FileSize=256      
    End Select
   
    'Log(FileSize)
   
End Sub

Sub btn_Quit_MouseClicked (EventData As MouseEvent)
    FTP.Close
    ExitApplication
End Sub
 

Attachments

  • SpeedTest.zip
    3.7 KB · Views: 331
Last edited:
Upvote 0
Top