I am trying to connect multiple devices via Wifi Direct and create a group owner so that the client devices can connect to group owner.
when group owner send or push a message then all connected client devices get this message at the same time.
Is this possible in Android ?
If yes, please provide me some documentation or sample code.
in essence, you would just use the normal Wifi Direct API.
In essence, you would need to advertise local service in order others devices to know which device they should connect to. And the one that advertises should be the Group owner.
in-case you could decide who's the group owner, then use the creategroup function to create a group, then add local service to advertise it, and to make the advertisement visible, you would need to keep peer discovery active, until you get first connection.
Then with Clients, just do discovery and find the device that is advertising the service and make connections to it.
Then in GO, after each connection changed event, get first connection information to see that you are really a GO, and then get group information to see how many clients you have connected to you.Note that new connection can also cause disconnected event to be shown, thus prepare your logic to handle those situations as well.
With clients, once you get connected event, then get connection information so you'll get the IP address of the GO.
GO should have active tread always accepting incoming connections, so your clients can connect it, and your GO would get the IP addresses of the clients connected to it.
Related
I am developing an android application , in which i need to connect multiple devices with each other using WiFi Direct. i am done with multiple connections as i am aware of connecting devices through one group owner device as if all client devices sends a request to one specific device.But i am doing it manually because if all client devices clicks on a name of one specific device then it is OK with multiple connections.But now i need to do it programmatically so that one device automatically becomes group owner and all other devices should automatically send connection request to that specific device.Please help me with this problem.I am stuck at this point from last many days.Thanks in advance for your help.
What is the exact logic/problem there ?
Does it has to be fully automated ? or can the group owner selection be manual ?
In case all devices start the connections same time, then you must really do either manual selection, or have the devices somehow negotiate how they are selecting the group owner. Selection case you be really easy:
Group owner uses createGroup to create the group for connection
Group owner adds local service, and starts Peers Discovery and keeps it running (generally my research has shown that if there is no active connection, or peer discovery, the device local service is invisible for other dervices)
Clients, simply do Peer & Service discovery and do the connections
if there is possibility that the devices appear is same place different times, then of course the logic for fully automatic would be simple.
First all devices advertise & search for devices same time
with The first connection one device is set as Group owner and the other gets to be client
Both devices stop Peer & service discoveries
The Client device stops advertising the service, thus making the Group owner only visible service for any devices appearing into the vicinity, thus they would be connecting to it.
Anyways, I would have small example project which might help you at: Thali/WDConApp, anyways, with WiFi-Direct you would need to manually allow connections, You would try doing the pairing as I explained in my Blog, though I have to admit that since tuesday this week, I'm not so sure if it really works that way. I would need to do some additional tests (when I have time) to verify that I would actually know how it behaves.
Then, if you don't want to have the manual user acceptance of the dialogs, I would have two options for you. You could use unsecured Bluetooth connection as is used in Thali/BtConApp, or you could use WiFi Direct Access points for connectivity.
Note that Using the access point connectivity, will cut off any other WiFi connections. I do have example for it as well, and I actually uploaded it while writing this reply and you can find it from Thali/WAPConApp. Note that its just my first test version, and I'll likely do loads more work on improving it.
I have made a simple multiplayer game , in which a player can select any of the available choices . I want to sync the choices between devices, i.e two devices are connected through Wifi direct but i dont know how to get the choice selected by another player & vice versa
So if the question simply on how would you get the connection done ? or is there something else ?
Basically, you would use the API as it is designed, you add a local service, and then start Service discovery and then once you find the service you start connecting. Then once you get connection event, simply fetch the connection information to see which one got to be group owner and which one got to be client. And as the client now knows the IP address of the Group owner, you can do normal connections between the devices.
If you are ok having BT connections, you could always try using my library, which is included in Thali/BtConApp sample app.
There are several choice for making a WiFi program in android, most common methods are using NSD and WifiP2pManager.
What is different between these 2 choice?
Firstly, these are not two entities to be differentiated. Even if you use Wi-Fi P2p for NSD, you should use WifiP2pManager for connection initiation and negotiation. NSD is to be used for Discovery phase
I assume your question to be difference between Using Wi-Fi P2p Service Discovery(NSD) and Not using it (using normal scan WifiP2pManager.discoverPeers() ).
The answer is explicitly provided here under three sub-topics. However
The difference is actually in the initial phase: The peer discovery phase
When you don't use the Wi-Fi P2p Service discovery, you scan for all
devices that are active with WiFi direct. The scan result list may contain peers that are not of your interest. You can't help it, as you do not have a filter.
In the case where you need to discover only peers that are of your
interest, then Wi-Fi P2p Service discovery should be used. Here, the
filter condition is set in the name of Service.
For ex: your app "XYZ" needs
to form groups ONLY with other devices that also use the same app
"XYZ", then you can create a service and name it, say service_xyz,
and this service info will be broadcast along with the Wi-Fi Direct
device details. On the receiver end, you implement a service listener
that listens for the service "service_xyz". By doing so, only devices
with the desired service name are discovered and listed. Useful for
gaming apps, social networking apps.
However, after this phase, in both the methods, from the discovered list a particular device is selected and connect request is initiated. From here on, the following phases are the same - connection request, negotiation, group formation...
hope this helped you.
i have 2 devices that are in same wifi network and are connected. Now, i want to listen whenever the device gets disconnected or reconnects. I dont want to listen to wifi connectivity with device but the connectivity between 2 devices in same wifi network.
How can we do that ?
Its certainly possible by using Android's peer to peer connection.
This is from the above link.
The WifiP2pManager.ActionListener implemented in this snippet only
notifies you when the initiation succeeds or fails. To listen for
changes in connection state, implement the
WifiP2pManager.ConnectionInfoListener interface. Its
onConnectionInfoAvailable() callback will notify you when the state of
the connection changes. In cases where multiple devices are going to
be connected to a single device (like a game with 3 or more players,
or a chat app), one device will be designated the "group owner".
We can communicate between 2 phones in the same using regular Socket's.
Server Side Link
Client Side Link
If you have a large amount of data to transfer, internet sockets have a greater data capacity and will be faster. The other advantage is that there is no such thing as "out of range". You can connect the two devices wherever internet is available.
So a UDP broadcast would seem like a good option. I.e where 2 devices with same app are running and a packet is broadcasted from one device on a particular socket, where as the other side the app listens on that socket.
I want to understand is it possible to make an app that, after installing and running on 2 different devices, connect with each other in a way, for example, there is a button and a text view in the app and when I click the button on my device, it updates the text view in the other device's app, and same the other way round.
If yes, then please guide me what's it called and where can I read and learn about it.
Thanks in advance :)
Take a look at the section on Connectivity on the Android Developer page. It has a few solutions to this.
In your case, there is a Bluetooth section on communicating between devices. Essentially you have one device act as a server and another as a client.
A description of what is involved as a server from that page.
Connecting as a server
When you want to connect two devices, one must act as a server by
holding an open BluetoothServerSocket. The purpose of the server
socket is to listen for incoming connection requests and when one is
accepted, provide a connected BluetoothSocket. When the
BluetoothSocket is acquired from the BluetoothServerSocket, the
BluetoothServerSocket can (and should) be discarded, unless you want
to accept more connections.
Now you connect to the server phone with your client phone.
Connecting as a client
In order to initiate a connection with a remote device (a device
holding an open server socket), you must first obtain a
BluetoothDevice object that represents the remote device. (Getting a
BluetoothDevice is covered in the above section about Finding
Devices.) You must then use the BluetoothDevice to acquire a
BluetoothSocket and initiate the connection.
Another option on newer phones can make use of WiFi Direct which is built for what you're trying to do as well.