开启android手机的wifi热点
来源:互联网 发布:域名再长我也记得住 编辑:程序博客网 时间:2024/04/30 01:16
因为无线wifi不能和wufi热点同时打开,所以开启wifi热点的时候要监测是否打开了wifi,打开了就要关闭
- wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
- private void startWifiAp() {
- handler = new Handler();
- if (wifiManager.isWifiEnabled()) {
- closeWifiHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- startWifiApTh();
- super.handleMessage(msg);
- }
- };
- cwt = new CloseWifiThread();
- Thread thread = new Thread(cwt);
- thread.start();
- }else{
- startWifiApTh();
- }
- }
- class CloseWifiThread implements Runnable {
- public CloseWifiThread() {
- super();
- }
- @Override
- public void run() {
- int state = wifiManager.getWifiState();
- if (state == WifiManager.WIFI_STATE_ENABLED) {
- wifiManager.setWifiEnabled(false);
- closeWifiHandler.postDelayed(cwt, 1000);
- } else if (state == WifiManager.WIFI_STATE_DISABLING) {
- closeWifiHandler.postDelayed(cwt, 1000);
- }else if(state == WifiManager.WIFI_STATE_DISABLED){
- closeWifiHandler.sendEmptyMessage(0);
- }
- }
- }
- private void startWifiApTh(){
- swat = new StratWifiApThread();
- Thread thread = new Thread(swat);
- thread.start();
- }
- class StratWifiApThread implements Runnable {
- public StratWifiApThread() {
- super();
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- WifiUtil mWifiUtil = new WifiUtil();
- int state = mWifiUtil.getWifiApState(wifiManager);
- if (state == WifiUtil.WIFI_AP_STATE_DISABLED) {
- mWifiUtil.stratWifiAp(wifiManager);
- handler.postDelayed(swat, 1000);
- } else if (state == WifiUtil.WIFI_AP_STATE_ENABLING
- || state == WifiUtil.WIFI_AP_STATE_FAILED) {
- handler.postDelayed(swat, 1000);
- }else if (state == WifiUtil.WIFI_AP_STATE_ENABLED){
- Toast.makeText(ActivityForAndroid.this, "已开启wlan热点", 2000).show();
- }
- }
- }
- public class WifiUtil {
- public static final int WIFI_AP_STATE_DISABLING = 0;
- public static final int WIFI_AP_STATE_DISABLED = 1;
- public static final int WIFI_AP_STATE_ENABLING = 2;
- public static final int WIFI_AP_STATE_ENABLED = 3;
- public static final int WIFI_AP_STATE_FAILED = 4;
- public void stratWifiAp(WifiManager wifiManager) {
- // WifiManager wifi = (WifiManager)
- // getSystemService(Context.WIFI_SERVICE);
- // wifiManager.setWifiEnabled(false);
- Method method1 = null;
- try {
- method1 = wifiManager.getClass().getMethod("setWifiApEnabled",
- WifiConfiguration.class, boolean.class);
- WifiConfiguration netConfig = new WifiConfiguration();
- //wifi热点名字
- netConfig.SSID = "\" T E S T \"";
- netConfig.allowedAuthAlgorithms
- .set(WifiConfiguration.AuthAlgorithm.OPEN);
- netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
- netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
- netConfig.allowedKeyManagement
- .set(WifiConfiguration.KeyMgmt.WPA_PSK);
- netConfig.allowedPairwiseCiphers
- .set(WifiConfiguration.PairwiseCipher.CCMP);
- netConfig.allowedPairwiseCiphers
- .set(WifiConfiguration.PairwiseCipher.TKIP);
- netConfig.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.CCMP);
- netConfig.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.TKIP);
- //密码
- netConfig.preSharedKey = "\"11111111\"";
- method1.invoke(wifiManager, netConfig, true);
- // Method method2 =
- // wifiManager.getClass().getMethod("getWifiApState");
- // int state = (Integer) method2.invoke(wifiManager);
- // Log.i("wifi state" + state);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public int getWifiApState(WifiManager wifiManager) {
- try {
- Method method = wifiManager.getClass().getMethod("getWifiApState");
- int i = (Integer) method.invoke(wifiManager);
- Log.i("wifi state: " + i);
- return i;
- } catch (Exception e) {
- Log.i("Cannot get WiFi AP state" + e);
- return WIFI_AP_STATE_FAILED;
- }
- }
- }
上面的代码用到handler每隔1秒post线程来监测是否关闭了wifi 和是否打开了wifi热点..不知道还有没有其他更好的方法..
- 开启android手机的wifi热点
- 开启android手机的wifi热点 .
- 开启android手机的wifi热点
- android开启手机wifi热点和连接到wifi热点
- Android 开启指定名称的WiFi热点
- 打开android手机的wifi热点
- android手机wifi热点通信
- Android酱油笔记之开启wifi热点
- Deepin Linux系统中开启ap-hotspot wifi热点供其他计算机和android手机上网
- Ubuntu 创建android手机能够连接的Wifi热点
- ubuntu 开启无线网卡的wifi热点
- 开启win7的虚拟WiFi 热点
- Ubuntu开启WiFi热点(安卓手机可识别)
- Win7开启wifi热点
- 电脑开启wifi热点
- win10开启wifi热点
- WIFI开启扫描热点
- 笔记本开启wifi热点
- 关于工作和跳槽
- mysql修改默认编码格式
- 敏捷杂谈
- source navigator启动报错
- Activity生命周期
- 开启android手机的wifi热点
- 简单的luence例子
- 在视图类中怎么进行分割,创建子视图?
- 如何看一些俄文注释的代码
- Linux Qt编程——在程序中启动其他应用、脚本
- python 二叉查找树
- 青蛙又回到了井里
- java.lang.Instrument 动态修改替换类代码
- queue