set Wifi Ap Enabled - Android Phone

Android examples for Phone:wifi

Description

set Wifi Ap Enabled

Demo Code


//package com.java2s;
import java.lang.reflect.Method;

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

public class Main {
    public static boolean setWifiApEnabled(WifiManager wifiManager,
            WifiConfiguration cfg, boolean flag) {
        try {//  ww  w  .  j  a  va  2 s. c o  m
            if (wifiManager.isWifiEnabled() && flag) {
                wifiManager.setWifiEnabled(false);
            }
            Method method = wifiManager.getClass().getMethod(
                    "setWifiApEnabled", WifiConfiguration.class,
                    Boolean.TYPE);
            Boolean enable = (Boolean) method
                    .invoke(wifiManager, cfg, flag);
            if (!flag) {
                wifiManager.setWifiEnabled(true);
            }
            return enable;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

Related Tutorials