使用WifiManager服务进行wifi相关设置实现

安卓API中提供了wifi的使用接口,在android.net.wifi包中。

既然使用了安卓系统接口,相关权限肯定少不了:
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


主要使用WifiManager类中提供的接口,因为WifiManager没有提供连接的状态是不是连接成功的接口,所以还需要用到ConnectivityManager提供的判断wifi的连接结果:连接中,已连接;而WifiManager中提供接口为wifi模块是否可以使用,是否被打开;

WifiInfo
DhcpInfo

这两个对象的实例化则是通过WifiManager类中提供的接口进行实例化;
mWifiInfo = mWifiManager.getConnectionInfo();
mDhcpInfo = mWifiManager.getDhcpInfo();


WifiInfo提供的接口有:
getMacAddress();
getSSID();
getBSSID();
int getIpAddress();
其中ssid就是我们能够看到wifi的名字了。
BSS:一种特殊的Ad-hoc LAN的应用,称为Basic Service Set (BSS),一群计算机设定相同的BSS名称,即可自成一个group,而此BSS名称,即所谓BSSID。

SSID(Service Set Identifier)也可以写为ESSID,用来区分不同的网络,最多可以有32个字符,无线网卡设置了不同的SSID就可以进入不同网络,SSID通常由AP广播出来,通过XP自带的扫描功能可以相看当前区域内的SSID。出于安全考虑可以不广播SSID,此时用户就要手工设置SSID才能进入相应的网络。简单说,SSID就是一个局域网的名称,只有设置为名称相同SSID的值的电脑才能互相通信。

初始化使用Wifi通常调用一下,即可扫描wifi;

mWifiManager.setWifiEnabled(true);
mWifiManager.startScan();
mWifiInfo = mWifiManager.getConnectionInfo();
mDhcpInfo = mWifiManager.getDhcpInfo();

还有部分接口没有完成,需要开发者自行获取转化;

如静态IP设置,网关转化成可以明白的字符串,wifi信号强度等;

//看到获取的IP地址是整形的都感觉哪里不对于是需要转化为正常人看到的字符串类型;
public String GetIPAddressStr() {
	if (mWifiInfo == null)
		return null;
	else {
		mWifiManager.reconnect();
		mWifiInfo = mWifiManager.getConnectionInfo();
		mDhcpInfo = mWifiManager.getDhcpInfo();			
		int iIP = mWifiInfo.getIpAddress();
		String strIP = Integer.toString(iIP & 0xFF) + "."
				+ Integer.toString((iIP >> 8) & 0xFF) + "."
				+ Integer.toString((iIP >> 16) & 0xFF) + "."
				+ Integer.toString((iIP >> 24) & 0xFF);
		Log.e(Tag, strIP);
		return strIP;
	}
}


//还有网关也要转
public String GetMaskStr() {
	if (mDhcpInfo == null)
		return null;
	else {
		int iMask = mDhcpInfo.netmask;
		String strMask = Integer.toString(iMask & 0xFF) + "."
				+ Integer.toString((iMask >> 8) & 0xFF) + "."
				+ Integer.toString((iMask >> 16) & 0xFF) + "."
				+ Integer.toString((iMask >> 24) & 0xFF);
		return strMask;
	}
}


//然后是wifi信号强度:
private final int MAX_RSSI = -50;
private final int MIN_RSSI = -100;

public int calculateSignalLevel(int rssi, int numLevels) {
	if (rssi <= MIN_RSSI) {
		return 0;
	} else if (rssi >= MAX_RSSI) {
		return numLevels - 1;
	} else {
		int partitionSize = (MAX_RSSI - MIN_RSSI) / (numLevels - 1);
		return (rssi - MIN_RSSI) / partitionSize;
	}
}

那么有必要设置静态IP的时候,还需要手动写入,没有提供现成类:
就写成一个类来调用吧;

public class WifiStaticIP {
public void setIpAssignment(String assign, WifiConfiguration wifiConf)
		throws SecurityException, IllegalArgumentException,
		NoSuchFieldException, IllegalAccessException {
	setEnumField(wifiConf, assign, "ipAssignment");
}

public void setIpAddress(InetAddress addr, int prefixLength,
		WifiConfiguration wifiConf) throws SecurityException,
		IllegalArgumentException, NoSuchFieldException,
		IllegalAccessException, NoSuchMethodException,
		ClassNotFoundException, InstantiationException,
		InvocationTargetException {
	Object linkProperties = getField(wifiConf, "linkProperties");
	if (linkProperties == null)
		return;
	Class<?> laClass = Class.forName("android.net.LinkAddress");
	Constructor<?> laConstructor = laClass.getConstructor(new Class[] {
			InetAddress.class, int.class });
	Object linkAddress = laConstructor.newInstance(addr, prefixLength);

	ArrayList<Object> mLinkAddresses = (ArrayList<Object>) getDeclaredField(
			linkProperties, "mLinkAddresses");
	mLinkAddresses.clear();
	mLinkAddresses.add(linkAddress);
}

public void setGateway(InetAddress gateway, WifiConfiguration wifiConf)
		throws SecurityException, IllegalArgumentException,
		NoSuchFieldException, IllegalAccessException,
		ClassNotFoundException, NoSuchMethodException,
		InstantiationException, InvocationTargetException {
	Object linkProperties = getField(wifiConf, "linkProperties");
	if (linkProperties == null)
		return;
	Class<?> routeInfoClass = Class.forName("android.net.RouteInfo");
	Constructor<?> routeInfoConstructor = routeInfoClass
			.getConstructor(new Class[] { InetAddress.class });
	Object routeInfo = routeInfoConstructor.newInstance(gateway);

	ArrayList<Object> mRoutes = (ArrayList<Object>) getDeclaredField(
			linkProperties, "mRoutes");
	mRoutes.clear();
	mRoutes.add(routeInfo);
}

public void setDNS(InetAddress dns, WifiConfiguration wifiConf)
		throws SecurityException, IllegalArgumentException,
		NoSuchFieldException, IllegalAccessException {
	Object linkProperties = getField(wifiConf, "linkProperties");
	if (linkProperties == null)
		return;
	ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>) getDeclaredField(
			linkProperties, "mDnses");
	mDnses.clear(); // or add a new dns address , here I just want to
					// replace DNS1
	mDnses.add(dns);
}

public Object getField(Object obj, String name)
		throws SecurityException, NoSuchFieldException,
		IllegalArgumentException, IllegalAccessException {
	Field f = obj.getClass().getField(name);
	Object out = f.get(obj);
	return out;
}

public Object getDeclaredField(Object obj, String name)
		throws SecurityException, NoSuchFieldException,
		IllegalArgumentException, IllegalAccessException {
	Field f = obj.getClass().getDeclaredField(name);
	f.setAccessible(true);
	Object out = f.get(obj);
	return out;
}

public void setEnumField(Object obj, String value, String name)
		throws SecurityException, NoSuchFieldException,
		IllegalArgumentException, IllegalAccessException {
	Field f = obj.getClass().getField(name);
	f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
}

public String getIpAssignment(WifiConfiguration wifiConf)
		throws SecurityException, IllegalArgumentException,
		NoSuchFieldException, IllegalAccessException {

	String output = getField(wifiConf, "ipAssignment").toString();
	return output;
}
}

关于Lock的问题:
内部类WifiManager.WifiLock
介绍
Allows an application to keep the Wi-Fi radio awake. Normally the Wi-Fi radio may turn off when the user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock is released. Multiple applications may hold WifiLocks, and the radio will only be allowed to turn off when no WifiLocks are held in any application. 

提供了相关接口
mWifiLock = mWifiManager.createWifiLock("WifiDemo");
mWifiLock.acquire();
---Locks the Wi-Fi radio on until release() is called.
mWifiLock.release();
----Unlocks the Wi-Fi radio, allowing it to turn off when the device is idle.

随意记录,转载请联系作者加上出处http://blog.csdn.net/dreamintheworld/article/details/42082943



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值