6

I want to setup an android phone as a wifi-hotspot, then run a webserver on it, and browse it from another phone connected to the hotspot.

My phone isn't rooted, but I have termux, which has many unix utilities. I don't want to install an extra app. (But happy to write one!)

I can view python's simple webserver on http://localhost (on the host), but not on the other phone. I tried using the public-facing IP of the host phone (checking it using whatsmyip-type webpages), but didn't work. Someone said that mobile ISPs prevent this, by mapping different internal and external IPs... but here, it's not going through the ISP, just the hotspot...

I also tried IP addresses from ifconfig and from the wifi controls within android, which does work for netcat - but only the IP of the client phone connected to the hotspot (not the host).

That is, netcat is on the phone connected to the hotspot and listens, then netcat on hotspot phone connects to it. (i.e.role swap: hotspot client is netcat server). Weirdly, the hotspot phone doesn't seem to have an IP (at least, not one I've been able to discover, so far). But it must have one, mustn't it, for netcat to connect...?

Anyway, I want the webserver on the hotspot host, so I need its IP to connect to it... is there a way to get it?

This has been bugging me for ages. Many thanks for any help!

EDIT the answers to this question says it's (almost) always 192.168.43.1. I can't try it right now; will update when I have.

UPDATE
1. 192.168.43.1 works
2. py http.server works fine for regular files (e.g. txt, pdf), but video files seem to require some streaming protocol it lacks. I found lighttpd had this (available within termux using apt install lighttpd). But it needs config (and no eg/default). I used:

$ cat > lighttp.conf
dir-listing.activate = "enable"
server.port = 8000
server.document-root = "MY PATH HERE"
$ lighttpd -D -f lighttpd.conf

The dir listing makes it much easier to use, but obviously no security configured here, so want to be careful what you make available.
3. Android (my 5.1, anyway) needs airplane mode off before you can make it a wifi hotspot - which makes sense for internet access... but here, I want only the client to have access to the host, not have it accessible to the whole internet. So I found you can turn off data-access to prevent that, and the hotspot still works. (There's surely a way to have a hotspot in airplane mode programmatically...)

There you have it! Phone as media server.

hyperpallium
  • 571
  • 5
  • 18
  • Yes its mostly 192.168.43.1 And you can easily get it on the device itself. Like you can also easily get the ip on the device itself when it is not a hotspot. – greenapps Dec 01 '17 at 17:34
  • @greenapps how do you easily get it? – hyperpallium Dec 02 '17 at 00:27
  • The serversocket has a member for that. Further you can google for this obvious task. – greenapps Dec 02 '17 at 09:37
  • Followup question.. (sorry, I know it has been a few months) Do we know if `192.168.43.1` will always be the same? Does this work for all android phones? What about for iOS? – luckyging3r Apr 05 '18 at 21:04
  • @luckyging3r Sorry all I know is it has worked on both androids I tried it on. You could ask a new specific question. Because it's specitic, you might get better answers. – hyperpallium Apr 05 '18 at 23:34
  • @greenapps the addresses obtained from methods on serversocket do not give the ip address of the device - recall that in this use-case there is no connection to start with (we can't form one, because we need the ip addresses to form one). If you really do know how to answer this question, please answer it. – hyperpallium Apr 06 '18 at 00:37
  • `If you really do know how to answer this question, please answer it.`. Well what is the question now exactly? Unclear. – greenapps Apr 06 '18 at 09:28
  • @greenapps I was mainly unhappy with your above comments about how you could "easily" get the ip... when I questioned this, you blew it off it as an "obvious" task. And now it turns out you aren't using this "easy" "obvious" way... Many people tackled this problem online without a neat answer, making it clear you were off-base, so I should have just dismissed your overconfidence as another random person on the internet, but for some reason it really bugged me... Anyway, I tell you all this to suggest you at least try to pretend a little humility and respect for others. – hyperpallium Apr 07 '18 at 01:49
  • You are unrealistic. The code i finally posted can be found a hundred times on stackoverflow. Very strange you did not manage to find it. – greenapps Apr 07 '18 at 08:39
  • That is strange. I did find it day before yesterday, but in Oracle docs [link in my comment on your answer]. The SO answers I did find then [link in the question itself] didn't mention it. Possibly, using slightly wrong search terms (or, maybe things changed in the last few months!?) I note that your idea at that time (serversocket) seemed to be wrong, also. – hyperpallium Apr 08 '18 at 04:00
  • @hyperpallium Thanks for your research on this. I have a qq on enabling hotspot without mobile data. Is it possible to enable hotspot without mobile data( I mean offline)? I have a use case where I want neighboring devices to connect to my Andriod app over hotspot and post the data to my server in the app. I don't want mobile data being enabled in my mobile. Thanks for your answer. – ranganath111 Mar 18 '19 at 23:53
  • @ranganath111 yeah, on all my android devices you can have wifi on without mobile data - but I don't know if this is universally true. BTW I could similarly enable a bluetooth hotspot (Settings->more->tethering & portable hotspot). I think I posted an answer on SO detailing all this, as a way to make a second phone be a second screen - have a search, if you can't find it, let me know. I haven't looked at this stuff for a year, so aren't across the details. – hyperpallium Mar 20 '19 at 01:39
  • Thanks for the reply. Yes, I tested on some phones that I got hold of. I am able to create a local network between my phone and two of my PCs over hotspot. Now, I want to know if I can do data transfer between these devices on hotspot port(192.168.43.1 I guess). Is there any example that I can use for data transfer over hotspot without internet? – ranganath111 Mar 20 '19 at 16:59
  • @ranganath111 It was on another stackexchange - see answer here https://unix.stackexchange.com/questions/443879/how-to-control-an-interactive-shell-e-g-vim-from-another-machine/444312#444312 (it covers other things irrelevant to you) It's for bluetooth, but I'd done the same thing with wifi, and IIRC it's similar. – hyperpallium Mar 21 '19 at 01:11
  • @ranganath111 Another https://android.stackexchange.com/questions/197457/doubled-headed-displays-by-casting-half-to-another-device It should be pretty straightforward, if you're still having trouble, there may be more to your problem, or you might misunderstand something etc. The best thing is to ask a question - and (very important!) include the steps you've tried. – hyperpallium Mar 21 '19 at 09:44
  • @hyperpallium Thanks for the pointers. I found enough relevant documentation that it's possible so I am going to believe that running a server on a hotspot device and doing data transfer is possible. The bigger question I have is about the hotspot ip address. Unfortunately, it's different from mobile to mobile, and I can't hardcode the IP address in my neighboring devices. – ranganath111 Mar 26 '19 at 23:22
  • The question would be, can I start a service on 192.168.43.1:8080 all the times so that my neighboring devices can have a hardcoded IP address to connect to my mobile phone. If it cannot be configurable, then it's difficult in my use case. – ranganath111 Mar 26 '19 at 23:24
  • > "and (very important!) include the steps you've tried". It's too hard to try to answer this in comments - please ask as a question. That's the idea of stackoverflow, isn't it? I see you did ask one, but if you didn't try anything, why should anyone try to do it for you? Also, maybe asking on an android specific site would be better - look at the sites of the answers I linked for you. – hyperpallium Mar 27 '19 at 08:09

1 Answers1

3
public static String getDeviceIpAddress( ) {
String deviceIpAddress = "###.###.###.###";

try {
    for (Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); enumeration.hasMoreElements();) {
        NetworkInterface networkInterface = enumeration.nextElement();

        for (Enumeration<InetAddress> enumerationIpAddr = networkInterface.getInetAddresses(); enumerationIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumerationIpAddr.nextElement();

           if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) 
            {
                deviceIpAddress = inetAddress.getHostAddress();

                Log.e(TAG, "deviceIpAddress: " + deviceIpAddress);
            }
        }
    }
} catch (SocketException e) {
    Log.e(TAG, "SocketException:" + e.getMessage());
}

return deviceIpAddress;
}

This will help you i think.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Thanks for posting an answer. I tried this approach yesterday (https://docs.oracle.com/javase/tutorial/networking/nifs/listing.html), but it doesn't give the hotspot ip. (I didn't check it, but I think the ip it gives is the public-facing ip). Anyway, I'd got my problem fixed, I was just looking into it again because @luckyging3r asked about it. – hyperpallium Apr 07 '18 at 01:52
  • Well, I must have messed up my tests yesterday (perhaps the hotspot wasn't properly on), but today it gives the correct ip (192.168.43.1) is amongst those listed. And your non-loopback and len==4 identifies it. BTW when connected to wifi (i.e. not being a hotspot) it gives another ip which differs from the public ip (that you see from whatsmyip websites). – hyperpallium Apr 07 '18 at 02:16
  • That other ip (when connected to wifi) works the same as 127.0.0.1, i.e. on the device, I can browse a webserver running on the device. I can also browse it from a different device connected to the same wifi. Unfortunately, I only have wifi at the moment, so can't do a public test. Anyway, that's a different question altogether - thanks for your help! – hyperpallium Apr 07 '18 at 02:31
  • `if gives another ip which differs from the public ip (that you see from whatsmyip websites).` Of course. It gives the ip the device got from the router. A local ip. Every device in your lan has a different ip. But they look all the same for the last block of digits. Whatsmyip gives the public ip of the router. Every device in your lan candetermine it. – greenapps Apr 07 '18 at 08:35
  • `Whatsmyip gives the public ip of the router. Every device in your lan candetermine it.` How can we determine this in code? (Also, BTW, how are you formatting your reply-quotes in the comments? nvm, I see how it's backticks code formatting) – hyperpallium Apr 08 '18 at 04:06
  • You request that http page using a http component and then parse the textual ip address out. – greenapps Apr 08 '18 at 08:35
  • That would work, but it seems there must be a more direct way! But I guess if the router is completely separate, there wouldn't be (not counting some out of band API, specific to that router) – hyperpallium Apr 08 '18 at 12:02