disable Wifi AP - Android Phone

Android examples for Phone:wifi

Description

disable Wifi AP

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class Main {
    public static boolean disableAP(Context context, String ntId,
            String password) throws Exception {
        boolean apstatus = false;

        WifiManager wifiManager = (WifiManager) context
                .getSystemService(context.WIFI_SERVICE);

        Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class     
        for (Method method : wmMethods) {
            if (method.getName().equals("setWifiApEnabled")) {

                WifiConfiguration netConfig = new WifiConfiguration();
                netConfig.SSID = ntId;/* w  ww.  ja v  a2  s .  co  m*/
                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 = password;
                netConfig.allowedPairwiseCiphers
                        .set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedPairwiseCiphers
                        .set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedGroupCiphers
                        .set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedGroupCiphers
                        .set(WifiConfiguration.GroupCipher.TKIP);

                apstatus = (Boolean) method.invoke(wifiManager, netConfig,
                        false);
            }
        }

        return apstatus;
    }
}

Related Tutorials