iOS Question [SOLVED] iPing not working

Sergio Haurat

Active Member
Licensed User
Longtime User
I am trying to implement iPing from this post from @Erel .

I don't know if a special permission is needed or what the problem is. If I solve this issue, I will have the B4X Ping ready. I am attaching the complete project.

Please note that it is necessary to copy the libraries attached in the iPing post to the additional libraries folder.

Project updated with the latest modifications 6/25/2024 10:40am GMT-3.

Android result
1719244555655.png
 

Attachments

  • SP Ping.zip
    13.1 KB · Views: 82
Last edited:

Sergio Haurat

Active Member
Licensed User
Longtime User
No reason to post the library again.
Okay, I just tried to help so you wouldn't have to search for them; I have already removed them from the post #1. In your example, it works, but it is on the same B4XPage. In my case, the variable declaration is in a class to integrate it into the same methods of a project in B4A.
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
Dear @Erel, I just attached the latest version of my project in post #1. I still can't get iPing to work within a class. I am rewriting the B4J segment thanks to a contribution from @Mariano Ismael Castro

Question: Does #IF JAVA work in B4I?

Thank you
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
I have tried all the possible ways that I understand in the examples and after the Wait For, it returns to the sub that called it. I already use Wait For in other projects, it always waits for it to finish and returns a result.
Below is the summary of the last thing I did trying to simulate the example link. The complete project in post #1

B4X:
'B4XMainPage
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private spPing As Ping
    Private Button1 As Button
End Sub

Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
    spPing.Initialize(Me, "SPPing")
End Sub

Private Sub SPPing_PingStarted
    Log("Ping started")
End Sub

Private Sub SPPing_PingFinished
    Log("Ping finished")
End Sub

Private Sub SPPing_PingError(Msg As String)
    Log(Msg)
End Sub

Private Sub Button1_Click
    spPing.Destination = "google.com"
    spPing.Attempts = 1
    spPing.Timeout = 200
    spPing.Start
    If spPing.pingResult.Success Then
      'More code here
    End If
End Sub

Ping Class:
Sub Class_Globals
    Type tpePing (Success As Boolean, toHost As String, toIP As String, fromDest As String, mapResponse As Map, mapStatistics As Map)
    Public pingResult As tpePing
    Private Pinger As SimplePing
End Sub

Public Sub Start()
    #If B4A
    pingResult = B4APing
    #Else If B4J
    pingResult = B4JPing
    #Else If B4I
    pingResult = B4IPing
    #End If
End Sub

Private Sub B4IPing() As tpePing
    pingResult.toHost = Destination
    iOSDoPing
    Return pingResult
End Sub

Private Sub iOSDoPing()
  Dim PingerResult As ResumableSub = PingerDoPing
    If PingerResult.IsInitialized And PingerResult.Completed Then
        For i = 1 To Attempts
            Pinger.Send
            Wait For Pinger_PacketSent (SequenceNumber As Int, Success As Boolean)
            Log("sent: " & SequenceNumber & ": " & Success)
            Sleep(Timeout)
        Next
        icmp_seq.Put(SequenceNumber.As (Byte), DateTime.Now - dblTStart)
    Else
        If xui.SubExists(mCallback, mEventName & "_PingError", 1) Then
            CallSub2(mCallback, mEventName & "_PingError", LastException)
        End If
    End If
    Pinger.Stop
    If xui.SubExists(mCallback, mEventName & "_PingFinished", 0) Then
        CallSub(mCallback, mEventName & "_PingFinished")
    End If
End Sub

Private Sub PingerDoPing() As ResumableSub
    Pinger.Initialize("iOSPing", Destination)
    Pinger.Start
    Wait For Pinger_Start (Success As Boolean)
    'Execution never continues after this line, even with the modification I made based on the shared link of how Wait For/ResumableSub works.
    pingResult.Success = Success
    Return Success
End Sub

I'm not really understanding where my error is, as I said previously in MsgBox2Async, HttpJob and others, the same thing doesn't happen to me, it works perfectly
 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
Well, I think I found the difference between the iPing example and what I'm developing.

In @Erel's iPing example, a button variable type inside a B4XPage initializes iPing and uses "Wait For" on _Start, waiting for a result, then enters a for/next loop and uses "Wait For" on _PacketSent , returning the results of the packet sending attempts

In my example, a button type variable inside a B4XPage calls a Sub called Start, inside a class called Ping.
The Sub Start, depending on "#IF B4A, B4I or B4J", calls another Sub within the class, which processes the ping command, fills a public variable of the class called pingResult with the collected information and fires a completion event of the task.

INI:
'Works
Sub
     call Wait For and wait for the result

'This does not work
Sub
     Sub
         Sub
             call Wait For and wait for the result

'This works?
Sub
     Sub ResumableSub
         Sub ResumableSub
             call Wait For and wait for the result

Another solution that I think may work would be
B4X:
'B4X Page
Private Sub Button1_Click
  varPing.Start
End Sub

'Class Ping

#IF B4A
Public Sub Start()
  'Code
End Sub
#ELSE IF B4I
Public Sub Start()
  'Code
End Sub
#ELSE IF B4J
Public Sub Start()
  'Code
End Sub
#END IF

I am right?
 
Last edited:
Upvote 0
Top