Can't set “WifiConfiguration” when enabling wifi-hotspot using “setWifiApEnabled”

后端 未结 2 1986
挽巷
挽巷 2021-01-21 08:19

I\'m trying to set my Android device to be an Access-Point using the code I\'ve seen here before:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 08:34

    Before Invoking the Method "setWifiApEnabled" you need to call "getWifiApConfiguration" to get the default WifiConfiguration
    Then change the SSID and Password and then invoke "setWifiApConfiguration" with the modified WifiConfiguration and after that call "setWifiApEnabled"
    Here is the Code.

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    
    getWifiConfig = wifi.getClass().getMethod("getWifiApConfiguration",null);
    WifiConfiguration myConfig = (WifiConfiguration) getWifiConfig.invoke(wifi,null);
    
    myConfig.SSID = "Hello World";
    
    setWifiConfig = wifi.getClass().getMethod("setWifiApConfiguration",WifiConfiguration.class);
    setWifiConfig.invoke(wifi,new Object[]{myConfig,true});
    
    enableWifi = wifi.getClass().getMethod("setWifiEnabled",WifiConfiguration.class,boolean.class);
    enableWifi.invoke(wifi,null,true);
    

提交回复
热议问题