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

北城余情 提交于 2019-11-30 06:52:25
brianestey

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.

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.

This will help to initiate. link1

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

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.

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!