package com.magicleap.network;

import android.content.Context;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.net.wifi.WifiManager;
import java.nio.ByteOrder;

public class WifiInfoGathering {

	public static String GetWifiIpv4Address() {
		Context context = UnityPlayer.currentActivity;
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		int ipAddress = wifiManager.getConnectionInfo().getIpAddress();

		if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN))
			ipAddress = Integer.reverseBytes(ipAddress);

		byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();

		String ipAddressString;
		try {
			ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
		} catch (UnknownHostException ex) {
			Log.e("MagicleapSpectator", "Unable to get host address.");
			ipAddressString = "";
		}

		return ipAddressString;
	}
}
