Get my phone's Internal ip - android

My phone has an IP.
I would like to know how could I retrieve it?
After searching in the web, I found that i can get only the current outside ip.(I want the local-perm ip of my phone)
Thanks,
ray.

A quick search on google sent me here:
http://www.droidnova.com/get-the-ip-address-of-your-device,304.html
Read the comments about how to use the first block of code to get the wifi ip address (on local network, not public ip)
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
Edit:
The emulator seems to return 0 on wifiInfo.getIpAddress(), but it works fine on a phone.
The following code converts the integer to an ip address:
String ipBinary = Integer.toBinaryString(ipAddress);
//Leading zeroes are removed by toBinaryString, this will add them back.
while(ipBinary.length() < 32) {
ipBinary = "0" + ipBinary;
}
//get the four different parts
String a=ipBinary.substring(0,8);
String b=ipBinary.substring(8,16);
String c=ipBinary.substring(16,24);
String d=ipBinary.substring(24,32);
//Convert to numbers
String actualIpAddress =Integer.parseInt(d,2)+"."+Integer.parseInt(c,2)+"."+Integer.parseInt(b,2)+"."+Integer.parseInt(a,2);

your ip will change with every networkthat you connect to - your phone has a mac address - is that what you are looking to find?

The IP which I may would call "local-perm" is the localhost IP which is always 127.0.0.1
The IP of any other network adapter (Wifi in this case) changes depending on the network

Related

How to get correct IPv6 Ip address?

I am trying to display the ip address of connected networks in android. I am using the following code. But it returns 2 IPv6 ip address. How to find the correct ip address from that?
I am using the following code:
List<LinkAddress> linkAddresses = connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getLinkAddresses();
for (LinkAddress linkAddress : linkAddresses) {
Log.i("","LinkAddress getAddress "+linkAddress.getAddress() + "");
Log.i("","Is IPV6 " + (linkAddress.getAddress() instanceof Inet6Address) +"");
Log.i("","Is IPV4 " + (linkAddress.getAddress() instanceof Inet4Address) +"");
Log.i("","Is isLinkLocalAddress " + (linkAddress.getAddress().isLinkLocalAddress()) +"");
Log.i("","Is not isLoopbackAddress " + (!linkAddress.getAddress().isLoopbackAddress()) +"");
}
Now I am getting 4 ip addresses.
LinkAddress getAddress /fe80::2d0:caff:fe00:5ad6
LinkAddress getAddress /2401:4900:2305:14e:2d0:caff:fe00:5ad6
LinkAddress getAddress /2401:4900:2305:14e:28e2:5192:e38f:3e9
LinkAddress getAddress /192.168.43.176
I can identify fe80 is Link Local Ip address and 192. is IPv4 address. But I am confused to identify IPV6 address from this. Please help me to find out the IPv6 ip address.
Both IPv6 addresses are valid. It's normal to have multiple addresses per interface. Both are in the same subnet (2401:4900:2305:14e::/64). When you look at the interface ID (the second half of the address) you'll see that one has ..ff:fe.. in the middle. That's a sign that that address is probably derived from the MAC address of the interface. The other address is a temporary address that will change over time to protect the privacy of the user.
But in short: both addresses are completely valid and usable.

Trying to get network device names with reverse dns in Android

I can able to fetch all device ip addresses in Local Area Network with inetaddress class. What I need to do is reverse lookup ip-address and find as device name in network like : "Jimmie's Macbook"
My Code block which able to fetch all IP address over Local Network Range:
private ArrayList<String> scanSubNet(String subnet) {
ArrayList<String> hosts = new ArrayList<>();
InetAddress inetAddress;
for (int i = 1; i <= 255; i++) {
try {
inetAddress = InetAddress.getByName(subnet + String.valueOf(i));
if (inetAddress.isReachable(1000)) {
hosts.add(inetAddress.getHostName());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getHostAddress());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getCanonicalHostName());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return hosts;
}
And i am calling my method as;
ArrayList<String> subnetList = scanSubNet("192.168.1.");ArrayList<String> subnetList = scanSubNet("192.168.1.");
in Log.d(TAG, i am trying to get device name with reverse dns. But both of line gives me output as ip-address ( Not Device-Name as string)
Is there any chance to succeed it ?
Regards,
Onder.
I just do it with fetching MACID and match first 3digits which belongs manufacturers.
https://macvendors.com/ this website also provide api (Post/GET) to resolve MAC Address.
Instead of resolve fullname of MAC, you need to do Handshake peer to peer.
This is probably happening due to router misconfiguration.
Within a LAN, there are no crucial functions that depend on successful reverse DNS lookups, so a misconfiguration of that kind can easily go undetected for a long time.
It is kind of hard to tell what is wrong in your particular case without a lot more information about your LAN, but the first thing that comes to mind is configuring a proper "DNS Suffix" on the router. This is usually found under DHCP settings.

get IP address but in xxx.xxx.xxx.xxx format in Android

Hi guys I've gone through a lot of coding on line to get my android mobiles IP address
Most of them are ending with
if (!inetAddress.isLoopbackAddress())
{ return inetAddress.getHostAddress().toString(); }
How ever i get something that looks like this:- "fe80::a00:27ff:fe37:28b5%eth1"
Weird cause I was expecting something like xxx.xxx.xxx.xxx
Can some one help me understand whats this?
That is an IPv6 address. Additionally, since it starts with fe80:: you know it's also a link-local IPv6 address, so cannot be used for communication beyond the local network. (in this case, eth1, since that is the scope specified at the end after the % - but note that using a % to identify the scope isn't always valid when using an IPv6 address.)
Try java.net.Inet4Address instead.
Its returning IPV6 address.
Check if its IPV4 address before returning result.
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress().toString();
}
Hope this helps.

PJSIP VOIP call not connected using SIP2SIP.info server

i am using PJSIP for voice calling. When i used our server, everything fine i.e. call connected, communicate. But when i am using SIP2SIP.INFO server. Registration is OK But Call is not connected. i saw log in SIP2SIP.info there wasn't log of outgoing or incoming call.
so call is not initiate.
char cfg_reg_uri[] = "sip:sip2sip.info";
char cfg_cred_realm[] = "sip2sip.info";
char cfg_cred_scheme[]="digest";
pjsua_acc_config cfg;
pjsua_acc_config_default(&cfg);
cfg.id = pj_str(cfg_id);
cfg.reg_uri = pj_str(cfg_reg_uri);
cfg.cred_count = 1;
cfg.cred_info[0].realm = pj_str(cfg_cred_realm);
cfg.cred_info[0].scheme = pj_str(cfg_cred_scheme);
cfg.cred_info[0].username = pj_str(cfg_cred_username);
cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
cfg.cred_info[0].data = pj_str(cfg_cred_password);
status = pjsua_acc_add(&cfg, PJ_TRUE, &_acc_id);
I noted that we need to use outbound proxy in sip2sip called "proxy.sipthor.net".
but confused how can i used in pjsip code.
please help expert.
If you read the Sip2Sip device configuration page it states that:
" the SIP device must always perform DNS lookups as defined in SIP standard RFC3263 (NAPTR + SRV + A DNS lookups)"
PJSIP supports DNS SRV lookups.
In PJSUA it will only do DNS SRV lookup if you don't provide the port number in the SIP URL.
"sip:xxx#sip2sip.info" will try to do a DNS SRV record lookup first then fail over to DNS A/C name lookup.
and
"sip:xxx#sip2sip.info:5060" will only do DNS A/C name lookup.
What PJSUA will not support automatically is failover support, they say:
"What we've been suggesting is to implement the failover mechanism in the application layer."
If you want a "quick and easy" setup, what you want to do is set the outbound_proxy to "proxy.sipthor.net". e.g.
cfg.outbound_proxy_cnt = 1;
cfg.outbound_proxy[0] = pj_str("sip:proxy.sipthor.net:5060");
If you want a more robust solution, you need to use pjsip's SRV resolution functions to resolve sip2sip.info srv record e.g: "_sip._udp.sip2sip.info" and then set the outbound_proxy records with the result.
The code is a little bit involved.
pjsip_resolver_t* resolver_;
...
status = pjsip_resolver_create( pool, &resolver_ );
...
pjsip_host_info host;
host.flag = PJSIP_TRANSPORT_DATAGRAM; // is using UDP, see pjsip_transport_flags_e
host.type = PJSIP_TRANSPORT_UDP; // if using UDP, see pjsip_transport_type_e
host.addr.host = pj_str("sip2sip.info");
host.addr.port = 5060;
pjsip_resolve(resolver_, pool, &host, token, resolver_cb_func);
...
static void resolver_cb_func( pj_status_t status, void *token, const struct pjsip_server_addresses *addr)
{
...
// use results to fill in the outbound_proxy
}
You could also take it further to support failover, but it looks like sip2sip doesn't have multiple sip servers in there DNS SRV record so it will not be used currently. If they ever add more then it would become more useful.
_sip._udp.sip2sip.info
Server: fritz.box Address: fd00::2665:11ff:fef9:ec51
Non-authoritative answer:
_sip._udp.sip2sip.info SRV service location:
priority = 100
weight = 100
port = 5060
svr hostname = proxy.sipthor.net
sip2sip.info nameserver = ns2.dns-hosting.info
sip2sip.info nameserver = ns1.dns-hosting.info
sip2sip.info nameserver = ns7.dns-hosting.info
Sip2Sip also support STUN setup, so I would also setup the STUN settings on the account as well:
cfg.stun_srv_cnt = 1;
cfg.stun_srv[0] = pj_str("sip2sip.info");
Since your example seems to not provide the port information it should work. To diagnose this further would require see the pjsip log output.

Unable to configure the wifi open network programmatically android?

I have written code for adding open network to wifi configured list.It adds the open network to configured lists and displays the same SSID's in Wifi Settings.but it adds the same network with same name extra but it doesn't shows any open network When i press on the second on alert shows with Security WEP the following text i observed with those same networks
1) Open network
2) remembered , not in range
But i want to add open network into my list, why this extra one is added and if i connect the same network it is trying to connect to the (2) one programmatically.Actually I changed secured network to open network for this trial.It displays with open network text and when i press on that one it obtains the address and connects succesfully manually.Why this extra one is adding how can i add the open network to my list.For reference plz see the link for image.
http://www.freeimagehosting.net/uploads/3dbccfc2bd.png
Code snippet :
String hotSpotSsid = hotSpot.SSID;
String hotSpotBssid = hotSpot.BSSID;
Log.i(TAG,"in RSSI Changed Acion SSID: "+hotSpotSsid+" BSSID: "+hotSpotBssid);
StringBuffer sBuf = new StringBuffer("\"");
sBuf.append(hotSpotSsid+"\"");
hotSpotSsid = sBuf.toString();
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = hotSpotSsid;
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
wifiConfiguration.BSSID = hotSpotBssid;
wifiConfiguration.hiddenSSID = false;
// wifiConfiguration.priority = 1;
// add this to the configured networks
int inetId = wifiManager.addNetwork(wifiConfiguration);
Log.i(TAG,"INetId :"+inetId);
configs = wifiManager.getConfiguredNetworks();
Log.e(TAG,"After adding config :"+configs);
if(inetId < 0) {
Log.i(TAG,"Unable to add network configuration for SSID: "+hotSpotSsid);
return;
}else {
message="\t Successfully added to configured Networks";
Log.i(TAG,message);
}
regards,
Rajendar
Try removing SSID and seeing if it works. I was having a similar issue and that worked for me.
Appending and prepending quotes to the SSID, as you did, should work though. Not sure why it doesn't.
Give SSID as wifiConfiguration.SSID = "\"".concat(SSID_NAME).concat("\"");

Categories

Resources