android turning on wifi programmatically

前端 未结 2 460
再見小時候
再見小時候 2020-12-08 23:37

I am trying to turn add a wifi network programmatically and to connect to that network.

My code works fine if the wi-fi is already turned on.

If wi-fi is off

2条回答
  •  伪装坚强ぢ
    2020-12-09 00:26

    You can turn on/off wifi using following instructions :

    WifiManager wManager = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wManager.setWifiEnabled(booleanValue); //true or false
    

    Set following permissions to your manifest:

    
    
    
    

    This code is deprecated in API 29 or over

    public boolean setWifiEnabled (boolean enabled)
    **This method was deprecated in API level 29**.
    

    Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect. If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.

    Source : Link

提交回复
热议问题