Android Question connect to mobile connection for HTTPJob while connected to ESP8266

Amateurtje

Member
Licensed User
Longtime User
I replied to an exisiting thread but knowing this forum, it is better to start a new thread.

I have an ESP8266 connected to the Wifi port of the telephone. This ESP8266 does not have an internet connection... (we are in the field with the application). The android app is gathering information from the device connected to the ESP8266.
in the android App, we add some info and we we upload the data with a HTTPjob...

This works well in the test environment where everything is in the same area (the ESP8266 is client)..
In the field, the ESP must be an AccesPoint.. This also works well. The data from the ESP8266 is perfectly downloaded in our app. The problem is in the upload. Although the telephone has a mobile connection, the HTTPjob Probably tries to use the open wifi connection to the ESP and can not execute..

In the answer of the exiting thread, there is a link: Try this: https://www.b4x.com/android/forum/threads/mqtt-chat-network-error.90892/post-574610 . However, this post is to let the application prefer the wifi connection...

In my case, the application must use both but the Mobile data connection for the httpjob...

Can anybody please help me??
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
Hi Erel,

That is a problem while the upload step has te be done about every minute...About the same rate as data comes in from the ESP6288.

Your solution I tried but with contante 0.. I do not understand anything about it (android and java) but I could just see that that had something to do with the type of connection but I did not try 12 ..

I let you know what 12 does when arriving back at programming pc.. But, I really need a solution for my complete system to work..... I have spend already a couple of months on all the building blocks and just this final step (before the releasedate) we run into this quiet unexpectedly... We missed it during the trials while i always assumed that the device would seek internet access..... My mistake, but a big one, so it seems..... We have been discussing for what type of device to use in this step but Android (not windows) is by far the preffered device for many reasons, so we based this part on B4A.

PS, even with just turning the wifi on and off, it still does not work.. The complete program must be restarted for the upload to use the mobile data connection.... I can not let the user do this every minute...
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
Dear Erel,

At the last line of the code he crashes. I used ID=12

B4X:
Sub PreferWifiRouting (id As Int) 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
        builder.RunMethod("addTransportType", Array(id))

Maybe stupid question, but I can not select and copypaste the history screen.. How do you/people do that?

EDIT: Without this script, I started the program without wifi on.. Upload was perfect. Turning on wifi however did not change the fact that downloading (wifi data) did not work...
Now restarting the program a couple of times (with the internetless wifi on), it sticks on the mobile data (and I can not download)

EDIT2: it seems I can start with mobile connection (upload works) switch on the wifi. Use ID =1 for the preffered wifi script and download the data....However, the switch back with the script crashes (id=12) or does not work (ID=0).

EDIT3:
I got the history reply on ID=12:
B4X:
Error occurred on line: 2797 (AvSpeedAutomaticActivity1)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7259)
    at android.view.View.performClickInternal(View.java:7236)
    at android.view.View.access$3600(View.java:801)
    at android.view.View$PerformClick.run(View.java:27896)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7397)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
Caused by: java.lang.IllegalArgumentException: Invalid TransportType 12
    at com.android.internal.util.Preconditions.checkArgument(Preconditions.java:50)
    at android.net.NetworkCapabilities.checkValidTransportType(NetworkCapabilities.java:1687)
    at android.net.NetworkCapabilities.addTransportType(NetworkCapabilities.java:720)
    at android.net.NetworkRequest$Builder.addTransportType(NetworkRequest.java:267)
    ... 22 more
 
Last edited:
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
I was just looking at it that there was another setting:
For example, Value 1 is:
public static final int NET_CAPABILITY_SUPL
public static final int TRANSPORT_WIFI

I go and try.. I am learning...

The problem is that with Value 0 for TRANSPORT CELLULAR he also does not detect a network......I go and add...

EDIT: Now it does nothing anymore. Even after restarting phone. Even:
B4X:
        builder.RunMethod("addTransportType", Array(1))
Does not work anymore.. (with the normal good common network connected to phone)
He does not reach:
B4X:
    Log("Network found.")
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What exactly happens if you turn off wifi?

Switching between the networks in the same process sounds fragile. You should make a test with another app that just connects to the cellular network (or remove the transport type at all and just add the capability). If it will work, the solution will be to start the second app, make the upload and close it. The second app can be started in the background.
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
How can you remove a transport type?

I was also thinking that not removing a setting might be the problem...
 
Last edited:
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
Oke, run it "Empty".. Will try.. I am trying to set up the network change in different app... Then I am going to test to run the apps simultaniously.. I have some troubles now with uploading the app to the phone.. That sometimes happens, And than, after a couple of times restarting etc (and some magic) it works again...
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
in a seperate program, it does not help.... In the communication with the Wifi dapter, cna we not specify in the socket what it should use for network? Evne the name of the wifi or something? That has nothing to do with the local port of the socket, I assume?
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…