Android跟蓝牙耳机建立连接有两种方式Word文件下载.docx
- 文档编号:13394199
- 上传时间:2022-10-10
- 格式:DOCX
- 页数:7
- 大小:16.88KB
Android跟蓝牙耳机建立连接有两种方式Word文件下载.docx
《Android跟蓝牙耳机建立连接有两种方式Word文件下载.docx》由会员分享,可在线阅读,更多相关《Android跟蓝牙耳机建立连接有两种方式Word文件下载.docx(7页珍藏版)》请在冰豆网上搜索。
//1)SDPquery
SDPClientclient=SDPClient.getSDPClient(address);
if(DBG)log("
ConnectingtoSDPserver("
+address+"
)..."
);
if(!
client.connectSDPAsync()){
Log.e(TAG,"
FailedtostartSDPconnectionto"
+address);
mConnectingStatusHandler.obtainMessage(SDP_ERROR).sendToTarget();
client.disconnectSDP();
return;
}
if(isInterrupted()){
client.waitForSDPAsyncConnect(20000)){//20secs
if(DBG)log("
FailedtomakeSDPconnectionto"
SDPserverconnected("
)"
intheadsetChannel=client.isHeadset();
headsetchannel="
+headsetChannel);
inthandsfreeChannel=client.isHandsfree();
handsfreechannel="
+handsfreeChannel);
client.disconnectSDP();
第2步才是去真正建立RFCOMM连接。
//2)RFCOMMconnectmHeadset=newHeadsetBase(mBluetooth,address,channel);
intresult=mHeadset.waitForAsyncConnect(20000,//20secs
mConnectedStatusHandler);
HeadsetRFCOMMconnectionattempttook"
+(System.currentTimeMillis()-timestamp)+"
ms"
if(result&
lt;
0){
mHeadset.waitForAsyncConnect()error:
"
+result);
mConnectingStatusHandler.obtainMessage(RFCOMM_ERROR).sendToTarget();
}elseif(result==0){
+result+"
(timeout)"
}else{
mHeadset.waitForAsyncConnect()success"
mConnectingStatusHandler.obtainMessage(RFCOMM_CONNECTED).sendToTarget();
当RFCOMM连接成功建立后,BluetoothHeadsetDevice会收到RFCOMM_CONNECTED消息,它会调用BluetoothHandsfree来建立SCO连接,广播通知Headset状态变化的Intent
(PhoneApp和BluetoothSettings会接收这个Intent)。
caseRFCOMM_CONNECTED:
//success
Rfcommconnected"
if(mConnectThread!
=null){
mConnectThread.join();
}catch(InterruptedExceptione){
Log.w(TAG,"
Connectattemptcancelled,ignoring
RFCOMM_CONNECTED"
e);
mConnectThread=null;
setState(BluetoothHeadset.STATE_CONNECTED,BluetoothHeadset.RESULT_SUCCESS);
mBtHandsfree.connectHeadset(mHeadset,mHeadsetType);
break;
BluetoothHandsfree会先做一些初始化工作,比如根据是Headset还是Handsfree初始化不同的ATParser,并且启动一个接收线程从已建立的RFCOMM上接收蓝牙耳机过来的控制命令(也就是AT命令),接着判断如果是在打电话过程中,才去建立SCO连接来打通数据通道。
/*package*/
voidconnectHeadset(HeadsetBaseheadset,intheadsetType){
mHeadset=headset;
mHeadsetType=headsetType;
if(mHeadsetType==TYPE_HEADSET){
initializeHeadsetAtParser();
}else{
initializeHandsfreeAtParser();
headset.startEventThread();
configAudioParameters();
if(inDebug()){
startDebug();
if(isIncallAudio()){
audioOn();
建立SCO连接是通过SCOSocket实现的
/**RequesttoestablishSCO(audio)connectiontobluetooth
*headset/handsfree,ifoneisconnected.Doesnotblock.
*Returnsfalseiftheuserhasrequestedaudiooff,orifthere
*issomeotherimmediateproblemthatwillpreventBTaudio.
*/
synchronizedbooleanaudioOn(){
mOutgoingSco=createScoSocket();
if(!
mOutgoingSco.connect(mHeadset.getAddress())){
mOutgoingSco=null;
returntrue;
当SCO连接成功建立后,BluetoothHandsfree会收到SCO_CONNECTED消息,它就会去调用AudioManager的setBluetoothScoOn函数,从而通知音频系统有个蓝牙耳机可用了。
到此,Android完成了和蓝牙耳机的全部连接。
caseSCO_CONNECTED:
if(msg.arg1==ScoSocket.STATE_CONNECTED&
amp;
&
isHeadsetConnected()&
mConnectedSco==null){
RoutingaudioforoutgoingSCOconection"
mConnectedSco=(ScoSocket)msg.obj;
mAudioManager.setBluetoothScoOn(true);
}elseif(msg.arg1==ScoSocket.STATE_CONNECTED){
RejectingnewconnectedoutgoingSCOsocket"
((ScoSocket)msg.obj).close();
mOutgoingSco.close();
mOutgoingSco=null;
2.蓝牙耳机主动跟Android连首先BluetoothAudioGateway会在一个线程中收到来自蓝牙耳机的RFCOMM连接,然后发送消息给BluetoothHeadsetService。
mConnectingHeadsetRfcommChannel=-1;
mConnectingHandsfreeRfcommChannel=-1;
if(waitForHandsfreeConnectNative(SELECT_WAIT_TIMEOUT)==false){
if(mTimeoutRemainingMs&
gt;
Log.i(tag,"
selectthrea
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 蓝牙 耳机 建立 连接 有两种 方式