Home » Android » service – Android http server not accessible from other devices on network intermittently

service – Android http server not accessible from other devices on network intermittently

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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);

Answer:

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).