Apps that interact with each other through Bluetooth on different android devises? - android

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.

Related

WiFi Data Sharing with socket in android

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.

How to find a Group owner device automatically

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.

Connect multiple devices to a specific bluetooth server

I want to connect multiple devices to a single bluetooth server. My application currently makes the server discoverable, and then, the client application lists the discovered devices and tries to connect to one of them. The problem is, I don't know which of the discovered devices is the server.
The way I am dealing with the problem envolves a try-catch mechanism, where I try to connect to each device and, upon connection, there is a brief message swapping between client and server, where the client asks if the connected device is a server for my application.
The main problem here is that the socket connection and message swapping takes too long, if there are multiple discoverable devices nearby I am forced to probe each one of them asking "are you a server for this app?". Is there any alternative to this system?
Note: There is a little "catch" in this application, there may be multiple servers, so I really need a fast way to go around my device list checking which ones I can connect to.

Can you establish multiple Bluetooth connections between the same two devices in Android?

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.

Android: Access to an already paired connection

I have a question on accessing already existing paired bluetooth connections...
How can a remote service detect an already paired connection and await a file transfer from it?
In all comments on stackoverflow, on tutorials, ebooks or on google there is always shown how to create a new connection using sockets and so on, but no word about accessing to an existing one.
Is it possible at all? If yes, can you also tell/show me how?
At the moment I have no clue how to get started with it.
Would be great if someone can help me get started :)
Pairing just means that the two devices have authenticated eachother. It does not imply an open socket connection.
After pairing, comes connecting: One side creates a server socket and the other device can then connect to it with a client socket.
I think the confusion is coming from the blending of two concepts: "already paired" with "already connected". Pairing just means they are authenticated. The devices cannot connect without first pairing, but pairing does not connect the devices.
To make an app wait for a file transfer it would go something like this:
Make device A pairable.
From device B, search for nearby devices and when you find A, pair with it (this is where you enter a secret PIN code that has to match on the two devices)
Now that the devices are paired, start your file server on device A or B (It then creates a server socket and awaits incoming connections)
Then initiate a connection to the file server from the other device.
With the sockets open, data can then flow.

Categories

Resources