How to programmatically connect 2 android devices with bluetooth?

19,980

Of course it is possible. I'll make a short tutorial out of the documentation:

Start with the BluetoothAdapter - it is your Bluetooth manager.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

If bluetoothAdapter is null, it means that this Android device does not support Bluetooth (It has no Bluetooth radio. Though I think it's rare to encounter these devices...)

Next, make sure Bluetooth is on:

if (!bluetoothAdapter.isEnabled()) {
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, request_code_for_enabling_bt);
}

If it's not on, we start the activity which asks the user to enable it.

Let's say the user did enable (I guess you should check if he did, do it in your onActivityResult method). We can query for the paired devices:

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

Then loop over them: for(BluetoothDevice device : pairedDevices) and find the one you want to connect to.

Once you have found a device, create a socket to connect it:

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(YOUR_UUID);

YOUR_UUID is a UUID object containing a special ID of your app. Read about it here.

Now, attempt to connect (The device you are trying to connect to must have a socket created with the same UUID on listening mode):

socket.connect();

connect() blocks your thread until a connection is established, or an error occurs - an exception will be thrown in this case. So you should call connect on a separate thread.

And there! You are connected to another device. Now get the input and output streams:

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

and you can begin sending/receiving data. Keep in mind that both actions (sending and receiving) are blocking so you should call these from separate threads.

Read more about this, and find out how to create the server (Here we've created a client) in the Bluetooth documentation.

Share:
19,980
Miky
Author by

Miky

Updated on June 05, 2022

Comments

  • Miky
    Miky almost 2 years

    I am developing an application which should connect 2 Android devices through Bluetooth automatically. Let's say they are already paired. Is it possible to achieve that?

  • Miky
    Miky over 12 years
    thank you, I already have implemented that in my application some time ago I figured it all out, nevertheless your answer is very good and I am marking it as accepted.
  • Miky
    Miky over 9 years
    @jaimin Sorry the automatic Bluetooth connection never worked satisfactory for me. Had to chose WiFi as an alternative.
  • MMJ
    MMJ over 4 years
    I have similar kind of requirement where I need to connect two BT devices over SPP. I was able to connect both the device as describe above. The issue I am facing is, when I disconnect the device using "socket.close()" it does not disconnect both the device and disconnect only one of them. What should I do in this case to disconnect both the device? Thank You