Create group using wifi p2p to connect multiple device - android

I want to create a connection where one device will be like server which will stream some data to connected and multiple device can connect to that device which
receive data.
So i have done some kind of research i got to know that we can do this using network service discovery and WiFi peer to peer, So i have made the connection where i can connect one device and stream data also but i want to create a group to connect multiple device ,So any help from any one would be appreciated.
Thanks!

WiFi-Direct or Wi-Fi Peer to Peer can do multiple device connection.
See this link to read more about P2P connections
-https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html
WiFi-Direct automatically select which device will be the Group Owner or the server base on which device is the most compatible to be the Group Owner or the server. But you can manually select which will be the Group Owner by setting WifiP2pConfig groupOwnerIntent = 15(the highest value).
If two devices are connected, when you connect another device this device will connect to the group owner(or server).
For you to able to send streams to client devices you can use java Sockets. When peer to peer connection is established, the group owner will be the one to handle the server socket and client socket for the client devices.
If you have multiple devices and you want to send a message to a specific client device you'll send it to the server and the server will send it to the specific client. see this link - WiFi Direct (Android 4.0) with multiple (3+) devices

Related

Android bluetooth connections issues

I have confusion on bluetooth connections.
Preface: when connecting devices for android over bluetooth you can connect as server or as client
My understand: connecting as the server essentially means that your device will be hosting the connection. Connecting as client means that you are being hosted by another device - and/or the device you are connecting to is the host.
I am trying to build an app that can connect via bluetooth and control a device. My first use case is my TV. so I want to make my app that can connect to my TV and control it as a remote. My initial thought is that the TV would act as the server. If I am connecting to another phone then I would imagine that my device would need to be the server. In most cases, if I want my app to act as a controller to send control signals to the device it connects to - is it correct to assume that my device is client or server?
Assuming that you mean Bluetooth Low Energy (BLE), then there are two separate types of roles that dictate how communication happens in Android:-
Central vs Peripheral - This has to do with the connection establishment and maintenance as follows:-
Peripheral device is the one responsible for advertising its presence as well as accepting incoming connections. Sensors usually fall into this category.
Central device is the one responsible for scanning and establishing the connection with the remote peripheral device. Phones usually fall into this category.
GATT client vs GATT server - This dictates how communication is handled when that connection is made:-
GATT server is the device that hosts all the data. Again, sensors usually fall into this category.
GATT client is the device that reads the data from the GATT server. Again, phones and computers usually take the GATT client role.
The two types of roles are separate (i.e. a Central can be a GATT server or a GATT client, and vice versa), but in the majority of the cases, the central is the GATT client.
And to finally answer your question, I would make the remote controller to be both the central and the GATT client. This way your TV would always be advertising its presence and hosting the date, while the remote scans/connects and reads/writes to that data.
You can find more information in the links below:-
The ultimate guide to Android BLE development
Bluetooth Low Energy: A primer
Android BLE examples
Client because when is server tv, you can connect from more clients (mobiles).
When is server mobile you can controll more tvs, but tv can connect only to defined mobiles.

Android P2P direct connection for multiple devices. Peer as GroupOwner and client at the same time

I want to create a chat application (without internet connection) for Android-based on P2P communication via WI-FI (or Bluetooth) between 3 or more devices. I would like every device to have a direct connection to all devices. I know that android API provides the WifiP2pGroup class, but in this case, one device is a groupOwner and the other are clients and the other ones do not have a direct connection with each other.
Is it possible for each device to be a groupOwner and client for other devices at the same time (like on the image)? Is there another way to solve my problem?

If I connect 2 android devices via WiFi-Direct - can one share its Internet connection with the other?

I am building an SDK for a client to connect 2 android devices for exchanging data (strings, commands...) via various channels, such as Bluetooth, USB cable, WiFi.
One of the devices is a standard android phone, with a SIM card and data, thus able to access the internet. The other has no SIM card.
Currently using WiFi I am opening a HotSpot on the phone device, and so the non-SIM device can access the internet via the HotSpot.
Now my client wants me to connect via WiFi-Direct, too.
So my question is - once I connect the two devices via WiFi-Direct, will I be able to access the internet on the non-SIM device, using the SIM/data on the "normal" phone?
Thx
Well, Wi-Fi direct can create hotspot independent of the legacy Wi-Fi hotspot BUT this hotspot doesn't share internet and any request to external ip will be dropped.
But for android you can use NetShare app to do that, you can download it from
here.
it works as follows:
• in the client side NetShare use the vpn service to catch all internet traffic of the device and send them to NetShare in the server device and wait for the response.
• in the server side NetShare run a server with specific port to receive the internet packets sent from the client side in
previous step, it send these packets to the internet, after receiving the reponse from the internet it will send these responses to the NetShare in the client side which in turn provide it to the back to the client device.
for more details see the official website

Server on Android reachable from devices not on local network?

I have been trying to set up an app that allows communication between two devices, a server and a client. I have established the connection and can send messages between the two as long as they are connected to the same source of internet. When I connect the devices to two different sources, I am not able to establish a connection between the two. It works by entering the IP address of the server you want to connect to.
For example, if my server device is connected to my home Wi-Fi and my client device is connected to the same Wi-Fi router, communication works as supposed to. But, when I connect the server to Wi-Fi and my client to cellular data (or different Wi-Fi router), the client is not able to find the server.
From all the searching I've done, I found that it is because my server is bound to my local host. So my question is, How can I make my server reachable from anywhere regardless of what network you are connected to?
I used this tutorial to get my server running:
http://android-er.blogspot.com/2014/02/android-sercerclient-example-server.html
The very first comment is a question that asks why it only works on a local network and someone answered saying you have to set up port forwarding on your router. From what I understand, this will work only if the server device is always connected to the same router. This will not be the case for my app. The server will have a new IP address as the device changes networks. I want this IP address to always be reachable no matter what network the server is connected to.
You will need to create a relay server. This would be set up using a java application on the PC and it would manage virtual "rooms" that then relay the messages to the clients. The computer you run your relay server off of will need port forwarding but the clients will not. Much of the code from that tutorial could be applied to this concept.
See: How to create a java Server that accepts client connections and then build a relay connection for a client pair
If your server is behind a firewall, you will not be able to reach it unless that port is open on the firewall. The only way to avoid this is to have a non-firewalled server.

connecting two or more devices via wifi with through my app

I want to have my app able to connect to other devices over the same wifi network and transfer some data, that is phone A and phone B have my app installed and are on the same network. I am able to connect two devices currently with one acting as server and the other being client but this is with a static IP reference to the server device. In the open world this IP will change of course so my problem is how do I get phone A to start up as a server device then have phone B see the server device on the network and connect to it. So the server device would accept this client connection and carry out the needed actions when the connection is made. Is this even possible?
I believe you would have A device listening on port number x, and the client version (device B) looking for that same port number x. This could be wrong.
This is a job for DNS. If you don't have a DNS server available you could invent a discovery protocol in which machine A periodically multicasts the IP & port on which it is listening.
Machine B joins the multicast group. Once machine B knows A's address and port it can open a socket using the code you already have.

Categories

Resources