Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to get a BluetoothHeadset proxy profile in Android.
I'm using the Kotlin code from the
SDK documentation
In my activity I have a field for the bluetoothHeadset and the profile listener to set it:
var bluetoothHeadset: BluetoothHeadset? = null
private val profileListener = object : BluetoothProfile.ServiceListener {
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
Log.d(TAG, "profile is $profile")
if (profile == BluetoothProfile.HEADSET) {
bluetoothHeadset = proxy as BluetoothHeadset
override fun onServiceDisconnected(profile: Int) {
if (profile == BluetoothProfile.HEADSET) {
bluetoothHeadset = null
In onCreate I get the bluetooth adapter and call getProxyProfile:
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter == null) {
Log.e(TAG, "No Bluetooth Adapter.")
} else {
// Verify bluetooth is enabled
if (!bluetoothAdapter.isEnabled) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
// Establish connection to the proxy.
bluetoothAdapter.getProfileProxy(
this@MainActivity,
profileListener,
BluetoothProfile.HEADSET
The weird thing is that bluetoothHeadset is never set.
The question can not connect to bluetooth headset in android covers situations in which getProxyProfile might fail which is indicated by it returning false. I've confirmed that in my case getProxyProfile is returning true and not false.
I've also confirmed in logcat that onServiceConnected is never called.
Why is it that getProxyProfile is returning true but the listener is never setting bluetoothHeadset?
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.