获取手机wifi信息
/**
* 获取Wifi的Mac地址
* @param context
* @return
*/
public static String getWifiBssid(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if(info != null){
return info.getBSSID();
}
return "";
}
/**
* 获取Wifi的名称
* getSSID()获取到的SSID名称和手机的Android版本有关,
* 如果是Android 4.2(API Level = 17)及其以上的版本,获取到的SSID名称是有双引号的, "Wifi"
* 如果是Android 4.2以下的版本,获取到的SSID名称没有双引号, Wifi
* @param context
* @return
*/
public static String getWifiSsid(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if(info != null){
return info.getSSID();
}
return "";
}
有的时候, Wifi并没有连接成功,这个时候,我们可以通过scanResult来扫描周围的热点:
String wserviceName = Context.WIFI_SERVICE;
WifiManager wm = (WifiManager) getSystemService(wserviceName);
List<ScanResult> wifiList = wm.getScanResults();
for (int i = 0; i < wifiList.size(); i++) {
ScanResult result = wifiList.get(i);
//do something about result.BSSID;
}
/**
* 获取网络的ID,有时候名称一样,就必须从NetworkID来判断是否连着同一个网络
* @param context
* @return
*/
public static int getNetworkId(Context context){
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if(info != null){
return info.getNetworkId();
}
return -1;
}
public static final String LINK_SPEED_UNITS = "Mbps";
/**
* 获取当前网络连接速度,以单位 "Mbps" 计算
* @param context
* @return
*/
public static int getLinkSpeed(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if(info != null){
return info.getLinkSpeed();
}
return 0;
}
对于wifi信息,需要添加权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
/**
* 获取IMEI号
* @param context
* @return
*/
public static String getIMEI(Context context) {
String imei = null;
try {
TelephonyManager telephonyMgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
imei = telephonyMgr.getDeviceId();
} catch (Exception e) {
e.printStackTrace();
}
return imei;
}
对于IMIE信息,需要添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
*获取手机品牌和型号
String model = android.os.Build.MODEL; //型号
String brand = android.os.Build.BRAND; //品牌
监听网络连接发生变化
可能在有些情况下,我们会想在网络变化的情况下去判断做变化,为此我们需要监听系统广播,获取网络变化的通知,并进行处理
public class NetworkConnectReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
// 当网络发生变化时候
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
try {
// 判断是否连接到了wifi
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
if (State.CONNECTED == state) {
// dosomething
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
需要添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
原文地址:http://blog.csdn.net/linmiansheng/article/details/44517163