GetIPAdress question

93 views
Skip to first unread message

Manuel Lopes

unread,
May 17, 2016, 8:53:34 AM5/17/16
to DroidScript
is this only show by wifi?or work if internet of mobile is on?
if i try the example of sample show ip
0.0.0.0
is normal in internet of mobile or only work with wifi?
sample:
function OnStart()
{
  ip = app.GetIPAddress();
  app.ShowPopup( ip );
}

Steve Garman

unread,
May 17, 2016, 1:40:04 PM5/17/16
to DroidScript
Yes,
app.GetIPAddress()
only returns an address if you are connected to WiFi.

If that fails, it is possible to get your external IP address by contacting an external service.

However, it is worth asking yourself whether the mobile address would be any use to you.

What do you want to do with it?

Steve Garman

unread,
May 17, 2016, 1:45:41 PM5/17/16
to DroidScript
If you have a use for your mobile address, the following code will get the address of your main internet connection.

If you are connected by WiFi, that will be the external IP of your router.

If you are only connected by phone data, it will be the ip address of your phone connection.

var ip
function OnStart()
{
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
txt = app.CreateText( "Checking" );
lay.AddChild( txt );
app.AddLayout( lay );

getIpAddress();
}

function getIpAddress()
{
app.HttpRequest( "GET",
"http://bot.whatismyipaddress.com",
"/",
"",
HandleReply,
"" );
}

function HandleReply( error, response )
{
if(error) txt.SetText("failed");
else txt.SetText(response);
}

Manuel Lopes

unread,
May 17, 2016, 3:50:06 PM5/17/16
to DroidScript
thanks steve;no only test pourpose;to see my ip but if i need a script for identify user how can handle this?i see not have a imei example;i find email of phone but not the best for id;this is i try by ip but show only 0.0.0.0;thanks

Steve Garman

unread,
May 17, 2016, 4:01:06 PM5/17/16
to DroidScript
To identify a device, I usually use
app.GetDeviceId();

Manuel Lopes

unread,
May 17, 2016, 4:21:24 PM5/17/16
to DroidScript
thanks ;i try read in wiki offline this or example app.GetEnv( ); but the app closes what the use of app.GetEnv( ),show nothing in popup

Steve Garman

unread,
May 17, 2016, 4:35:04 PM5/17/16
to DroidScript
app.GetEnv returns the value of an environment variable.

var s = app.GetEnv( "PATH" );
app.ShowPopup(s);

It is not normally useful in most DroidScript apps.

If you need it, you will probably know that you need it.

Manuel Lopes

unread,
May 17, 2016, 4:39:09 PM5/17/16
to DroidScript
thanks i understand now

Warren Downs

unread,
May 18, 2016, 1:07:24 PM5/18/16
to DroidScript
I extended Steve's code to also get the internal IP address used (WiFi, phone, cabled ethernet or USB).  I believe it will work without requiring a rooted phone, but I need someone to test it for me.

Note that the internal IP address cannot be contacted from outside your network, but if you have an unusual situation (such as a wired phone connection) or just want to see if your phone has an internet connection without connecting to an outside service, this may be useful.

I called the function "getInternalIpAddresses" (plural) because though only WiFi or data (not both) have active IP addresses, there could be some other special case when you get more than one internal IP address-- I'm not sure.

function OnStart()
{
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

txt0 = app.CreateText( "IP Addresses:" , -1, -1, 'Multiline');
txt0.SetTextSize( 32 );
lay.AddChild( txt0 );
txt=app.CreateText("Checking external...");
txt.SetTextSize(32);
lay.AddChild(txt);
app.AddLayout( lay );
getInternalIpAddresses();
getIpAddress();
}

function getInternalIpAddresses()
{
app.SysExec("ip addr").split("\n").forEach(function(curVal) {
   if(curVal.indexOf('scope global') == -1) { return; } // Ignore loopback
   curVal=curVal.split('inet ')[1].split('/')[0]; // Parse IP
   txt0.SetText(txt0.GetText()+'\n'+curVal);
});
}

function getIpAddress() 
    app.HttpRequest( "GET",
        "/",
        "",
        HandleReply,
        "" ); 
}

function HandleReply( error, response )
{
    if(error) txt.SetText("external failed");
    else txt.SetText(response);
}

Manuel Lopes

unread,
May 18, 2016, 4:33:09 PM5/18/16
to DroidScript
why this function?and what the work for this function?

Warren Downs

unread,
May 18, 2016, 4:48:55 PM5/18/16
to DroidScript
1) "Why this function?"

The DroidScript app.GetIPAddress() returns "0.0.0.0" if WiFi is not enabled.  But on some devices, you may have a local IP address without WiFi (for example, you have a USB OTG cable and/or an ethernet cable connection).  In those cases, you may wish to get your IP address even without WiFi enabled.

Or, you may wish to check if there is an Internet connection available and you don't care what the IP address is (just that there is one).  This function will tell you if there is an available connection without relying on an external server to check it.

However, most of the time you will want the IP address returned by the built-in function.

2) "What the work for this function?"  I'm not sure if you're asking the purpose (see above) or how this function works.  If you are asking how it works:

app.SysExec runs the built-in command "ip addr" to list all the available IP address information.  This returns a lot of text, so split("\n") breaks it up into lines.
forEach on an array processes each element and calls the inner function with it (curVal)
If curVal does not contain 'scope global' then it is not an IP address we are interested in.
But if it does, then we isolate the IP address after the 'inet ' text, and return that.

Manuel Lopes

unread,
May 18, 2016, 4:51:42 PM5/18/16
to DroidScript
thanks bro
Reply all
Reply to author
Forward
0 new messages