B4A Library MLwifi Library Updated to v4.00

Attached Java Source Code

MLWifi.zip - couldn't attach the entire Android Studio project as even zipped it was too big (900 KB) for the forum limits. If you want the entire project, private message me with your email address & I'll send it to you.

Library updated to v4.40

Added @Events so the callbacks can be auto-generated by the B4A IDE.

Library updated to v4.40

Added WifiBSSID function. Returns a string with the currently connected AP BSSID. Note that Wifi_ConnectionResult & isonline_pingdone don't require the EventName prefix.

Library updated to v4.30

Minor code tweaks & tidy up. Tested saveWifiAP, connectWifiAP, reconnectWifiAP & disconnectWifiAP on Android 4.4.2, 9 & 10.

Library updated to v4.20

Bug Fixes / Improvements:
* Added support for Android Pie.
* Improved connectivity when using saveWifiAP with Android Q+.

Library updated to v4.10

Added:

reconnectWifiAP: (reconnects to a saved Wifi network)

Parameters:
ssid - the name of the Wifi Network
timeout - fail if not connected within timeout (Ms)

Raises Wifi_ConnectionResult event on success or failure

NOTE FOR SDK 29+: Will only work if the network being connected to is not secured.
You can connect to the previously connected network by calling disconnectWifiAP
or a specific network by calling connectWifiAP with the correct parameters.

Example:
B4X:
Private Sub connectToWifi
    Private wifi As MLwifi
    'reconnect to a Wifi network saved on this device
    wifi.reconnectWifiAP("wifissid", 30000)
End Sub

Public Sub Wifi_ConnectionResult(success As Boolean)
    If success then
        'Connected - set up sockets, etc...
    Else
        'Couldn't connect...
    End If
End Sub

Bug Fixes:

isWifiConnected now correctly reports the connection state (true or false).

v4.00

connectWifiAP:
(connects to an existing Wifi Network)

Parameters:
ssid - the name of the Wifi Network
security = 0 = Open (SECURITY_OPEN), 1 = WEP (SECURITY_WEP), 2 = WPA-2/PSK (SECURITY_WPA2PSK)
password - the password or "" if the network is open
timeout - fail if not connected within timeout (Ms)

Raises Wifi_ConnectionResult event on success or failure

Example:
B4X:
Private Sub connectToWifi
    Private wifi As MLwifi

    'To connect to an open network
    wifi.connectWifiAP("wifissid", wifi.SECURITY_OPEN, "", 30000)
    'To connect to a WPA-2 secured network
    wifi.connectWifiAP("wifissid", wifi.SECURITY_WPA2PSK, "password", 30000)
End Sub

Public Sub Wifi_ConnectionResult(success As Boolean)
    If success then
        'Connected - set up sockets, etc...
    Else
        'Couldn't connect...
    End If
End Sub

saveWifiAP: (adds a Wifi network, then optionally connects)

Parameters:
ssid - the name of the Wifi Network
security = 0 = Open (SECURITY_OPEN), 1 = WEP (SECURITY_WEP), 2 = WPA-2/PSK (SECURITY_WPA2PSK)
password - the password or "" if the network is open
connect - ignored for Android Q+ (will always connect)
timeout - fail if not connected within timeout (ms)

Raises Wifi_ConnectionResult event on success or failure

Example:
B4X:
Private Sub connectToWifi
    Private wifi As MLwifi

    'To save & connect to an open network
    wifi.saveWifiAP("wifissid", wifi.SECURITY_OPEN, "", True, 30000)
    'To save & connect to a WPA-2 secured network
    wifi.saveWifiAP("wifissid", wifi.SECURITY_WPA2PSK, "password", True, 30000)
End Sub

Public Sub Wifi_ConnectionResult(success As Boolean)
    If success then
        'Connected - set up sockets, etc...
    Else
        'Couldn't connect...
    End If
End Sub

disconnectWifiAP: (disconnects from a Wifi network)

Usage:
B4X:
    Private disconnected As Boolean = MLwifi.disconnectWifiAP

Note that the connectWifiAP & saveWifiAP functions no longer return a Boolean. They now raise the Wifi_ConnectionResult(success As Boolean) callback event.

I haven't made any changes to any other library functions, nor have I tested them - so if there are functions that aren't working correctly, you'll have to let me know & I'll take a look at them. I also haven't tested the functions I did change/add beyond a couple of use cases (connecting to an ESP8266 Access Point & connecting to my home Wifi), so if there are situations that it doesn't work correctly in, you'll have to let me know.

Also note that there are some Android Q+ Wifi behaviors that are quite different to before, so I suggest you take a look at the android.net.wifi reference to give you a better idea of how the deprecated functions have been re-implemented.

- Colin.
 

Attachments

  • MLwifi400-v4.41.zip
    26 KB · Views: 1,342
  • MLwifi.zip
    8.6 KB · Views: 517
Last edited:

Acuario

Active Member
Licensed User
Longtime User
I'm using the MLWifi400 library to switch WiFi connections and have now got that working correctly.
Is it possible to switch the connections silently, i.e. without the confirmation dialog (with its timeout)?
If the WiFi network I want to connect to is available then I want to connect to it - I don't want to have to confirm the connection.
This is part of an automatic configuration process for an ESP8266 and I want as little user interaction as possible.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I'm using the MLWifi400 library to switch WiFi connections and have now got that working correctly.
Is it possible to switch the connections silently, i.e. without the confirmation dialog (with its timeout)?
If the WiFi network I want to connect to is available then I want to connect to it - I don't want to have to confirm the connection.
This is part of an automatic configuration process for an ESP8266 and I want as little user interaction as possible.

It depends on the version of Android running on the device trying to connect. I'd suggest you have a read of this thread from the start (post #16 might be relevant to your question) & then see if you still have questions. The short answer is that if you're trying to connect from a device running Q+, you can't do it silently.

- Colin.
 

Acuario

Active Member
Licensed User
Longtime User
Ok, so it looks like not then.. a pity - I don't know how they achieve it in the ESP touch library but there is no confirmation needed there - I need to dig into their code a bit more to see if they are connecting to an AP.

Anyway, that aside, I have a problem that I can't seem to crack (well I can but not how it seems it should work).

My 'fresh' ESP knows nothing so it boots into AP mode.
Using MLWifi I connect to my ESP, no problem (after confirming in the dialog box).
I send the WiFi credentials to it, it then saves them and reboots to connect to the WiFi.
My phone then disconnects from the AP (as the AP no longer exists) and being a Q+ reconnects to the original WiFi.
If I just leave the phone to reconnect to its original WiFi in this way (which it does) my app can no longer connect to anything using HTTP.
Oddly, UDP works and I can send packets (I use UDP broadcast to get the IP address of the newly configured ESP) but with HTTP I get a socket exception.

To solve this (but not the way I would like) I have to re-scan the WiFi and then explicitly connect to the WiFi (using WiFi.ConnectWiFiAP with the ssid and password).
When it is reconnected then it will once again connect via HTTP.

Also closing my app and re-starting it also 're-enables' connections via HTTP.

As I say, it works but not as I would expect, having to force the reconnect to the original WiFi.
Any ideas?
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi Colin

There is an app called Wifi Analyzer - Apps on Google Play and when it does a scan it can show the chanel, mac, signal, chn Herts (2.4/5), can MLwifi do the same please.. It would be really useful if it could as we have a use case for being able to do that. I am currently using MLWifi 4.40.

Regards

John.
 

danoveb

Member
Licensed User
Longtime User
How do I set the debug property? I have a specific phone that behave weird with this library. (Works great on any other phone).
I never get the _Scandone event, and also if I use the MLwifi, WifiSSID returns "<unknown ssid>" no mather what SSID I am connected to. I think I messed up something in the phone when fiddling with the PreferWifiRouting
 

danoveb

Member
Licensed User
Longtime User
Nevermind, found the issue. Had lost the PERMISSION_ACCESS_FINE_LOCATION permission...
 

Lakhtin_V

Active Member
Licensed User
Longtime User
Hi, Who can offer an example that works correctly with this version of the library. I am a beginner and it is not clear to me how to run such an example, I would like to test smartphones in Wi-Fi direct without using a router. So you need to debug the USB cable, and turn off the Wi-Fi router. What are the requirements for the androyd version and the lines in the manifest?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Yes, it would be great if you could post it.

Thanks

John
I'm travelling at the moment but will be home tomorrow, so I'll do it over the next couple of days.

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Thanks Colin
I've attached the source (MLWifi.zip) to the first post in this thread. It's only the Java file as the entire Android Studio project zipped was too big (900 KB) for the forum. If you want the entire project, send me a private message with your email address & I'll send it to you.

- Colin.
 
Top