我如何通过WifiConfiguration连接到特定的wifi

时间:2018-09-17 19:18:22

标签: java android

在这里,我尝试了很多尝试之后仍然无法连接,根据谷歌,我在android 6.0(API 23)上工作,但是您无法在android 6.0中使用新的wifi配置(不知道为什么?),所以我更改了它到android 5.1.1(API 22)仍然是同样的问题

有什么问题

MainActivity .java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView text;WifiConfiguration wfc;
    String ssid = "myssid";
    String password = "pass123456789";
    WifiManager wfMgr;
    Button button;
    int networkID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wfMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        setConf();

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        this.registerReceiver(this.myWifiReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    private BroadcastReceiver myWifiReceiver
            = new BroadcastReceiver(){

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            NetworkInfo networkInfo = (NetworkInfo) arg1.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
                button.setText("Connected");
            }
            else
                button.setText("Disconnected");
        }};

    @Override
    protected void onResume() {
        super.onResume();

    }

    public void setConf(){
        wfc = new WifiConfiguration();

        wfc.SSID = "myssid".concat(ssid).concat("pass123456789");
        wfc.status = WifiConfiguration.Status.DISABLED;
        wfc.priority = 40;
        wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wfc.preSharedKey = "mypass".concat(password).concat("pass123456789");
        networkID = wfMgr.addNetwork(wfc);
    }
    public void wifiConnect(){
        wfMgr.disconnect();
        wfMgr.enableNetwork(this.networkID, true);
        wfMgr.reconnect();
    }
    public void wifiDisconnect(){
        wfMgr.disconnect();
    }

    @Override
    public void onClick(View v) {
        if(button.getText()=="Connected"){
            wifiDisconnect();
        }
        else
            wifiConnect();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(myWifiReceiver);
        super.onPause();
    }
}

0 个答案:

没有答案