2

I am working in Android marshmallow api's which enables user to work on both LTE and WiFi .(i.e) we can force our application to use LTE using hipri network even when the wifi is turned ON by setting network type.

I checked this link: Send request over Mobile data when WIFI is ON.(Android L)

    builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);

    mNetworkCallback =
            new NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    super.onAvailable(network);
                    Log.d(TAG, "activate(): onAvailable(): " + network);
                    myConnManager.bindProcessToNetwork(network);
                    myCallback.networkStateChanged(State.CONNECTED);
                }

                @Override
                public void onLosing(Network network, int maxMsToLive) {
                    super.onLosing(network, maxMsToLive);
                    Log.d(TAG, "activate(): onLosing(): ms to live: " + maxMsToLive);
                    myCallback.networkStateChanged(State.DISCONNECTING);
                }

                @Override
                public void onLost(Network network) {
                    super.onLost(network);
                    Log.d(TAG, "activate(): onLost(): " + network);
                    myConnManager.bindProcessToNetwork(null);
                    myCallback.networkStateChanged(State.DISCONNECTED);
                }
            };

Now I am working on getting the IP address of both the connected WiFi network and LTE network. I am not sure how to retrieve the IP address of both connected networks in parallel.

Any help here would be appreciable.

Community
  • 1
  • 1
Ajitha
  • 717
  • 1
  • 7
  • 30
  • "Retrieve the IP address" --- ?. I think you want to do a DHCP connection on both your interfaces(LTE and WiFi). Check if there is any API for a DHCP connection request. – Jay Dec 10 '15 at 10:33

1 Answers1

2

Here is how you may get the InetAddress out of a Network network object:

ConnectivityManager manager = getSystemService(ConnectivityManager.class);
LinkProperties prop = manager.getLinkProperties(network);
InetAddress addr = prop.getLinkAddresses().get(0).getAddress();

In your case, you may use this in any of the NetworkCallback methods, you'll get the IP address of your mobile data connection (altough I suspect it only make sense in onAvailable()). In API 23, you may use getActiveNetwork() to get a Network object corresponding to the "currently active default data network", that should be Wi-Fi (however, legacy means to get the IP address should also point to this one).

cuihtlauac
  • 1,808
  • 2
  • 20
  • 39
  • BTW, there was a bug in 6.0 which prevented using `requestNetwork()`, it is fixed in 6.0.1. – cuihtlauac Dec 11 '15 at 08:39
  • Thanks cuihtlauac. NowI have another query. I am in a wifi call... When the wifi drops connectivity , I wanna connect it with the data network and keep the call going.... This is possible when I register myself with the data network that I am going to connect with. For this I need the IP of data connection. So we are thinking to make the wifi->data transition manual via code when the wifi strength is becoming less than 1.So that we will have both the IP's. Can I force the packet to go in data and wifi seamlessly in same call? – Ajitha Dec 11 '15 at 09:36
  • Sorry, I've never attempted to do anything like that. Looks like a big topic, probably worth asking another question :-) – cuihtlauac Dec 11 '15 at 09:48