像 Google Lens 一样以编程方式连接 wifi

发布于 2025-01-11 00:44:30 字数 1787 浏览 2 评论 0原文

我正在尝试以编程方式实现 wifi 连接,但似乎自从 android 10 以来它遇到了很多问题并尝试了来自 GitHub 的许多源代码,每个实现都有一些限制。 我不是 Android 开发人员,我开始在 flutter 中工作,它需要以编程方式连接 wifi,以便它可以与其他设备交互,例如本地主机网络。 我发现一些按方面连接的片段,但它不与本地主机交互

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)
//        }
    }

我也尝试了其他实现,但它有一些问题,例如如果设备已经连接到另一个wifi网络,它不会切换到请求的网络,我还尝试了这些包 flutter wifi_iot、flutter_wifi_connect。

但是,当我尝试与 Google Lens 连接时,它工作得很好,所以我想知道我们是否可以使用像 Google Lens 一样的实现级别,我想知道它在技术上是否可行,或者是否受到 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文