Android Open Source - ConnectedSpace Android Address Utils






From Project

Back to project page ConnectedSpace.

License

The source code is released under:

Copyright (c) 2015, Shreyas Raman <skraman1999@gmail.com>.

If you think the Android project ConnectedSpace listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.shreyasr.connected.android.util;
//from www  .j  a  va 2 s  . c  o  m
import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import org.apache.http.conn.util.InetAddressUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;

/**
 * Based off of http://stackoverflow.com/a/13007325/2197700
 */
public class AndroidAddressUtils {

    public static InetAddress getIPAddress() {
        List<NetworkInterface> interfaces;
        try {
            interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        } catch (SocketException e) {
            Log.e("ERROR", "Failed to get network interfaces");
            return null;
        }

        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress() && InetAddressUtils.isIPv4Address(addr.getHostAddress().toUpperCase())) {
                        return addr;
                }
            }
        }

        return null;
    }

    public static InetAddress getBroadcastAddress(Context mContext) throws UnknownHostException {
        WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifi.getDhcpInfo();

        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);
    }
}




Java Source Code List

me.shreyasr.connected.ConnectedSpace.java
me.shreyasr.connected.android.AndroidLauncher.java
me.shreyasr.connected.android.activity.ClientListAdapter.java
me.shreyasr.connected.android.activity.LobbyActivity.java
me.shreyasr.connected.android.networking.BroadcastListenerThread.java
me.shreyasr.connected.android.networking.ConnectionThread.java
me.shreyasr.connected.android.networking.HeartbeatThread.java
me.shreyasr.connected.android.networking.LobbyManager.java
me.shreyasr.connected.android.networking.ServerSocketThread.java
me.shreyasr.connected.android.util.AndroidAddressUtils.java
me.shreyasr.connected.android.util.Client.java
me.shreyasr.connected.desktop.DesktopLauncher.java
me.shreyasr.connected.entities.Laser.java
me.shreyasr.connected.entities.Star.java
me.shreyasr.connected.network.NetworkHandler.java
me.shreyasr.connected.util.MathHelper.java
me.shreyasr.connected.util.SpriteSheet.java