在Android中以编程方式连接WPS?

时间:2016-11-25 01:29:24

标签: android wifi android-wifi wifimanager wps

我如何以编程方式尝试使用WPS协议连接到wifi? 我已经找到了如何连接WEP,WPA和开放wifi,但不是WPS ......

全部谢谢!

Greg432收到后编辑

我已尝试使用此代码,但它不起作用,解决方案?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                wpsInfo = new WpsInfo();
                wpsInfo.BSSID = wifiList.get(0).BSSID;
                wpsInfo.pin = "11883737";
                wpsInfo.setup = 0;


                wpsCallback = new WifiManager.WpsCallback() {
                    @Override
                    public void onStarted(String pin) {
                        Toast.makeText(getApplicationContext(), "Started wps connection.", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSucceeded() {
                        Toast.makeText(getApplicationContext(), "Connected!", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailed(int reason) {
                        String errorMessage;
                        switch (reason) {
                            case WifiManager.WPS_OVERLAP_ERROR:
                                errorMessage = "WPS_OVERLAP_ERROR";
                                break;
                            case WifiManager.WPS_WEP_PROHIBITED:
                                errorMessage = "WPS_WEP_PROHIBITED";
                                break;
                            case WifiManager.WPS_TKIP_ONLY_PROHIBITED:
                                errorMessage = "WPS_WEP_PROHIBITED";
                                break;
                            /*case WifiManager.IN_PROGRESS:
                                mWifiManager.cancelWps(null);
                                startWps();
                                return;*/
                            case WifiManager.WPS_TIMED_OUT:
                                errorMessage = "WPS_TIMED_OUT";
                                //startWps();
                                return;
                            default:
                                errorMessage = "Generic error.";
                                break;
                        }

                        Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT).show();
                    }
                };

                wifiManager.startWps(wpsInfo, wpsCallback);
}

2 个答案:

答案 0 :(得分:1)

使用WifiManager的startWps()方法:

mWifiManager.startWps(wpsConfig, mWpsCallback);

您应该传递配置和回调。

示例

https://android.googlesource.com/platform/packages/apps/TvSettings/+/lollipop-wear-release/Settings/src/com/android/tv/settings/connectivity/WpsConnectionActivity.java

答案 1 :(得分:0)

2021 年更新

从 API 28 及更高版本开始,以编程方式连接 WPS 已被弃用和移除。

无法再连接到 mWifiManager.startWps(wpsConfig, mWpsCallback); 并且也没有其他选择。

来源:https://developer.android.com/reference/android/net/wifi/WifiManager#startWps(android.net.wifi.WpsInfo,%20android.net.wifi.WifiManager.WpsCallback)