Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically connect to paired Bluetooth device

Is there a way, using the Android SDK, to programmatically connect to an already-paired Bluetooth device?

In other words: I can go into Settings -> Wireless & networks -> Bluetooth settings, and tap the device (listed as "Paired but not connected"), at which point it will connect. I'd like to be able to do this programmatically, but don't see a way to do this.

I see the options to create an RFCOMM socket, and for a SPP device, I'm assuming that'll do the connection part as well, but for an A2DP device, where the actual data transfer will be handled by the OS rather than by my app, I think that's not applicable?

like image 790
rmc47 Avatar asked Mar 02 '11 17:03

rmc47


People also ask

How do I connect to a Bluetooth speaker that is already connected?

Go to settings, Bluetooth, and find your speaker (There should be a list of Bluetooth devices that you last connected to). Tap on the Bluetooth speaker to connect, then turn the speaker on AFTER you pressed the connect button, while your device is trying to connect to it.

Can I connect to someone's Bluetooth without them knowing?

Theoretically, anyone can connect to your Bluetooth and gain unauthorized access to your device if the visibility of your Bluetooth device is on. However, this is an unlikely scenario as modern Bluetooth devices require some kind of pairing sequence before successful connection.


2 Answers

Okay, since this was driving me crazy, I did some digging into the source code and I've found a 100% reliable (at least on my Nexus 4, Android 4.3) solution to connect to a paired A2DP device (such as a headset or Bluetooth audio device). I've published a fully working sample project (easily built with Android Studio) that you can find here on Github.

Essentially, what you need to do is:

  • Get an instance of the BluetoothAdapter
  • Using this instance, get a profile proxy for A2DP:

adapter.getProfileProxy (context, listener, BluetoothProfile.A2DP);

where listener is a ServiceListener that will receive a BluetoothProfile in its onServiceConnected() callback (which can be cast to a BluetoothA2dp instance)

  • Use reflection to acquire the connect(BluetoothDevice) method on the proxy:

Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);

  • Find your BluetoothDevice:

String deviceName = "My_Device_Name";  BluetoothDevice result = null;  Set<BluetoothDevice> devices = adapter.getBondedDevices(); if (devices != null) {     for (BluetoothDevice device : devices) {         if (deviceName.equals(device.getName())) {             result = device;             break;         }     } } 

  • And invoke the connect() method:

connect.invoke(proxy, result);

Which, at least for me, caused an immediate connection of the device.

like image 136
Kevin Coppock Avatar answered Sep 20 '22 17:09

Kevin Coppock


the best way I found to solve my problem was finding out that I can create a button that brings up the Bluetooth Settings screen. I didn't realize you could do this, or I would have from the beginning.

startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); 
like image 32
Belboz Avatar answered Sep 20 '22 17:09

Belboz