赞
踩
WifiManager.setWifiEnable(true/false)在10以后都没有作用,一直会返回false,可以将targetSdk版本设置到28,可以触发权限判断
自己尝试下来不root有两种办法
- val suggestion = WifiNetworkSuggestion.Builder()
- .setSsid(“指定wifi的SSID,就是wifi的名字”)
- .setWpa2Passphrase(“WIFI密码”)
- .setIsAppInteractionRequired(true)
- .build()
- val suggestionsList = listOf(suggestion)
- val status = wifiManager.addNetworkSuggestions(suggestionsList)
- Log.d(TAG, status.toString())
- if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
-
- }
- val intentFilter = IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);
- val broadcastReceiver = object : BroadcastReceiver() {
- override fun onReceive(context: Context, intent: Intent) {
- if (!intent.action.equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
- return
- }
- }
- };
- context.registerReceiver(broadcastReceiver, intentFilter);
实际操作下来,并没有什么卵用,当设置里连接过wifi,你的建议Android不听
PS:谷歌官方示例里的这句话不要加,在荣耀手机加了无法连接,其他手机没试
//.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
- val specifier = WifiNetworkSpecifier.Builder()
- .setSsidPattern(PatternMatcher("WIFI名字", PatternMatcher.PATTERN_PREFIX))
- .setWpa2Passphrase("密码")
- .build()
-
- val request = NetworkRequest.Builder()
- .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
- //谷歌官方示例里的这句话不要加,在荣耀手机加了无法连接,其他手机没试
- //.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
- .setNetworkSpecifier(specifier)
- .build()
-
- val connectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
-
- val networkCallback = object : ConnectivityManager.NetworkCallback() {
- override fun onAvailable(network: Network) {
- Log.e(TAG, "onAvailable")
- }
-
- override fun onUnavailable() {
- Log.e(TAG, "onUnavailable")
- }
- }
- connectivityManager.requestNetwork(request, networkCallback)
连接下来还是挺顺畅的,就是连上以后系统WIFI不会进行记录,并且出现其他app不能联网的情况,高版本的还是老老实实的自己跳WIFI设置吧
startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。