Connect to bluetooth headset in android

无人久伴 提交于 2019-12-23 03:13:03

问题


I have following problem: When my headset is turned on before my phone bluetooth is on, it will not connect to my phone. It will only connect to my phone when phone bluetooth is on and my headset is turned on second. I found out that I can still connect to my headset when bluetooth is turned on second (1. case), by going into androids bluetooth menu where my paired devices are listed and select my headset (although my headset is listed as 'connected'). This will connect my headset and my phone too.

What I now want to do is to perform the second way of connecting to my headset, when my phone bluetooth is turnd on second, programatically. I want to use android version 2.0 (API Level 5), so i tried BluetoothDevice.createRfcommSocketToServiceRecord and called connect() on my socket. But I'm always getting an Exception "Connection refused" or "Service discovery failed". I have used many UUID which I thought could be the right one and have anything to do with headset.


回答1:


You cannot do call the RFCOMM api to connect to the headset. It is meant to connect to the SPP profile , the Headset will use the HS/HFP Profiles. Using differnt UUIDs also on the RFCOMM apis will not solve the problem.

Basically this Headset level profile connection is done internally by Android (and there are no application level access to establish connections) Applications can only get state of the conenctions - see the BluetoothHeadset Class

the current behavior is the correct recommended behavior , i.e. the headset should connect to the Phone , so the phone should be on before the headset is turned on. It is recommended as such for good user experience and predictability, if both devices try to connect to each other randomly it will not get connected.



来源:https://stackoverflow.com/questions/5707894/connect-to-bluetooth-headset-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!