taontech / githublog

一个基于github issues的博客系统,实时呈现,零依赖,零代码部署,不用打包不用上线。
4 stars 1 forks source link

安卓11 以上连接方式 #68

Open taontech opened 1 year ago

taontech commented 1 year ago
@RequiresApi(Build.VERSION_CODES.Q)
    private fun newCodeForAndroid11andMore(
        data: ScanResult,
        wifiSSID: String,
        wifiPassword: String,
        capabilities: String
    ) {
        val suggestion2 = WifiNetworkSuggestion.Builder()
            .setSsid(data.SSID) // SSID of network
            .setWpa2Passphrase(wifiPassword) // password is network is not open
            .setIsAppInteractionRequired(true) // Optional (Needs location permission)
            .setIsUserInteractionRequired(true)
            .build()

        val suggestionsList = listOf(suggestion2);

        val wifiManager =
            applicationContext.getSystemService(WIFI_SERVICE) as WifiManager

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            return
        }

        val list = wifiManager.configuredNetworks
        for (i in list) {
            if (i.SSID != null && i.SSID == "\"" + wifiSSID + "\"") {
                val isDisconnected = wifiManager.disconnect()
                val isEnabled = wifiManager.enableNetwork(i.networkId, true)
                val isReconnected = wifiManager.reconnect()
                break
            } else {
                wifiManager.removeNetwork(i.networkId)
                wifiManager.saveConfiguration()
            }
        }

        val status = wifiManager.addNetworkSuggestions(suggestionsList);
        Log.e("NETWORK", "Status:" + status)

        if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
            // already connected with another network
//            val status1 = wifiManager.removeNetworkSuggestions(suggestionsList)
//            Log.e("NETWORK", "Remove:" + status1)
        }

        if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
            // do error handling here
            Log.e("NETWORK", "Error")
            //Toast.makeText(context, "Error:"+status, Toast.LENGTH_SHORT).show()
        }

        // Optional (Wait for post connection broadcast to one of your suggestions)
        val intentFilter =
            IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);

        val broadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                Log.e("NETWORK", "broadcastReceiver")

                if (!intent.action.equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
                    Toast.makeText(
                        context,
                        "Post connection broadcastReceiver:" + intent,
                        Toast.LENGTH_SHORT
                    ).show()

                    return;
                }
                // do post connect processing here
                //Toast.makeText(context, "Post connection:"+intent, Toast.LENGTH_SHORT).show()
                Log.e("NETWORK", "post connect")
            }
        };
        registerReceiver(broadcastReceiver, intentFilter)

        //android10andMoreVersions(data, data.SSID, wifiPassword, data.capabilities)
    }