Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create access point programmatically

Tags:

I've written the code to create an access point for android devices. I've tested on both emulator and real device.But it doesn't work. Where did i get wrong?

public class MainWAP extends Activity {

    WifiManager wifiManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_wap);
    }

    public void openWifi(View v) {
        createWifiAccessPoint();
    }

    private void createWifiAccessPoint() {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
        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 = "AccessPoint";
                netConfig.allowedAuthAlgorithms.set(
                    WifiConfiguration.AuthAlgorithm.OPEN);
                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("Splash Activity",
                            "Access Point created");
                    } else {
                        Log.d("Splash Activity",
                            "Access Point creation failed");
                    }

                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        if (!methodFound) {
            Log.d("Splash Activity",
                "cannot configure an access point");
        }
    }
}
like image 963
daniel Avatar asked Oct 17 '12 03:10

daniel


Video Answer


1 Answers

Your WiFiManager is definately not initialized.

In your onCreate method add this:

wifiManager = (WiFiManager) getSystemService(Context.WIFI_SERVICE);
like image 69
Dmitry Zaytsev Avatar answered Oct 03 '22 00:10

Dmitry Zaytsev