Hereby an update with my progress:
I’ve written a function that checks if the device is online or not. It executes the following steps:
- Check if device is in AP mode. If true, device is offline. If false, continue
- Check device state, if disconnected, device is offline. If not, continue
- If device state is not ‘connected’, wait for this state
- Ping a couple of hosts (like Google’s DNS / CloudFlare’s DNS, both IPv4 and IPv6). If one ping succeeded, device is online. If not, continue
- If ping didn’t succeed (could be firewall issues), execute a HTTP(s) ping to our own server. If completed, device is offline. Else device is offline.
If this check determines that the device is offline, it’ll scan all Wi-Fi networks and saved it in a variable. Then it starts the AP.
Connect to configured access points
I’ve also created a function that tries to connect to networks that are already configured. This function does the following:
- Check if device is online (see above). If it is, return that it’s connected. If not, continue
- Get all configured connections, filtered by Wi-Fi connections. If none found, stop. If not, continue
- Scan Access Points
- If an access point is found that has a configured connection (based by SSID) OR if a config is found with
hidden: true, continue. Else, stop
- Try connecting to configured access points that are found (or hidden). If succeeded, awesome. If not, return that it’s not awesome.
After this function, based on the response, the access point can be started again.
I had to create this function, because the built-in function of NetworkManager didn’t work as expected. If I activate the connection
"/" on a specific device, according to the documentation, it should automatically connect to the best configuration found. But, according to NetworkManager, the best configuration is the AP configuration , even with
I’ve also improved my NetworkManager library. The scan function handles the errors well. And when activating a connection, the library waits for the device state to change to connected. Or failed, if it fails, and throws an error. So you’re certain that when activating a network, it’s really activated and not failed connecting.
If someone has any suggestions about the functions I’ve created, please let me know!
I’m planning on publishing the NetworkManager library (when it’s mature enough), so more people can work on it and can improve it.