Android Question Question about WifiNetworkSpecifier + NetworkRequest

Filippo

Expert
Licensed User
Longtime User
Hi,

I am using this code snippet from Erel.
The connection to the WiFi network (XIAO-ESP32C3) works very well and quickly.
If the WiFi fails, e.g., is switched off, my app receives the message/event about 15 seconds later.
Question: Is there a way to receive this message/event faster?
Or should I use a timer to check whether the connection is established?


Thank you very much.
Filippo
 

drgottjr

Expert
Licensed User
Longtime User
you want onLost, not onUnavailable. that's why you're getting the delay. they are different methods with different purposes.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
you want onLost, not onUnavailable. that's why you're getting the delay. they are different methods with different purposes.
Hi @drgottjr ,
thank you for your reply.
Unfortunately, the “onLost” event is not triggered any faster than “onUnavailable.”


Here is my modified code:
B4X:
#if Java
public static class MyNetworkCallback extends android.net.ConnectivityManager.NetworkCallback {
    private final BA ba;
    public MyNetworkCallback(B4AClass me) {
        this.ba = me.getBA();
    }
    @Override
    public void onAvailable(android.net.Network network) {
        super.onAvailable(network);
        BA.Log("Network available: " + network);
        ba.raiseEvent(this, "network_state", true, network);
    }
    @Override
    public void onLost(android.net.Network network) {
        super.onLost(network);
        BA.Log("Network onLost");
        ba.raiseEvent(this, "network_state", false, null);
    }
}
#End If
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
for me, disconnect is instaneous. try the attached. i hadn't used it for a long time, but i just tried it on android 12 and 16 devices. put the .jar and .xml in your add'l libraries folder and run the .apk. i don't know way your project doesn't respond the way it should.
 

Attachments

  • conn.zip
    137.2 KB · Views: 6
Upvote 0
Top