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:
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:
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:
disconnectWifiAP: (disconnects from a Wifi network)
Usage:
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.
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
Last edited: