Android WiFi功能开发

来源:互联网 发布:青蛙少年 知乎 编辑:程序博客网 时间:2024/06/05 07:20

Android WiFi功能开发

1、功能说明

        在桌面弹窗滑动按钮进行wifi的开关、wifi切换、wifi密码记录。

2、功能

在桌面启动弹窗
WindowManager wm = this.getWindowManager();int width = wm.getDefaultDisplay().getWidth();int hight = wm.getDefaultDisplay().getHeight();LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View mainview = inflater.inflate(R.layout.dialog_target, null);setContentView(mainview, new LayoutParams(width * 10 / 13,hight * 3 / 5));

打开/关闭WiFi
/** * 打开Wifi网卡 */public void openNetCard() {if (!mWifiManager.isWifiEnabled()) {mWifiManager.setWifiEnabled(true);}}/** * 关闭Wifi网卡 */public void closeNetCard() {if (mWifiManager.isWifiEnabled()) {mWifiManager.setWifiEnabled(false);}}

扫描周边网络
/** * 扫描周边网络 */public void scan() {mWifiManager.startScan();listResult = mWifiManager.getScanResults();if (listResult != null) {Log.i(TAG, "当前区域存在无线网络,请查看扫描结果");} else {Log.i(TAG, "当前区域没有无线网络");}}



连接/断开wifi
/** * 连接指定网络 */public void connect() {mWifiInfo = mWifiManager.getConnectionInfo();}/** * 断开当前连接的网络 */public void disconnectWifi() {int netId = getNetworkId();mWifiManager.disableNetwork(netId);mWifiManager.disconnect();mWifiInfo = null;}

根据不同加密方式,用密码和名称连接指定wifi
/** * 连接到指定的wifi */public int connectwifi(String name,String password,String capabilities){WifiConfiguration config = new WifiConfiguration();config.allowedAuthAlgorithms.clear();          config.allowedGroupCiphers.clear();          config.allowedKeyManagement.clear();          config.allowedPairwiseCiphers.clear();          config.allowedProtocols.clear();  //config.SSID = "\"HI-LINK_4E77\"";config.SSID = "\"" +name+ "\"";//    config.preSharedKey = "\"12345678\"";if(capabilities.equals("[ESS]")){//config.wepKeys[0] = "";  config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);              config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  //            config.wepTxKeyIndex = 0;    }else if(capabilities.contains("WEP")){config.hiddenSSID = true;              config.wepKeys[0] = "\"" + password + "\"";   config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);               config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);              config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);              config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);              config.wepTxKeyIndex = 0;  }else {config.preSharedKey = "\""+password +"\"";        config.hiddenSSID = true;        config.status = WifiConfiguration.Status.ENABLED;        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);         config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);        config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);  }        int netId = mWifiManager.addNetwork(config);        mWifiManager.enableNetwork(netId, true);        return netId;}

3、实例截图



4、代码demo
http://download.csdn.net/download/workwayli/10113696


原创粉丝点击