32

I'm using NanoHTTPD to present a web server in an android app. I'm using a wifi lock to keep the network alive, and a CPU lock to keep the cpu awake, and keeping it running via a foreground service so that it should never die.

What i'm finding is that it'll be accessible from other devices for half an hour, and then not accessible for half an hour. Whenever it becomes inaccessible, I can open safari on the android device and browse to 0.0.0.0:30000 and verify that the server is indeed running. And i also check to make sure the wifi is connected which it appears to be.

Any suggestions of where to look?

Chris
  • 39,719
  • 45
  • 189
  • 235
  • 2
    It could be a DHCP issue since several Android versions are affected. (2.1 - 4.1.1 as far as I can remember). Did you try to set a static IP address? – Mibit Dec 09 '15 at 12:57
  • No i didn't set a static IP, however i've found that my router always gives it the same address. Although, next time it plays up i'll definitely check the IP address. – Chris Dec 10 '15 at 01:27
  • It's android 4.4 i believe. – Chris Dec 10 '15 at 01:27
  • It hasn't failed in a while now to be honest, i'm a little puzzled as to what's going on. – Chris Dec 11 '15 at 03:20
  • hi @Chris can you say me what are you exert doing at android side? are you make APK or all time open server from safari browser from android? – Vishal Patel Dec 11 '15 at 06:11
  • 2
    @VishalPatel I wrote an article about it if you're still interested, i've thrown in the towel to be honest: http://www.splinter.com.au/2015/12/20/android-server/ – Chris Dec 21 '15 at 23:06
  • 1
    yeeeh thank you @Chris nice article it may help lots in future. – Vishal Patel Dec 22 '15 at 04:55
  • It could bef a DOZE-related issue actually. – Stan Mar 21 '16 at 10:04
  • @Stan I believe DOZE is android M (6), I was using 4.x – Chris Mar 23 '16 at 22:15
  • @Chris do you have working solution for the Android M(6)? I have rooted tablet and I put server "whitelist" already but server still gets not accessible after unused time. – Alper Özaslan Apr 30 '19 at 09:14
  • @AlperÖzaslan Sorry no. I assume android has aggressive sleeping of background apps. – Chris May 01 '19 at 22:12

2 Answers2

1

I don't know the exact reason why it's stop working. There are ways to keep service running but try this line of code in your activity onCreate if this can help you out

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Qamar
  • 4,959
  • 1
  • 30
  • 49
0

Debugging suggestions

When it becomes inaccessible, can you verify that the network connection is actually working? Not just showing up as "connected" in the UI, but actually working with HTTP connections to remote servers. Does your local HTTP server suddenly become accessible from remote when you do that? While poking, you could watch your server remotely:

watch curl http://{android_ip}:3000/

Do you keep the screen on or does the screen go off? It could be a buggy implementation of the PowerLock.

Does this happen with other wireless routers? It could be an issue with Wifi Power Save Mode (PSM).

szym
  • 5,606
  • 28
  • 34