14

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
shiami
  • 7,174
  • 16
  • 53
  • 68
  • Can you clarify your question. What do you want to connect to and how? Do you mean to say you want to connect a device (like a PC) to your phone's wifi hotspot (ie tethering) automatically? – brianestey Nov 28 '11 at 02:52
  • I want to write an app which can communicate between several android devices. (I think it's up to total 6 devices since the wifi hotspots has limit of 5 clients at most.) For example, a local subnet chatroom. – shiami Nov 28 '11 at 05:52
  • So that will be a server and 5 clients in the network. And can communicate like server-client structure. – shiami Nov 28 '11 at 05:53
  • It will not require any internet connection. – shiami Nov 28 '11 at 05:55
  • I currently use NetworkInterface.getNetworkInterfaces() to find the address start with 192.168. in a client. And then connect to the same local network with 192.168.x.1. Use the ServerSocket and Socket to start a connection. Is that correct? – shiami Dec 06 '11 at 04:09
  • 1
    The server itself needs to use the ServerSocket to listen for incoming connections on a particular port and the clients would then connect to the server IP Address on that port. Take a look at NanoHttpd below, as it does exactly that and the code is all there. – brianestey Dec 06 '11 at 05:48
  • Hey did you solved yr problem?? Do ypu have any sample code? If yes than.plz share here. Thanks in advance! –  Sep 09 '16 at 01:56
  • can you post some code here, If you've implemented this? –  Sep 12 '16 at 18:01

5 Answers5

7

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.

Community
  • 1
  • 1
brianestey
  • 8,202
  • 5
  • 33
  • 48
4

Possible answers:

Before ICS and non rooted devices

There is no way you can connect to a device over wifi. There is no API to do. Bluetooth is your best option or user interaction.

Before ICS and rooted devices

I am not sure if someone hacked the code, but when I tried to do an automatic connection to a wifi spot I noticed:

  • The api is hidden inside the SDK.
  • The method that does the connection checks the thread who ask for it. If it's not the os thread, it throws an exception.

Using ICS

With ICS there is a new way to connect devices through wifi called Wi-Fi Direct. Here is a link with some demos.

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • 1
    Maybe I am misunderstanding you, but the [WifiManager](http://developer.android.com/reference/android/net/wifi/WifiManager.html) has been around since API v1 and its listed responsiblities include configuring networks and estalishing connections. If you mean after they are on the same network and they cannot connect to each other once on the same network, take a look at [NanoHttpd](http://elonen.iki.fi/code/nanohttpd/) which uses a ServerSocket for listening for incoming connections. – brianestey Dec 02 '11 at 05:31
0

If you are going to develop your own application for each and every individual terminals(devices) then i think for you socket programming will do the trick .

Yes , android supports socket programming in the same was as java socket programming .

1st google java socket programming tutorials and then you can using the same knowledge in android .

Also do remember to include the uses internet permission in the manifest , actually thats something which ate my brains for a long time :)

Tapan Thaker
  • 362
  • 5
  • 13
0

You mean communication between wifi enabled devices without any server like p2p? if so its wifi direct which is supported in ICS check this out http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html

Before ICS there is no standard android API for wifi direct, though Galaxy S2 has wifi direct it uses proprietary API's which 3rd party devs can't use.

Riyaz Mohammed Ibrahim
  • 9,505
  • 5
  • 26
  • 33
0

i dont know programmically create wify LAN,but you can create wify LAN manually,then you can do java tcp/udp program as said by brianestey

"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 manual connection follow the steps

  • take settings/wireless&networks/portable wi-fi hotspot settings from any one of the phone

  • create new hotspot and turn on wi-fi portable hotspot from there

  • connect all other device to that hotspot including your pc

  • you will get ip of each device programmically (includig pc,but i dont know)

sajith
  • 2,564
  • 8
  • 39
  • 57