二话不说,直接上代码解决。
1.WifiApAdmin.java
package com.example.wifierdian;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
/**
* 创建热点
*
*/
public class WifiApAdmin {
public static final String TAG = "WifiApAdmin";
public void closeWifiAp(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
closeWifiAp(wifiManager);
}
private WifiManager mWifiManager = null;
private Context mContext = null;
public WifiApAdmin(Context context) {
mContext = context;
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
closeWifiAp(mWifiManager);
}
private String mSSID = "";
private String mPasswd = "";
public void startWifiAp(String ssid, String passwd) {
mSSID = ssid;
mPasswd = passwd;
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
stratWifiAp();
MyTimerCheck timerCheck = new MyTimerCheck() {
@Override
public void doTimerCheckWork() {
// TODO Auto-generated method stub
if (isWifiApEnabled(mWifiManager)) {
Log.v(TAG, "Wifi enabled success!");
this.exit();
} else {
Log.v(TAG, "Wifi enabled failed!");
}
}
@Override
public void doTimeOutWork() {
// TODO Auto-generated method stub
this.exit();
}
};
timerCheck.start(15, 1000);
}
public void stratWifiAp() {
Method method1 = null;
try {
method1 = mWifiManager.getClass().getMethod("setWifiApEnabled",
WifiConfiguration.class, boolean.class);
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = mSSID;
netConfig.preSharedKey = mPasswd;
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);
method1.invoke(mWifiManager, netConfig, true);
} 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();
}
}
private static void closeWifiAp(WifiManager wifiManager) {
if (isWifiApEnabled(wifiManager)) {
try {
Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
method.setAccessible(true);
WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);
Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method2.invoke(wifiManager, config, false);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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();
}
}
}
private static boolean isWifiApEnabled(WifiManager wifiManager) {
try {
Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifiManager);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
2.MyTimerCheck.java
package com.example.wifierdian;
public abstract class MyTimerCheck {
private int mCount = 0;
private int mTimeOutCount = 1;
private int mSleepTime = 1000; // 1s
private boolean mExitFlag = false;
private Thread mThread = null;
/**
* Do not process UI work in this.
*/
public abstract void doTimerCheckWork();
public abstract void doTimeOutWork();
public MyTimerCheck() {
mThread = new Thread(new Runnable() {
@SuppressWarnings("static-access")
@Override
public void run() {
// TODO Auto-generated method stub
while (!mExitFlag) {
mCount++;
if (mCount < mTimeOutCount) {
doTimerCheckWork();
try {
mThread.sleep(mSleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
exit();
}
} else {
doTimeOutWork();
}
}
}
});
}
/**
* start
* @param times How many times will check?
* @param sleepTime ms, Every check sleep time.
*/
public void start(int timeOutCount, int sleepTime) {
mTimeOutCount = timeOutCount;
mSleepTime = sleepTime;
mThread.start();
}
public void exit() {
mExitFlag = true;
}
}
3.上两步加入后,这里可以开始运用了。
private Button open;
private Context mContext = null;
String HotSpot_name;
String keys;
TextView textv;
Button close;
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HotSpot_name = "hotspotbyandroid";//热点名称
keys = "1234567890";//热点密码
mContext = this;
open=(Button)findViewById(R.id.open_hotspot);
close=(Button)findViewById(R.id.close_hotspot);
textv = (TextView)findViewById(R.id.textv);
final WifiApAdmin wifiAp = new WifiApAdmin(mContext);
open.setText("点击创建热点");
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiAp.startWifiAp(HotSpot_name, keys); //启动热点
textv.setText("HotSpot_name:"+HotSpot_name+"\nkeys:"+keys);
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiAp.closeWifiAp(mContext);//关闭热点
textv.setText("已关闭热点");
}
});
}
4.最后还有个重点,权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>