V
V
Vladimir Prikhodko2014-01-15 01:03:44
Android
Vladimir Prikhodko, 2014-01-15 01:03:44

Disable hotspot

Good afternoon!
There is a method for creating an access point:

public void createWifiAccessPoint() throws SocketException {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
        Log.d("Debug:getClass:", wifiManager.getClass().toString());
        Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();
        boolean methodFound = false;
        for (Method method : wmMethods) {
            if (method.getName().equals("setWifiApEnabled")) {
                methodFound = true;
                WifiConfiguration netConfig = new WifiConfiguration();
                netConfig.SSID = name_ap;


                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.preSharedKey = key;
                try {
                    boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);
                    for (Method isWifiApEnabledmethod : wmMethods) {
                        if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")) {
                            while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {
                            }
                            for (Method method1 : wmMethods) {
                                if (method1.getName().equals("getWifiApState")) {
                                    int apstate;
                                    apstate = (Integer) method1.invoke(wifiManager);
                                    Log.d("Debug:my", String.valueOf(apstate));
                                }
                            }
                        }
                    }
                    if (apstatus) {
                        Log.d("Debug:my", "Access Point created");
                    } else {
                        Log.d("Debug:my", "Access Point creation failed");
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        if (!methodFound) {
            Log.d("Debug:my", "cannot configure an access point");
        }
    }

That is, the setWifiApEnabled method of the WifiManager class is used through reflection.
The question is, how to turn off the access point ??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zvorygin, 2014-01-15
@cska63

for (Method method : wmMethods)
  if (method.getName().equals("setWifiApEnabled"))
    method.invoke(wifiManager, netConfig, false);

Tried? Well, or null instead of netConfig?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question