How to detect Bluetooth connection status

2,574 views
Skip to first unread message

Hos

unread,
Apr 1, 2011, 5:50:05 PM4/1/11
to android-platform
Hi everybody,

I want to detect the connection status of a paired Bluetooth headset
to the phone.
In Android 3.0 (API level 11) "BluetoothHeadset" class has
"isAudioConnected()" method.

- I don't know how to create (initialize) a "BluetoothHeadset" object.
It seems that I need to use
"getProfileProxy ()" but I need a sample code to find out how I need
to create and pass the parameters.

Thanks,
Hos

jaikumar Ganesh

unread,
Apr 1, 2011, 8:25:16 PM4/1/11
to android-...@googlegroups.com, Hos
adapter = BluetoothAdapter.getDefaultAdapter();

adapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);

private BluetoothProfile.ServiceListener mProfileListener =
new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};

Use mBluetoothHeadset to call the functions on BluetoothHeadset.java

Note: Audio Connection is different from Connection.

> --
> You received this message because you are subscribed to the Google Groups "android-platform" group.
> To post to this group, send email to android-...@googlegroups.com.
> To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.
>
>

Hos

unread,
Apr 5, 2011, 2:00:04 PM4/5/11
to android-platform, jaikumar Ganesh
Hi Jaikumar,
Thanks to your help now I can compile the code, thanks!

In the next step I want to have this code for Android 2.1 and 2.2 (as
you know they don't support BluetoothHeadset class).

Back to the main question again: I want to check the connection status
of a bluetooth headset (or simply if any bluetooth headset is
connected to the phone) without trying to establish connection to the
paired bluetooth headset (because this one is slow). Do you know for
any other way for doing this.

When I search the web, I found some non public Android APIs for
BluetoothHeadset class (ex: http://hi-android.info/src/android/bluetooth/BluetoothHeadset.java.html),
but after adding those code I am getting error with compiling the
code!


Thanks!
Hos

names khong

unread,
Nov 5, 2014, 12:35:08 PM11/5/14
to android-...@googlegroups.com

Hi Hos and JaiKumar,

I have some errors when built the project

    -mProfileListener cannot be resolved to a variable

 

at line

 

mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.

HEADSET);

 

    -Illegal modifier for the variable mProfileListener; only final is permitted

 

at line

private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {

 

          -Cannot refer to a non-final variable mBluetoothHeadset inside an inner class defined in a different method

at line

mBluetoothHeadset

= (BluetoothHeadset) proxy;

 

and line

mBluetoothHeadset

=

null;

 

Can you tell me how to correct?

Thank you so much!

Reply all
Reply to author
Forward
0 new messages