Android Question Mqtt chat network error

postasat

Member
Licensed User
Hello,

I have a problem using mqtt chat with autodiscovery.

I use a tablet as server and phones as client in a mqtt chat with autodiscovery.
Both are connected in the same wifi network but the wifi network have no internet connection.

If the phone have wifi on and mobile data on, when I start connection, the phone/client can't find the tablet/server and I have success=false in a few time in the "Private Sub client_Connected (Success As Boolean)".

If I turn off the phone mobile data and leave only wifi on, I can connect the phone/client with the tablet/server without problems.

Is it possible to use a phone with mobile data "on" to search and connect with the tablet in a wifi network without internet connection ? How ?

Thank you.
 

Erel

Administrator
Staff member
Licensed User
The problem is that Android prefers the network with the internet connection.

If you are using Android 5+ then you can try this solution:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Wait For (PreferWifiRouting) Complete (Success As Boolean)
   Log(Success)
End Sub

Sub PreferWifiRouting As ResumableSub
   Dim p As Phone
   If p.SdkVersion >= 21 Then
       Dim ctxt As JavaObject
       ctxt.InitializeContext
       Dim builder As JavaObject
       builder.InitializeNewInstance("android.net.NetworkRequest.Builder", Null)
       Dim manager As JavaObject = ctxt.RunMethod("getSystemService", Array("connectivity"))
       builder.RunMethod("addTransportType", Array(1)) 'NetworkCapabilities.TRANSPORT_WIFI
       Dim event As JavaObject
       event.InitializeNewInstance(Application.PackageName & ".main$NetworkCallback", Null)
       manager.RunMethod("requestNetwork", Array(builder.RunMethod("build", Null), event))
       Wait For network_available (Network As Object)
       Log("Network found.")
       Dim cm As JavaObject
       Return cm.InitializeStatic("android.net.ConnectivityManager").RunMethod("setProcessDefaultNetwork", Array(Network))
   End If
   Return False
End Sub

#if Java
public static class NetworkCallback extends android.net.ConnectivityManager.NetworkCallback {
  public void onAvailable(android.net.Network network) {
         processBA.raiseEventFromUI(null, "network_available", network);
  }
}
#End If
It is based on: https://stackoverflow.com/questions/35152417/send-data-through-wifi-no-internet-when-mobile-data-on
 

postasat

Member
Licensed User
Hello,

is it possible programmatically to check mobile data and if it's "on", turn it "off" when app start and turn again "on" when exit ?
(no rooted phone)

Thank you, Erel.
 

postasat

Member
Licensed User
Hello,

is it possible only to check if mobile data are on ?

So I could give a message to user to tun it off.

Thank you.

I think I found the solution:
B4X:
Dim p1 As Phone
    Dim dataon As String
    dataon  = p1.GetDataState
    
    If dataon <> "DISCONNECTED" Then
        Msgbox(stringxx, stringyy)
        Return
    End If
I will Try, Thank you.
 
Last edited:
Top