在我的上一篇博客写了WiFi开发,这一篇自然就是热点开发,然后就是二者之间的通信了。如还不懂WiFi开发的参考上一篇博客http://blog.csdn.net/lhp15575865420/article/details/78357434
对于热点。我们需要能够打开和关闭,自己设置热点名和密码以及锁的类型。然后呢,还要能够获取连接此热点的设备信息。关于热点,没有找到相应的操作类,查阅资料后得知要用method反射机制来实现。反射机制不了解的自行百度。下面直接贴上相关代码及解析
public class Mywifiap {
private static final int NO_PASS=0;
private static final int WPA_PSK=1;
private static final int WPA2_PSK=2;
private WifiManager wifiManager;
private WifiConfiguration apconfig;
public Mywifiap(Context context) {
wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
//开启热点
public void openwifiap(String name,String password,int type){
if(wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(false);//如果WiFi是开启的就关闭WiFi。
apconfig=new WifiConfiguration();
apconfig.SSID=name;//设置WiFi名字
//热点相关设置
switch (type){
case NO_PASS:
apconfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
apconfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
apconfig.wepKeys[0]="";
apconfig.wepTxKeyIndex=0;
break;
case WPA_PSK:
apconfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
apconfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
apconfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
apconfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
apconfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
apconfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
apconfig.preSharedKey=password;
break;
case WPA2_PSK:
//由于wpa2是不能直接访问的,但是KeyMgmt中却有。所以我们这样写
for(int i=0;i<WifiConfiguration.KeyMgmt.strings.length;i++){
if("WPA2_PSK".equals(WifiConfiguration.KeyMgmt.strings[i])) {
apconfig.allowedKeyManagement.set(i)