Android代码实现打开打开wifi wps按钮和wps pin码输入

wps按钮连接wifi,通过WifiManager.startWps打开wps,并传入wpsinfo,和wpsListener。wpsListener继承自WifiManager.WpsCallback里面有三个需要重写的方法,本别为wps开启的时候,和wps连接成功和失败的时候的回调。在三个回调里实现自己代码即可。WifiManager.starWps开始连接。
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void startWifiTest() {

    WpsInfo wpsConfig = new WpsInfo();
    wpsConfig.setup = WpsInfo.PBC;
    WpsListener mWpsListener = new WpsListener();
    wifiManager.startWps(wpsConfig, mWpsListener);

}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
class WpsListener extends WifiManager.WpsCallback{

    @Override
    public void onStarted(String pin) {
        Logger.d("start wps");

    }

    @Override
    public void onSucceeded() {
        Logger.d("wps connected success");
        removeWifi();
        disableWifi();
        testPass("");
    }

    @Override
    public void onFailed(int reason) {

        wifiManager.cancelWps(null);
        disableWifi();
        testFailed("wps connected failed:"+reason);

    }
}
 
  
wps pin码输入与wps按钮连接相似,参考代码即可,注意wosInfo配置的时候不一样。
 
  
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void startWifiTest() {

    WpsInfo wpsConfig = new WpsInfo();
    wpsConfig.setup = WpsInfo.DISPLAY;
    WpsListener mWpsListener = new WpsListener();
    wifiManager.startWps(wpsConfig, mWpsListener);

}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
class WpsListener extends WifiManager.WpsCallback{

    @Override
    public void onStarted(String pin) {
        Logger.d("start wps");
        Logger.d("pin:"+pin);

        DialogShowMessage showMessage = new DialogShowMessage("请在路由器终端输入PIN码","PIN码:"+pin,R.drawable.ic_launcher);
        EventBusUtils.post(showMessage);

    }

    @Override
    public void onSucceeded() {
        Logger.d("wps connected success");
        removeWifi();
        disableWifi();
        testPass("");
    }

    @Override
    public void onFailed(int reason) {

        wifiManager.cancelWps(null);
        disableWifi();
        testFailed("wps connected failed:"+reason);

    }
}

你可能感兴趣的:(Android,开发相关)