[Android]获取局域网广播地址的两种方法
2013-10-30 13:20
197 查看
第一种是自己写的,有一些bug,不过凑合着用也可以,第二种是stackoverflow的,推荐用这种。
(1)
(2)
(1)
private InetAddress calcBroadcastAddress(InetAddress mask, InetAddress ip) throws IOException { int ipaddress = getIntAddress(ip); int maskaddress = getIntAddress(mask); int broadcast = ipaddress & maskaddress | ~maskaddress; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) { quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); } return InetAddress.getByAddress(quads); } private InetAddress getBroadcastAddress() throws IOException { // 自动获取 String strIp = getCommandResult("/system/bin/getprop", "dhcp.eth0.ipaddress"); String strMask = getCommandResult("/system/bin/getprop", "dhcp.eth0.mask"); if (!strIp.equals("\n")) { InetAddress mask = InetAddress.getByName(strMask); InetAddress ip = InetAddress.getByName(strIp); Log.v("ws-discovery", "auto ip:" + strIp); return calcBroadcastAddress(mask, ip); } else { // 手动配置 String strManual = getCommandResult("/system/bin/ifconfig", "eth0"); String[] netInfo = strManual.split(" "); if (netInfo.length >= 5) { InetAddress mask = InetAddress.getByName(netInfo[4]); InetAddress ip = InetAddress.getByName(netInfo[2]); Log.v("ws-discovery", "manual ip:" + netInfo[2]); return calcBroadcastAddress(mask, ip); } } // wifi WifiManager wifi = (WifiManager) mContext .getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); Log.v("ws-discovery", "wifi ip:" + Integer.toString(dhcp.ipAddress)); int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) { quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); } return InetAddress.getByAddress(quads); } private int getIntAddress(InetAddress address) { byte[] addrs = address.getAddress(); int addr = 0; addr = ((addrs[3] & 0xff) << 24) | ((addrs[2] & 0xff) << 16) | ((addrs[1] & 0xff) << 8) | (addrs[0] & 0xff); return addr; } private String getCommandResult(String commands, String args) { Process process = null; String inet = null; try { process = new ProcessBuilder().command(commands, args) .redirectErrorStream(true).start(); InputStream in = process.getInputStream(); int count = 0; while (count == 0) { count = in.available(); } byte[] b = new byte[count]; in.read(b); inet = new String(b); } catch (IOException e) { e.printStackTrace(); } finally { if (process != null) process.destroy(); } return inet; }
(2)
public static String getBroadcast() throws SocketException { System.setProperty("java.net.preferIPv4Stack", "true"); for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum.hasMoreElements();) { NetworkInterface ni = niEnum.nextElement(); if (!ni.isLoopback()) { for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) { if (interfaceAddress.getBroadcast() != null) { return interfaceAddress.getBroadcast().toString().substring(1); } } } } return null; }
相关文章推荐
- [Android]获取局域网广播地址的两种方法
- Android /java获取系统广播地址、ip地址、子网掩码的方法
- Android平台上捕获Back键的事件,Back键是手机上的后退键,一般的软件不捕获相关信息可能导致你的程序被切换到后台,而回到桌面的尴尬情况,在Android上有两种方法来获取该按钮的事件
- Android获取屏幕分辨率的两种方法及比较
- Android获取本机Mac地址及IP地址方法
- Android_获取屏幕大小的两种方法
- Android从Camera中获取图片的两种方法
- 获取 android 设备 mac 地址的方法
- android 中获取屏幕大小的两种方法
- 获取网页URL地址及参数等的两种方法(js和C#)
- android获取屏幕宽高的两种方法
- Android 获取手机中的图片信息的两种方法
- 获取网页URL地址及参数等的两种方法(js和C#)
- Android中获取状态栏高度的两种方法分享
- android应用 获取本地指定类型文件 的两种最优方法
- Android编程实现基于局域网udp广播自动建立socket连接的方法
- Android开发中获取本机Mac地址和IP地址的方法
- js获取地址栏中传递的参数(两种方法)
- Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法
- Android OnCreate()中获取控件高度与宽度两种方法详解