create Wifi Access Point - Android Phone

Android examples for Phone:wifi

Description

create Wifi Access Point

Demo Code


//package com.java2s;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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

public class Main {
    public static void createWifiAccessPoint(Context context, String ntId,
            String password) {/*ww w  .j a v  a  2  s .c  om*/
        try {
            WifiManager wifiManager = (WifiManager) context
                    .getSystemService(context.WIFI_SERVICE);

            if (wifiManager.isWifiEnabled()) {
                wifiManager.setWifiEnabled(false);
            }

            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;
                    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);
                    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);
                                    }
                                }
                            }
                        }
                        if (apstatus) {
                            Log.d("better", "Access Point Created");
                        } else {
                            Log.d("better",
                                    "Failed to create Access Point!");
                        }

                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Related Tutorials