本文主要介绍android与wifi交互的一些应用技巧。常见的场景有:检测wifi,连接wifi,打开wifi设置,检测wifi网络变化,只允许wifi网络下载,获取wifi名称,检测网速等。废话不说,直接贴代码,easy,权当笔记咯。
普及下基本知识:
1.WiFi的五种状态
1 2 3 4 5
| WifiManager.WIFI_STATE_DISABLED: WifiManager.WIFI_STATE_DISABLING: WifiManager.WIFI_STATE_ENABLED: WifiManager.WIFI_STATE_ENABLING: WifiManager.WIFI_STATE_UNKNOWN:
|
2.需要注册的权限
1 2 3 4
| <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /><!-- 允许程序改变网络状态 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><!--允许程序访问WIFI网络状态信息 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/><!-- 允许程序改变WIFI状态 --> <uses-permission android:name="android.permission.INTERNET"/><!-- 允许程序访问网络-->
|
3.判断是否为wifi网络
1 2 3 4 5 6 7 8 9 10
| */ public static boolean isWifi(Context context) { ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo=cm.getActiveNetworkInfo(); if (networkInfo != null&& networkInfo.getType()==ConnectivityManager.TYPE_WIFI) { return true; } return false; }
|
4.获取wifi的名称
1 2 3 4 5 6
| public static String getCurrentWifiName(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String ssid = wifiInfo != null ? wifiInfo.getSSID() : ""; return ssid.replace("\"","").replace("<unknown ssid>",""); }
|
5.判断wifi的状态(是否开启)
1 2 3 4 5 6 7 8 9 10 11 12
| public static boolean checkWifiState(Context context) { ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if (state== NetworkInfo.State.DISCONNECTED||state== NetworkInfo.State.DISCONNECTING){ LogUtils.debug("wifi closed "); return false; }else{ LogUtils.debug("wifi connected "); return true; } }
|
6.判断wifi是否连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public boolean isWiFiActive(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] infos = connectivity.getAllNetworkInfo(); if (infos != null) { for (NetworkInfo ni : infos) { if (ni.getTypeName().equals("WIFI") && ni.isConnected()) { return true; } } } } return false; }
|
7.获取wifi网络对应的IP地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static String getLocalIP(Context context) { if (context==null){ return ""; } WifiManager wifiService = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiinfo = wifiService.getConnectionInfo(); int ip = wifiinfo.getIpAddress(); if (ip==0){ return ""; } String ip_address = intToIp(ip); return ip_address; } public static String intToIp(int i) { return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF); }
|
8.打开wifi系统设置界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static void openWifiConfig(Activity activity, int requestCode) { Intent intent = null; if (android.os.Build.VERSION.SDK_INT > 10) { intent = new Intent(Settings.ACTION_WIFI_SETTINGS); } else { intent = new Intent(); ComponentName component = new ComponentName( "com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(component); intent.setAction("android.intent.action.VIEW"); } if (requestCode<0){ activity.startActivity(intent); }else { activity.startActivityForResult(intent, requestCode); } }
|
9.通过wifi与智能硬件交互
本质上和普通wifi网络没有差别,一般需要将当前所连接的wifi IP地址最后的那段改为1,即为server的服务地址。视情况而定,比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| * 根据本机IP,获取当前serve访问地址 */ public static String formatIPAddress(String ip_address) { try{ if (ip_address!=null&&!"".equals(ip_address)) { ip_address = ip_address.substring(0, ip_address.lastIndexOf(".")); ip_address = "http://" + ip_address + ".1:8080/"; } }catch (Exception e) { LogUtils.debug(e.toString()); } return ip_address; }
|
欢迎交流,未完待续!
更多精彩,访问Dusan’s blog :duqian.site
1
| Dusan_杜乾:Q:291902259,公众号:OpenDeveloper
|