20

我已经编写了代码来为安卓设备创建一个接入点。我已经在模拟器和真实设备上进行了测试。但它不起作用。我哪里弄错了?

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");
        }
    }
}
4

3 回答 3

7

WiFiManager的肯定没有初始化。

在您的onCreate方法中添加以下内容:

wifiManager = (WiFiManager) getSystemService(Context.WIFI_SERVICE);
于 2012-11-09T12:36:43.573 回答
3

你需要很少的东西来使这个代码工作。

1) 初始化 wifiManager onCreate()

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

2)您需要向您请求此权限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) 您的应用程序需要使用系统证书进行签名。

于 2013-04-16T12:18:50.717 回答
0

使用您使用反射利用未记录 API 的方法,事情可能无法在所有场景中正常工作。好吧,您可以尝试将这些添加到清单文件中并尝试一下。

android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.WRITE_APN_SETTINGS 
于 2012-10-17T04:30:46.010 回答