android turning on wifi programmatically

前端 未结 2 459
再見小時候
再見小時候 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:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
    

    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

    0 讨论(0)
  • 2020-12-09 00:29

    You need to create wifiLock with wifi mode_full, a bit strange name for that method but doing the work.

    0 讨论(0)
提交回复
热议问题