androidandroid-wifilocal-network

How to start a local network connection programmatically via Wi-Fi hotspot on Android?


I want to start a local network connection using the built-in Wi-Fi hotspot on Android devices. Is there a way to accomplish it? And how to communicate between two devices?

EDIT: I want to do it programmatically. Then I can transfer my own data between devices.


Solution

  • I haven't done this before, so my answer is all theoretical.

    I think what you need to do is declare one device the server and start its hotspot manually with a known name.

    Start / stop built-in Wi-Fi / USB tethering from code? (According to this question, you can't start hotspot programatically).

    Once you have the hotspot set up, you start up the server app and wait for incoming connections. The server app can show you the ip address or hostname.

    On the clients, you can do a simple check and see if the hotspot name is around and automatically connect to it if you find it. See this link for connecting to a network:

    Using WifiManager to connect to a network

    As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server.

    For reference, I found NanoHttpd, which is a webserver for Android. It uses ServerSocket to listen for incoming connections.