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.
Related
I am trying to make an app, that when it sees a specific bluetooth device to connect to it and send a command and before loose that connection to send another command.
The device is standard bluetooth serial device.
Is there a way to check when i am going to loose the connection?
No unfortunately Bluetooth doesn't work this way. You are usually notified that the remote device disconnected and you can even get the disconnection reason (e.g. BT_HCI_REMOTE_USER_TERMINATED_CONNECTION), but by then it is already too late and the link between your device and the remote device is already lost. Generally speaking, the way a disconnection works is that there are empty Bluetooth packets sent back and forth between the two devices (similar to an ACK) to indicate that the link is alive. If that packet does not arrive after a certain timeout, the BLE stack throws an event to the application notifying it that the connection has been lost (i.e. a disconnection event).
If you are using Bluetooth Low Energy, and if you are in control of both devices (your one and the remote one), then you could implement additional communication on the advertising channels. This is not as efficient as performing the communication through a connection, but you can advertise this additional command upon disconnection, and the remote device would scan for this new command upon disconnection as well.
I hope this helps.
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.
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 anyone in simple words explain me the need of UUID in android bluetooth example. I have read some articles about it but it is still not getting clear the exact need of UUID.
And now let me explain you the scenario of what I want to develop:
I want to develop an android application to transfer data for example a "FILE with .xyz extension"
from my phone to the other phone over bluetooth. IT IS NOT AT ALL NECESSARY THAT THE RECEIVING PHONE SHOULD ALSO HAVE THE APPLICATION THAT I AM USING. I just want to transfer data from my application to other phone and thats it. I don't care what the receiver does with the data.
I just want to connect to a device within range and transfer that file using my application
Now how should I do this? Where does the the role of UUID come here? I have read that UUID is for my application , and both the server and the receiver should be aware of this UUID to form a connection . But what if the receiver does not have the my application? It will surely not know my Applications UUID ? then how the data transfer will be possible? I simply want to use Bluetooth without concerning a specific application.
In here, what my application should be doing? Should it be creating a server socket / a client socket or what? and Why.
An explanation in simple words is appreciated(some articles if possible). I dont want regular answers having BluetoothChat suggestions. If you don't understand the question please let me know, I will try to be more specific and elaborate it for you.
The basic goal of this question is to clarify the use of UUID and transfer data between two DEVICES (and not an application) using bluetooth from an application running on one android phone.
Even with bluetooth you can create a client-server application.. there is a BluetoothSocket
read here http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html
Now, lets say you have two devices:
device A
device B
and assume that device A sending data to device B, You didn't say if device B is also sending data to device A, so I'll just describe the first scenario when A send to B.
So in that case, since all the data is stored in device A and you want to send it to device B, it will be more reasonable to create device A as a BluetoothServer and device B as BluetoothClient which listening to the server.
But.. If you want both of devices will exchange data.. you can make one of them as a server
and for each one of them create 2 threads:
Thread that sends data
thread that listening to data
so both of them can exchange data..
Another thing.. if you ever programmed a regular client server you noticed the accept() method which is blocking until there is a client which connected to server.. the same is with Bluetooth client-server application.
Summarize:
One device will act as a server - so you'll need to write a server project and install
it on the first device
Second device will act as a client - so you'll need to write a client project and
install it on the second device
Don't forget to add the bluetooth permission in the manifest file for both
projects.
Both of the projects need the same UUID as you mentioned in your question.
in simple words both of the sides need the UUID so they each know with who they're
communicate
I think it's more like a port in a regular client-server..
I read somewhere that is used for RFC communication.. as you probably know there are
some protocols for Bluetooth like RFC,SDP and etc..
EDIT:
In most of the phones there is a pairing process when you want to send data through
bluethooth. so if you don't want to use the client-server version, I think you can
do this:
Your application will search for devices to connect to. (pairing process)
After pairing you are connected to the other device and just send the data
EDIT 2:
You want to send data from A to B right?
I'll explain more clearly..
You're right when you said the client should know who is the server and need insert the port and
the IP of the server this is correct and works in this way.
Now, look..
The server listen to connection from clients, when a connection established the communication
begins.
Client ask for data
The server processing the client request and send him the related data
So any data like: Files, Databases should be stored on the server side..
Now in your case, the files you want to send are located in device A and not in device B,
So if device A is the server he will listen for connections.. when device B connects to the server
(device A) the communication begins.. Device B can request for files from Device A..
Also, since device A is the server, he can even broadcast a message.. means send the same message
for all clients which are connected to him.
But what you're want to do is to send file even if device b didn't ask for it, right?
I don't understand if you want that device B will also send file to device A, so lets divide it
into scenarios:
just device A send to B:
In this case, since the files are located in device A, means device A have the data,
device A is the server and device B is the client.
So when connection established you can send from A to B.
Both devices exchange data:
In this case, both devices should listen to each other, but just one of the should act
as a server and the other as a client. means that you need to install the serverApp on
one of them and the clientApp on the other.
But each of them can send and listen to other. so for each of the you need to create
thread that handle with the sending data and another thread that handles the receiving data
UUID is the Universal Unique Identifier. When you want to connect to any of the service that bluetooth is exposing then you should have the UUID to tell to the bluetooth software module that it has to inititae connection to this particular service. In your case in order to send file from DevA to DevB it has to use File Transfer Profile and there is specific uuid which is associated with this and this is defined by Bluetooth SIG which is the authority which qualifies bluetooth products and works on the technology. This is known by all the devices which uses bluetooth.
Tu cut the story in short when DevB receives the incoming connection request with the uuid which is unique it comes to know to which particular service of DevB the device is trying to connect to and it connects to that service. Hence if you want to send file from DevA to DevB then you need not have the same application at DevB. But you need to make sure that you use the UUID which is specified for File Transfer profile by Bluetooth SIG.
Regards,
Shripathi
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.