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.
Related
To try out the nearbyAPI, I decided to build an app that would allow a teacher to track attendance of student in a class.
Similar to what Caren Chang is doing.
I have gone through the google sample codes on rockpaperscissors and walkietalkie
But only a single device seems to connect at any one time when i test the samples using 4 phones.
I want to build a teacher and student app which the teacher advertises and discovers students, sends payloads to each and every connected device simultaneously as more devices become connected in a classroom set up.
How can i use nearby api to connect and send data to new and multiple devices simultaneously?
That's a great use case, and one we've talked about in the past.
If it's specifically for attendance, then you don't need to form a connection. You can have each device advertise while one device constantly scans. You'll build up a list of devices quickly that way.
If you want to do more than attendance, though, such as pushing an assignment to everyone's device, you'll need to build a mesh. To start with, you'll want to use Strategy.P2P_CLUSTER. We have 3 strategies available inside Nearby Connections (CLUSTER, STAR, POINT_TO_POINT) and cluster is the most general one. With cluster, you can connect to as many devices as you want, and you can receive incoming connections from as many devices as you want. Or, almost... The Bluetooth radio inside phones is weak and can only hold 3~4 connections at a time.
To be able to connect all ~30 devices, I'd recommend forming a 'snake-like' connection. The head and tail of the device will scan and advertise at the same time (and devices that aren't connected to anyone are considered snakes of length 1). The heads and tails will keep connecting to each other (being sure not to connect to itself*), and you'll pretty quickly have a long chain of connections connecting everyone together. From there, you can forward messages down the chain to make sure everyone gets it.
To avoid connecting to yourself, you can either assign every device a random number (eg. 1, 4, 8, 10) and each device tries to connect to the next highest number, or you can broadcast a message when connecting and disconnect if you get an echo back (because the broadcast went in a circle).
I wanted to know if WiFi P2P android API is ideal for use if we take lot of connections into consideration, tha app's users can be at time clients and servers they can share files and send messages and get them also as clients ? if no what is the best solution ( N.B : i'm opened to client/server architecture ).
I just wanted to know the limits of the API cited above and Thank you guys ...
Wifi direct as a technology is ideal for peer to peer communication.However talking about wifi direct for android , it works well only for a single connection that is only 1-1 use case .It is possible to create a one to many where every device connects to the group owner (Soft AP).But even if u build a one to one and try extend to multi use case you will have problems with certain devices that do no connect perfectly.
Checkout some of the problems listed under problems after downloading this application https://play.google.com/store/apps/details?id=com.budius.WiFiShoot&hl=en
If you want to go for multiple connection it is better to use hotspot .One device ends up being the hotspot and other clients connect to the hotspot .It is being used by plenty of application like xender, zapiya.
I have made an app on iOS and Android that can connect to a BLE device. I connect to the "device by service" and things work well.
A customer is asking me why the device is not showing in the list of bonded bluetooth devices. I didn't need pairing to connect because I found code that didn't require it, but now that I think, I wonder :
What would be the benefits to have a paired device ?
Would it connect faster ? Hold connection better ? ...
Security, mainly.
Sending data to a non-paired device requires that the device be discoverable and open to receiving data from random devices. This isn't always the case for reasons of security, and so many devices ship with both disabled.
The attack vector is something like this: you'll always have to pick a device in the list of discovered devices. It's not too difficult to create a device with "<CEO>'s iPhone" as the name and wait until he tries to send the secret memorandum to his iPhone, then intercept it.
Additionally, bluetooth stacks have been known to have vulnerabilities that can be exploited by sending data to the device, which was made more problematic by devices auto-accepting data without confirmation.
Paired devices create a pre-existing relationship between the two, allowing your user to be notified when something out of the ordinary happens. This is always a good thing.
It is indeed also true that not having to go through device discovery (which may take up to a few seconds) improves performance when doing the initial connection setup, but I wouldn't see that as the major reason.
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.
I have two Android devices. One is acting as a server and the other as a client. The client connects to the server and requests a file - this is done in one thread on the client and one thread on the server so that both can continue doing what they want.
The client then attempts to connect to the server again to request another file. Right now I am getting a java.io.IOException: Device or resource busy when attempting to connect (socket.connect()). Is it because Bluetooth (on Android) only allows one channel between two devices? (if it were another device it would work but if it is the same it doesn't ?) Note that both attempts are made with the same service name and UUID.
Even if the error is specific to my code, I would like to know if this is the case or not.
System: android 2.2.1 communicating with the bluecove bluetooth library.
Definitely not with the same UUID(universally UNIQUE Identifier).
Reference for that was taken from here
Maybe with more than one. You can connect multiple devices in the Server/Client style, you can try to set one of the devices as a server and start several clients on the other. My first guess would be to start several client threads, but you might have to find a way to change the MAC address for each of them.
Here you can find another discussion about how to change your mac address, but only works in rooted devices. I can't find anything else for non rooted ones. No idea on how to do this programmatically but it might give you a start.
Here there is a discussion about connecting several clients at the same time in a server. I got there from this question. ( I think this might be your closest shot)
Here you have a discussion about peer-to-peer networks.
AFAIK, multiple connectivity is not possible in case of Bluetooth Connection. Bluetooth is Connectivity API is by default Synchronized so only one connection at a time is possible. So you can not perform multiple connections.
However it can be possible in another way like making one connection , performing 2 seconds operation on it and then creating another connection and performing 2 seconds operations like in normal multitasking operating system happens.