开启和关闭wifi的代码

1、需要申请的权限
  android.permission.ACCESS_WIFI_STATE
  android.permission.CHANGE_WIFI_STATE
  android.permission.WAKE_LOCK
2、获取WifiManager
  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
3、开启、关闭wifi
  if (wifiManager.isWifiEnabled()) {
  wifiManager.setWifiEnabled(false);
  } else {
  wifiManager.setWifiEnabled(true);
  }

Android设置wifi

1.new 一个wificonfiguration对象。

2.设置这个对象的一些属性。

[java] view plain copy
  1. <spanstyle="font-size:18px">WifiConfigurationwc=newWifiConfiguration();
  2. wc.SSID="\""+sr.SSID+"\"";//<spanstyle="color:rgb(255,0,0);">这个地方一定要注意了。旁边的“是不能够省略的。密码的地方也一样。</span>
  3. wc.preSharedKey="\""+etPassword.getText().toString()+"\"";//该热点的密码
  4. wc.hiddenSSID=true;
  5. wc.status=WifiConfiguration.Status.ENABLED;
  6. wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
  7. wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  8. wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  9. wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
  10. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
  11. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
  12. wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);</span>
3.判断wifi是否加密:
[java] view plain copy
  1. <spanstyle="font-size:18px">publicstaticintgetSecurity(ScanResultresult){
  2. if(result.capabilities.contains("WEP")){
  3. return1;
  4. }elseif(result.capabilities.contains("PSK")){
  5. return2;
  6. }elseif(result.capabilities.contains("EAP")){
  7. return3;
  8. }
  9. return0;
  10. }
  11. </span>

4.连接未加密wifi连接:

[java] view plain copy
  1. <spanstyle="font-size:18px"><prename="code"class="java">WifiConfigurationconfig=newWifiConfiguration();
  2. config.SSID="\""+sr.SSID+"\"";
  3. config.allowedKeyManagement.set(KeyMgmt.NONE);
  4. intnetworkId=wifiManager.addNetwork(config);
  5. if(networkId!=-1){
  6. wifiManager.enableNetwork(networkId,false);
  7. wifiManager.saveConfiguration();
  8. }</pre><p></p>
  9. <pre></pre>
  10. <p></p>
  11. </span>

更多相关文章

  1. 转:android 实现 流媒体 播放远程mp3文件 代码
  2. android xml操作
  3. Android(安卓)6.0 关于WiFi的改变
  4. Android(安卓)硬编码
  5. android【打电话,收发短信实现】
  6. Android之TabHost的几种使用方法
  7. Android——HashMap的替代方案
  8. Android(安卓)-- 文字设置超链接
  9. Android(安卓)Bundle类

随机推荐

  1. Android(安卓)MTK Launcher3安装三方apk,
  2. android 中调用接口发送短信
  3. Android(安卓)死机问题分析方法收集
  4. Android(安卓)百分比布局
  5. Android弹性动画三种实现方式
  6. Android中visibility属性VISIBLE、INVISI
  7. Android(安卓)Studio安装使用图文教程
  8. Android(安卓)studio使用lambda表达式
  9. Android(安卓)读取doc文件
  10. android服务器问题