Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically connect 2 android devices with bluetooth?

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?

like image 600
Miky Avatar asked Sep 01 '11 16:09

Miky


People also ask

Can you connect 2 Bluetooth devices to Android?

Most Android devices can connect to two or five Bluetooth devices simultaneously, while others support up to seven devices. The number of supported connections depends on the Bluetooth module your device is equipped with. Obviously, the newer the hardware, the more Bluetooth devices you can connect to.

How do I automate Bluetooth pairing?

In the logic flow, first we will open the Bluetooth settings page on Android device, then apply KEYCODE_DPAD_UP event for a soft press at turn on/turn off area, next apply KEYCODE_DPAD_CENTER to do hard press to change the status to on/off.


1 Answers

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.

like image 151
Jong Avatar answered Oct 18 '22 19:10

Jong