Miko Chu
Miko Chu

Reputation: 1382

How to send data (String, int, etc) to the same application installed on another android device using bluetooth?

I'm just so confused... I mean let's say that after the user chooses the device he wants to be paired then how does my app finds the same app and send String data??? Then what's the API for receiving the event once data are sent???

A simple sample code would totally help me! (Please don't reference Google's Bluetooth Chat Example, they used design patterns which I'm not comfortable with yet).

Upvotes: 1

Views: 209

Answers (1)

Mackovich
Mackovich

Reputation: 3593

Alright I will try to explain things as simple as possible.

Let's assume you are using an RFCOMM channel for communication between your two Android devices.

One device will act as a server, the other as a client.


Server-side :

  • First you need to register your application to the Bluetooth Stack with the following line :

public BluetoothServerSocket listenUsingRfcommWithServiceRecord (String name, UUID uuid)

where the name is your app service name (choose any) and the uuid is a java.util.UUID that you choose too (here you have an online generator). Do not forget that UUID.

  • Second, you will have to start the Bluetooth server with the following line :

public BluetoothSocket accept (); //possibility to add a timeout parameter

Note: this is a blocking call meaning the thread that is running this code will block and wait until the method accept() returns. It will only return when your client application connects to your server application. Use the timeout parameter if you do want to block forever (or for as long as your app is running). Since it's a blocking call, do not call it on your UI Thread it will cause an ANR (Application Not Responding). Instead use a Thread.


Client-side :

There is a two-case scenario : either your Android devices are paired, either they are not. The simplest way would be to manually pair your two devices via the Settings but unless you want perfect user experience for your app, use the discovery & binding operation.

Now onto the connexion to the server :

  • First parse your list of bonded devices (retrieved with getBondedDevices()) and find the device you want to connect to. In the case where you performed a discovery & binding operation, keep track of the device the user chose to pair with. That will be the device you want to connect to.
  • Second, establish the connexion with the following lines :

public BluetoothSocket createRfcommSocketToServiceRecord (UUID uuid);

public void connect (); //from the returned BluetoothSocket

Where the UUID is the one you use in your server application. Note: again, connect() is a blocking call, though it will not block forever because there is an internal timeout parameter. Call it from another Thread.

  • Third, once the connexion is established between your two devices and more precisely between your two applications, you can retrieve the outputStream and inputStream from the BluetoothSocket (both server and client gets a socket).
  • Finally, you may now use these streams to send and receive data from/to your client app and from/to your server. You may send Strings, ints or whatever you want as long you properly convert them to bytes and convert back to their original values.

A guide to understand Stream and Bytes: http://fr.slideshare.net/shahjahan786/understanding-java-streams.


That's it. You have all the basic elements to work on your app and establish a Bluetooth connexion between two devices, wih your app, so that you can exchange data.

Upvotes: 1

Related Questions