Connect to Bluetooth programmatically
This is how i made this work using Java Reflection and BluetoothProfile:
Attributes:
private boolean mIsConnect = true;
private BluetoothDevice mDevice;
private BluetoothA2dp mBluetoothA2DP;
private BluetoothHeadset mBluetoothHeadset;
private BluetoothHealth mBluetoothHealth;
Call:
mBluetoothAdapter.getProfileProxy(getApplicationContext() , mProfileListener, BluetoothProfile.A2DP);
mBluetoothAdapter.getProfileProxy(getApplicationContext() , mProfileListener, BluetoothProfile.HEADSET);
mBluetoothAdapter.getProfileProxy(getApplicationContext() , mProfileListener, BluetoothProfile.HEALTH);
Listener:
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
mBluetoothA2DP = (BluetoothA2dp) proxy;
try {
if (mIsConnect) {
Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
connect.setAccessible(true);
connect.invoke(mBluetoothA2DP, mDevice);
} else {
Method disconnect = BluetoothA2dp.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
disconnect.setAccessible(true);
disconnect.invoke(mBluetoothA2DP, mDevice);
}
}catch (Exception e){
} finally {
}
} else if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
try {
if (mIsConnect) {
Method connect = BluetoothHeadset.class.getDeclaredMethod("connect", BluetoothDevice.class);
connect.setAccessible(true);
connect.invoke(mBluetoothHeadset, mDevice);
} else {
Method disconnect = BluetoothHeadset.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
disconnect.setAccessible(true);
disconnect.invoke(mBluetoothHeadset, mDevice);
}
}catch (Exception e){
} finally {
}
} else if (profile == BluetoothProfile.HEALTH) {
mBluetoothHealth = (BluetoothHealth) proxy;
try {
if (mIsConnect) {
Method connect = BluetoothHealth.class.getDeclaredMethod("connect", BluetoothDevice.class);
connect.setAccessible(true);
connect.invoke(mBluetoothHealth, mDevice);
} else {
Method disconnect = BluetoothHealth.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
disconnect.setAccessible(true);
disconnect.invoke(mBluetoothHealth, mDevice);
}
}catch (Exception e){
} finally {
}
}
}
public void onServiceDisconnected(int profile) {
}
};
I hope this helps anyone trying to connect to Bluetooth Audio devices and headsets.
![Skizo-ozᴉʞS](https://i.stack.imgur.com/ijSum.png?s=256&g=1)
Skizo-ozᴉʞS
Hi, I'm an Android Enthusiast! From Spain I'm @SOreadytohelp
Updated on June 18, 2022Comments
-
Skizo-ozᴉʞS about 2 years
I'm trying to connect programmatically my device to for example on my Headsets... I had
KitKat
version and all worked perfect (Bluetooth
always was connecting without problems autommatically) but since I've updated toLolipop
it doesn't. I'd like to know if there is any way to connect any paired device of myAndroid phone
toBluetooth
when it turns on.Since now I've this code (gets the Device name and Device Address) because I thought with it I could connect doing something like
device.connect(MAC-Address);
but it didn't work...BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set < BluetoothDevice > pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device: pairedDevices) { mDeviceName.add(device.getName()); mDeviceMAC.add(device.getAddress()); } } bluetoothClass.setDeviceName(mDeviceName); bluetoothClass.setDeviceMac(mDeviceMAC);
Question
On my MotoG (KitKat) if I turn my
Bluetooth
it connects autommatically to device (if it's near and paired ofc...) but on my LG G3 I must go to Configuration/Bluetooth/Paired devices/ and there tap the device to connect... and I want to avoid this... should be possible?
I would like to know if there is any possibility to connect to specific Bluetooth just adding the
Device name
orDevice MAC
... More or less likeAndroid
does when I click on my device to connect it connects autommatically... I just want to get that CLICK event. I know thatAndroid
should connect autommatically to a paired device but there's any exceptions that doesn not ... the only way to pair it it's doing the click... that's why I'm wondering if it's there a way to do it... I've read and tested kcoppock answer but it still don't work ..Any suggestion?
EDIT
The main thing that I wanted to do is to connect my
Bluetooth
autommatically but since I've read on Hey you answer... I figured it out and I know it's anAndroid
bug, so the thing that I would like to do is select thepaired devices
and then click on the device that I want to connect (Without doing anyIntent
) and connect it, instead to goConfiguration/Bluetooth/...
. Btw I've read any answers onStackOverflow
and I found something withSockets
are they used to connectBluetooth
?Could be it a solution? -
Skizo-ozᴉʞS over 8 yearsI'll take a look when I have time thanks for the answer hexan :)
-
Skizo-ozᴉʞS over 8 yearsIf I need some code more to make it works I'll let you know :D
-
hexan over 8 yearsNo problem! :) You should just need to set mDevice and mBluetoothAdapter (BluetoothAdapter mBluetoothAdapter). If you need any more help don't hesitate.
-
x0a almost 6 yearsVery nice. Thanks. Do you know where we can read about why you need "getDeclaredMethod" rather than calling the methods directly?