Android Question SOLVED - Connecting to a Wifi network using MLwifi

JMB

Active Member
Licensed User
Longtime User
Hi there,

I am slowly working my way through how the MLWifi library works.

My question is this:

How do I actually connect to a network?

If I run this code:
B4X:
    If myMLWifiObject.connectWifiAP("ESP_AP") Then
        Log("Connected")
    End If
    Log(myMLWifiObject.ActiveNetworkTypeName)

then my device appears to connect to the specified network, but my program completely bombs out without any warning messages or any messages on the Logger.

It's as though it crashes.

How do I correctly use the MLWifi library to connect to a known network?

Thanks,

JMB
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Try disconnecting from the existing AP first. Also, the connection won't be instantaneous, so you might want to wait until you are actually connected before you call ActiveNetworkTypeName:

B4X:
Private Sub connectToWifi
    Private connectionStart as Long = DateTime.Now
    Private connectionTimeOut as Long = 30000
 
    If myMLWifiObject.WifiSSID <> "ESP_AP" Then
        myMLWifiObject.disconnectWifiAP
        myMLWifiObject.connectWifiAP("ESP_AP")
        Do Until myMLWifiObject.isWifiConnected
            Sleep(0)
            If DateTime.Now > connectionStart + connectionTimeout Then
                Log("CONNECTION FAILED")
                Return
            End If
        Loop
        Log(myMLWifiObject.ActiveNetworkTypeName)
    End If
End Sub

- Colin.
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Hi Colin,

Thank you very much for that code - most helpful.

There was another reason my code appeared to be crashing out with no log data - the connection was to an ESP access point, and of course as soon as the code switched across to the Access Point, the connection to the debugger broke and that was the end of my session! Simple things....

Anyway, thanks again - progress being made.

JMB
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi Colin,

Thank you very much for that code - most helpful.

There was another reason my code appeared to be crashing out with no log data - the connection was to an ESP access point, and of course as soon as the code switched across to the Access Point, the connection to the debugger broke and that was the end of my session! Simple things....

Anyway, thanks again - progress being made.

JMB
Yep - that'll do it! When I had one of my WeMos boards set up as an AP, I did all my testing with my Android device connected to B4A via USB. Actually that's how I test 99% of the time anyway.

- Colin.
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Hi Colin - you've solved a whole load of problems with your post. I had forgotten that one could connect using USB, and I've managed to get a whole load of stuff working that I've been struggling away at for about a week!

Thank you for that reminder!

- Jonathan
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi Colin - you've solved a whole load of problems with your post. I had forgotten that one could connect using USB, and I've managed to get a whole load of stuff working that I've been struggling away at for about a week!

Thank you for that reminder!

- Jonathan
Yeah - generally the only time I test using the bridge is either when I'm testing a multiplayer game on 2 devices at the same time, or when I'm testing on my Nexus 7 - which has a dodgy USB port & will sometimes disconnect randomly when connected via cable (very annoying when you're in debug mode!). I have my most commonly used test devices always connected to a USB hub anyway, so I find it more convenient & faster.

- Colin.
 
Upvote 0
Top