Android 关于热点的操作

1.获取便携式wifi热点开关状态;

2.热点的开启与关闭。


原创博客地址:http://blog.csdn.net/u013049709/article/details/42235829


直接上代码了,两个功能的方法:

[java]  view plain  copy
  1. // 权限:  
  2. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  

[java]  view plain  copy
  1. /** 
  2.  * WIFI热点业务类 
  3.  * @author wlh 
  4.  * 
  5.  */  
  6. public class WifiHostBiz {  
  7.   
  8.     private final String TAG = "WifiHostBiz";  
  9.     private WifiManager wifiManager;  
  10.     private String WIFI_HOST_SSID = "AndroidAP";  
  11.     private String WIFI_HOST_PRESHARED_KEY = "12345678";// 密码必须大于8位数  
  12.   
  13.     public WifiHostBiz(Context context) {  
  14.         super();  
  15.         //获取wifi管理服务  
  16.         wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
  17.     }  
  18.   
  19.     /**判断热点开启状态*/  
  20.     public boolean isWifiApEnabled() {  
  21.         return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;  
  22.     }  
  23.   
  24.     private WIFI_AP_STATE getWifiApState(){  
  25.         int tmp;  
  26.         try {  
  27.             Method method = wifiManager.getClass().getMethod("getWifiApState");  
  28.             tmp = ((Integer) method.invoke(wifiManager));  
  29.             // Fix for Android 4  
  30.             if (tmp > 10) {  
  31.                 tmp = tmp - 10;  
  32.             }  
  33.             return WIFI_AP_STATE.class.getEnumConstants()[tmp];  
  34.         } catch (Exception e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.             return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;  
  38.         }  
  39.     }  
  40.   
  41.     public enum WIFI_AP_STATE {  
  42.         WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING,  WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED  
  43.     }  
  44.   
  45.     /** 
  46.      * wifi热点开关 
  47.      * @param enabled   true:打开  false:关闭 
  48.      * @return  true:成功  false:失败 
  49.      */  
  50.     public boolean setWifiApEnabled(boolean enabled) {  
  51.         System.out.println(TAG + ":开启热点");  
  52.         if (enabled) { // disable WiFi in any case  
  53.             //wifi和热点不能同时打开,所以打开热点的时候需要关闭wifi  
  54.             wifiManager.setWifiEnabled(false);  
  55.             System.out.println(TAG + ":关闭wifi");  
  56.         }else{  
  57.             wifiManager.setWifiEnabled(true);  
  58.         }  
  59.         try {  
  60.             //热点的配置类  
  61.             WifiConfiguration apConfig = new WifiConfiguration();  
  62.             //配置热点的名称(可以在名字后面加点随机数什么的)  
  63.             apConfig.SSID = WIFI_HOST_SSID;  
  64.             //配置热点的密码  
  65.             apConfig.preSharedKey = WIFI_HOST_PRESHARED_KEY;  
  66.             //安全:WPA2_PSK  
  67.             apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);  
  68.             //通过反射调用设置热点  
  69.             Method method = wifiManager.getClass().getMethod(  
  70.                     "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);  
  71.             //返回热点打开状态  
  72.             return (Boolean) method.invoke(wifiManager, apConfig, enabled);  
  73.         } catch (Exception e) {  
  74.             return false;  
  75.         }  
  76.     }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值