fun start() { // Framework code throws if a listener is registered twice without unregistering. if (isRegistered) return val request = NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .build() // AFAICT, the framework does not send an event when a new NetworkCallback is registered // while the WIFI is not connected, so we push this manually. If the WIFI is on, it will send // a follow up event shortly val noCallbacksReceivedYet = lastKnownStateWasAvailable == null if (noCallbacksReceivedYet) { lastKnownStateWasAvailable = false notifyListeners(false) } connectivityManager.registerNetworkCallback(request, frameworkListener) isRegistered = true }
Attaches the [WifiConnectionMonitor] to the application. After this has been called, callbacks added via [addOnWifiConnectedChangedListener] will be called until either the app exits, or [stop] is called.