Do the Wifi-Direct Ip_address and Ip address given by any WIFI router to the smart-phone same? I am below code to get my IP address.
public String getP2PIpAddr() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_P2P_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipString = String.format(
"%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
return ipString;
}
but its giving me 0.0.0.0 when i am not connected to any wifi router.But it is returning me IP address given by router to phone but not the wifi-direct IPAddress..
Please Help...
Thanks In advance..
No the IP addresses are not the same. You are making mistakes here,
getSystemService(WIFI_P2P_SERVICE)
returns WifiP2pManager notWifiManager
.Secondly You use
wifiManager.getConnectionInfo()
when you are connected to a normal Wifi network i.e. via a router or hotspot. You do not get IP when connected via WifiDirect like this as it is not a normal wifi network. Thus, the reason you are getting IP as0.0.0.0
.Now the main the question what are the IPs in WifiDirect. If you are a GroupOwner you have fixed GO_IP =
192.168.43.1
. So, in onConnectionInfoAvailable or onGroupInfoAvailable, you can doinfo.isGroupOwner()
and if you are not the groupOwner then the other system is, hence his IP is GO_IP, then this system can communicate to the GO using GO_IPNow if GO wants to communicate with client, he can get his IP using
NetworkInterfaces
. Refer this how to get the client IP.I think this all should solve your problem.