20

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");
        }
    }
}
Rudolf Real
  • 1,948
  • 23
  • 27
daniel
  • 201
  • 1
  • 2
  • 3

3 Answers3

8

Your WiFiManager is definately not initialized.

In your onCreate method add this:

wifiManager = (WiFiManager) getSystemService(Context.WIFI_SERVICE);
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
  • @Tom this is an answer to his question. If he'll initialize WiFiManager (and now it's `null`) everything will work fine (I've just tested it). – Dmitry Zaytsev Nov 09 '12 at 13:04
  • You just stated a fact. I think you should explain how it relates to the specific question and what should be done to solve the problem. There's a reason your answer has been automatically classified as low-quality. – toniedzwiedz Nov 09 '12 at 13:07
  • That's exactly what I mean. The post you replied to has been generated automatically by the review system. – toniedzwiedz Nov 09 '12 at 13:09
  • wifiManager = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE); // to prevent memory leak – Farshid Ahmadi Dec 25 '20 at 11:40
3

You need few things to make this code to work.

1) Init wifiManager onCreate() :

WifiManager wifiManager = (WiFiManager) getSystemService(Context.WIFI_SERVICE);

2) You need to ask for this permissions in you AndroidManifest.xml :

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />

3) Your application need to be signed with system certificates.

EvZ
  • 11,889
  • 4
  • 38
  • 76
0

With your method of exploiting undocumented APIs using reflection, things might not work well in all scenarios. Well, you can try adding these in your manifest file and give a try.

android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.WRITE_APN_SETTINGS 
Durairaj Packirisamy
  • 4,635
  • 1
  • 21
  • 27
  • I've added in manifest file: android.permission.CHANGE_WIFI_STATE, android.permission.CHANGE_NETWORK_STATE, android.permission.INTERNET android.permission.ACCESS_NETWORK_STATE, android.hardware.wifi, android.permission.WRITE_APN_SETTINGS. But when i run in emulator and click on button, it pop-up force close. – daniel Oct 17 '12 at 07:21
  • In the log message it show: 1)caused by java.lang.reflect.InvocationTargetException 2)caused by java.lang.NullPointerException – daniel Oct 17 '12 at 07:32
  • 1) Caused by java.lang.reflect.InvocationTargetException at line 27: `createWifiAccessPoint();` 2) Caused by java.lang.NullPointerException at line 33: `if(wifiManager.isWifiEnabled())` @WilHall – daniel Oct 17 '12 at 08:11