0

I tried to change mobile hotsport broadcast channel in android by programmatically by using following code but it not changing any value/channel.

Note: I able to change the SSID and Password by programmatically.
I tried to set channel 11, Still it not working...

Thanks in advance

My code is

public void HotspotChannelWrite() {
    WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);

    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    netConfig = new WifiConfiguration();
    netConfig.SSID = "TipturInfo";
    netConfig.preSharedKey = "Sharath";
    netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    try {

        Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        boolean apstatus=(Boolean) setWifiApMethod.invoke(wifiManager, netConfig,true);

        Method isWifiApEnabledmethod = wifiManager.getClass().getMethod("isWifiApEnabled");
        while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){};

        Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState");
        int apstate=(Integer)getWifiApStateMethod.invoke(wifiManager);

        Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
        netConfig=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wifiManager);

        Log.i("Writing HotspotData", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");

        // For Channel change
        Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");
        int freq = 2462; // default to channel 11
        wcAdhocFreq.setInt(netConfig, freq);
        Log.i("HotspotData Channel", "\n Frequence:"+freq );
        Log.i("HotspotData Channel", "\n Frequence:"+wcAdhocFreq );

        // For Saving Data
        wifiManager.saveConfiguration();

    } catch (IllegalFormatException ife) {
        ife.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    wifiManager.saveConfiguration();
}

I tried alternative way as well:

Field wcFreq = WifiConfiguration.class.getField("channel");
       wcFreq.setInt(netConfig,11);
kgsharathkumar
  • 1,022
  • 1
  • 15
  • 29
  • http://stackoverflow.com/questions/6394599/android-turn-on-off-wifi-hotspot-programmatically –  Aug 24 '16 at 06:42
  • 1
    @Adnanul, above link is for turn On/Off WiFi HotSpot programmatically but my question is on specifically changeing mobile hotspot broadcast channel..??? Thanks, – kgsharathkumar Aug 24 '16 at 06:48
  • kgsharathkumar ,have you got the available hotspot broadcast channels , yet? this should be the 1st step,right? –  Aug 24 '16 at 07:07
  • Yes, that i alredy done, i need last step?? – kgsharathkumar Aug 24 '16 at 07:57
  • http://stackoverflow.com/questions/19879708/android-wifi-hotspot-client-connection-events?rq=1 –  Aug 24 '16 at 08:03
  • https://github.com/nickrussler/Android-Wifi-Hotspot-Manager-Class –  Aug 24 '16 at 08:04
  • @Adnanul, i implemented like this(please see edited code) but not changing channel.. – kgsharathkumar Aug 25 '16 at 05:04

3 Answers3

1

Try this, works for Android 7.0

                Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
                netConfig=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wifiManager);

                //Log.i("Writing HotspotData", "\nSSID:" + netConfig.SSID + "\nPassword:" + netConfig.preSharedKey + "\n");

                Field wcBand = WifiConfiguration.class.getField("apBand");
                int vb = wcBand.getInt(netConfig);
                Log.i("Band was", "val=" + vb);
                wcBand.setInt(netConfig, 2); // 2Ghz

                // For Channel change
                Field wcFreq = WifiConfiguration.class.getField("apChannel");
                int val = wcFreq.getInt(netConfig);
                Log.i("Config was", "val=" + val);
                wcFreq.setInt(netConfig,11); // channel 11

                Method setWifiApConfigurationMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
                setWifiApConfigurationMethod.invoke(wifiManager, netConfig);

                // For Saving Data
                wifiManager.saveConfiguration();
N0rka
  • 26
  • 1
  • Consider adding some explanations - and to properly indent your source code. No need for 15 character border on the left hand side! – GhostCat Jul 07 '17 at 19:40
0

Your question i totally agreed, But there is no API are avilable for reading/writing mobile hotspot broadcast channel in android by programmatically.

0

You cannot programmatically.

I totally agree with your question, but in Android no APIs are available for reading/writing mobile hotspot broadcast channel programmatically.

Niccolò Passolunghi
  • 5,608
  • 2
  • 24
  • 33