1 year ago

#233588

test-img

Rahul Gupta

Programmatically wifi connect like Google Lens

I am trying to implement wifi connect programmatically, but it seems since android 10 It had many issues and tried many source code from GitHub, every implementation has some limitations. I am not an android developer, I started working in flutter and it requires connecting wifi programmatically, so it can interact with other devices, such as the localhost network. I found some snippets that connect as aspected, but it does not interact with localhost

fun wifiConnect(ssid: String, password: String) {

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        val specifier = WifiNetworkSpecifier.Builder()
            .setSsid(ssid).apply {
                setWpa2Passphrase(password)
//                setWpa3Passphrase(password)
            }
            .build()

        val request = NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
            .setNetworkSpecifier(specifier)
            .build()

        this.networkCallback = object : ConnectivityManager.NetworkCallback() {
            override fun onAvailable(network: Network) {
                super.onAvailable(network)
                connectivityManager.bindProcessToNetwork(network)
//                    result.success(true)
                // cannot unregister callback here since it would disconnect form the network
            }

            override fun onUnavailable() {
                super.onUnavailable()
//                    result.success(false)
                //connectivityManager.unregisterNetworkCallback(this)
            }
        }

        val handler = Handler(Looper.getMainLooper())
        connectivityManager.requestNetwork(request, networkCallback!!, handler)
//        }
    }

I also tried other implementations but it has some issues, such as if the device is already connected to another wifi network, it doesn't switch to the requested network, I also tried these packages flutter wifi_iot, flutter_wifi_connect.

But when I try to connect with Google Lens it works perfectly, So I am wondering if we can use the same level of implementation like Google Lens, I would like to know if its technically possible or its limited with Google Lens

android

flutter

android-studio

android-wifi

wifimanager

0 Answers

Your Answer

Accepted video resources