講稿網(wǎng)上藍(lán)牙_第1頁(yè)
講稿網(wǎng)上藍(lán)牙_第2頁(yè)
講稿網(wǎng)上藍(lán)牙_第3頁(yè)
講稿網(wǎng)上藍(lán)牙_第4頁(yè)
講稿網(wǎng)上藍(lán)牙_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、藍(lán)牙模塊AUDIOPHONEBluetoothSettingsHEADSET_S E_CHA NGED_ACT ONHEADSET_S E_CHA NGED_ACTIONENABLED_ACTION DISABLED_ACTIONPAIRING_REQUEST_A CTIONAc ivityBluetoothHeadsetBLUE OO HAIDLBluetoothDev ceB uetoothHeadset Serv ceENABLED_ACTION BONDING_CREATED_ACT ON DISABLED_ACTION REMOTE_DEVICE_D SCONNECT_REQUEST

2、ED_ACTIONA DLBluetoo udioGateBluetooth EventLoopBluetoothDevice ServiceBluetoothHandsfreeSDPC ientwaysetBluetoothScoOnSCOSocketandro d_bluetooth_SC OSocketandroid_bluetooth_Blu etoo udioGatewayandroid_server_Bluet oothEventLoopandroid_server_Bluet oothDev erv ceandroid_bluetooth_SD PC ntRegister SDP

3、 forHeadsendsf eeDBUSDBUSBlueZFunct on Ca lentAIDLActiv tyInvoke ScriptDBUS在 SystemServer 啟動(dòng)的時(shí)候,會(huì)生成一個(gè) BluetoothDeviervice 的實(shí)例,BluetoothDeviervice 會(huì)生成一個(gè) BluetoothEventLoop 實(shí)例,它們兩者均通過(guò) DBUS 來(lái)和BlueZ 通信。BluetoothDeviervice 是通過(guò) DBUS 向 BlueZ 發(fā)送命令,而命令的返回結(jié)果則NINIJ IJNIJNIJNI/ Skip Bluetooth if we have an emul

4、ator kernel/ TODO: Use a more reliable check to see if this product should/ support Bluetooth - see bug 988521if (SystemProperties.get(ro.kernel.qemu).equals(1) Log.i(TAG, Registering null Bluetooth Service (emulator);ServiceManager.addService(Context.BLUETOOTH_SERVICE, null); else if (factoryTest =

5、 SystemServer.FACTORY_TEST_LOW_LEVEL) Log.i(TAG, Registering null Bluetooth Service (factory test); ServiceManager.addService(Context.BLUETOOTH_SERVICE, null); else Log.i(TAG, Starting Bluetooth Service.); bluetooth = new BluetoothDeviervice(context); bluetooth.init();ServiceManager.addService(Conte

6、xt.BLUETOOTH_SERVICE, bluetooth); bluetoothOn = Settings.System.get(mContentResolver,Settings.System.BLUETOOTH_ON, 0);if (bluetoothOn 0) bluetooth.enable(null);android_bluetooth_Hea dsetBaseHeadsetBaseAud oManagerInCallScreenBluetoothPINReque stWirelessSettingsPhoneApp是由 BlueZ 通過(guò) DBUS 傳回給 BluetoothE

7、ventLoop 的( 具體交互請(qǐng)參見(jiàn) BlueZ 的dbus_api.txt ) ,BlueZ 也會(huì)通過(guò) DBUS 向 BluetoothEventLoop 發(fā)送一些事件通知。 BluetoothEventLoop 和外部的接口是通過(guò)預(yù)先定義的ent,初始的時(shí)候藍(lán)牙是沒(méi)有使能的,要通過(guò) BluetoothSettings 或者WirelessSettings 來(lái)打開(kāi)藍(lán)牙設(shè)備,然后通過(guò) BluetoothSettings 去查找附近的其他藍(lán)牙設(shè)備,找到后可以建立接和配對(duì)。M 連藍(lán)牙耳機(jī)Android 實(shí) 現(xiàn)了對(duì) Headset 和 Handsfree 兩種 profile 的支持。其實(shí)現(xiàn) Bl

8、uetoothHeadsetService,在 PhoneApp 創(chuàng)建的時(shí)候會(huì)啟動(dòng)它。是BluetoothHeadsetService 通過(guò)接收 ENABLED_ACTION、BONDING_CREATED_ACTION 、DISABLED_ACTION 和 REMOTE_DEVICE_DISCONNECT_REQUESTEDACTION 來(lái)改變狀態(tài),它也會(huì)Phone 的狀態(tài)變化。BluetoothHeadsetService 收到ENABLED_ACTION 時(shí),會(huì)先向BlueZHeadset 和Handsfree兩種 profile (通過(guò)執(zhí) 行 sdptool 來(lái)實(shí)現(xiàn)的 ,均作為 Aud

9、io Gateway ),然后讓 BluetooudioGateway 接收M 連接,讓 BluetoothHandsfree 接收 SCO 連接(這些操作都是為了讓藍(lán)牙耳機(jī)能主動(dòng)連上 Android)。if (action.equals(Bluetoothent.ENABLED_ACTION) / SDP server may not be ready, so wait 3 seconds before/ registering records./ TODO: Use a different mechanism to register SDP records,/t actually ACKs

10、 on sucs, sot we can retry rather/n hardcoding a 3 second guess.mHandler.sendMessageDelayed(mHandler.obtaessage(REGISTER_SDP_ RECORDS),3000);mAg.start(ingConnectionHandler);entFilter filter = newentFilter(Bluetoothent.BONDING_CREATED_ACTION);filter.addAction(Bluetoothent.REMOTE_DEVICE_DISCONNECT_REQ

11、UESTED_ACTION); filter.addAction(Bluetoothent.ENABLED_ACTION);filter.addAction(Bluetoothent.DISABLED_ACTION); registerReceiver(mBluetoothentReceiver, filter);mPhone.registerForPhoneSeChanged(mSeChangeHandler, PHONE_SE_CHANGED, null);if (getSystemService(Context.BLUETOOTH_SERVICE) != null) mBndsfree

12、= new BluetoothHandsfree(this, phone); startService(newent(this, BluetoothHeadsetService.class); else / Device is not bluetooth capable mBndsfree = null;BluetoothHeadsetService 收到 DISABLED_ACTION 時(shí),會(huì)停止 BluetooBluetoothHandsfree。udioGateway 和Android 跟藍(lán)牙耳機(jī)建立連接有兩種方式。1.Android 主動(dòng)跟藍(lán)牙耳機(jī)連BluetoothSettings

13、中和 藍(lán)牙耳機(jī) 配對(duì)上之 后, BluetoothHeadsetService 會(huì)收到 BONDING_CREATED_ACTION,這個(gè)時(shí)候 BluetoothHeadsetService 會(huì)主動(dòng)去和藍(lán)牙耳機(jī)建M 連接。立M 連接的真正實(shí)現(xiàn)是在 ConnectionThread 中,它分兩步,第一步先通過(guò)SDPCnt 查詢(xún)藍(lán)牙設(shè)備時(shí)候支持 Headset 和 Handsfree profile。/ 1) SDP querySDPCnt cnt = SDPCnt.getSDPCnt(address);if (DBG) log(Connecting to SDP server ( + addre

14、ss + ).); if (!cnt.connectSDPAsync() Log.e(TAG, Failed to start SDP connection to + address);mConnectingSusHandler.obtaessage(SDP_ERROR).sendTo(); cnt.disconnectSDP();return;if (iserrupted() cnt.disconnectSDP(); return;if (!cnt.waitForSDPAsyncConnect(20000) / 20 secsif (DBG) log(Failed to make SDP c

15、onnection to + address); mConnectingSusHandler.obtaessage(SDP_ERROR).sendTo(); cnt.disconnectSDP();return;if (DBG) log(SDP server connected ( + address + ); headsetChannel =nt.isHeadset();if (action.equals(Bluetoothent.BONDING_CREATED_ACTION) if (mSe = BluetoothHeadset.SE_DISCONNECTED) / Lets try an

16、d initiate anM connection try mBinder.connectHeadset(address, null); catch (RemoteException e) if (action.equals(Bluetoothent.DISABLED_ACTION) mBndsfree.onBluetoothDisabled();mAg.stop();mBndsfree.onBluetoothEnabled();第二步才是去真正建立M 連接。當(dāng)M 連接成功建立后,BluetoothHeadsetDevice 會(huì)收到M_CONNECTED消息,它會(huì)調(diào)用 BluetoothHan

17、dsfree 來(lái)建立 SCO 連接,廣播通知 Headset 狀態(tài)變化的ent(PhoneApp 和 BluetoothSettings 會(huì)接收這個(gè)ent)。caseM_CONNECTED:/ susif (DBG) log(m connected); if (mConnectThread != null) try mConnectThread.join(); catcherruptedException e) Log.w(TAG,Connectattemptcancelled,ignoring/ 2)M connectmHeadset = new HeadsetBase(mBluetooth

18、, address, channel); if (iserrupted() return;result = mHeadset.waitForAsyncConnect(20000,/ 20 secsmConnectedSusHandler); if (DBG) log(HeadsetM connection attempt took +(System.currentTimeMillis() - timest) + ms); if (iserrupted() return;if (result 0) try Log.i(tag, select thread timed out, but + mTi

19、meoutRemainingMs+msofwaiting remain.);Thread.sleep(mTimeoutRemainingMs); catch (erruptedException e) Log.i(tag, select thread waserrupted (2),exiting);merrupted = true;case SCO_CONNECTED:if (msg.arg1 = ScoSocket.SE_CONNECTED & isHeadsetConnected()&mConnectedSco = null) if (DBG) log(Routing audio for

20、 outgoing SCO conection); mConnectedSco = (ScoSocket)msg.obj; mAudioManager.setBluetoothScoOn(true); else if (msg.arg1 = ScoSocket.SE_CONNECTED) if (DBG) log(Rejecting new connected outgoing SCO socket); (ScoSocket)msg.obj).close();mOutgoingSco.close();mOutgoingSco = null; break;return true;如果當(dāng)前是正在連

21、接狀態(tài),則先停掉已經(jīng)存在的BluetoothHandsfree 去建立 SCO 連接。ConnectThread ,并直接調(diào)用如果當(dāng)前是已連接的狀態(tài),這種情況是一種錯(cuò)誤 case,所以直接斷掉所有連接。藍(lán)牙耳機(jī)也可能會(huì)主動(dòng)發(fā)起 SCO 連接, BluetoothHandsfree 會(huì)接收到一個(gè) SCO_ACCEPTED 消息,它會(huì)去調(diào)用 AudioManager 的 setBluetoothScoOn 函數(shù),從而通知音case BluetoothHeadset.SE_CONNECTED:if (DBG) log(Already connected to + mHeadsetAddress +

22、, disconnecting +info.mAddress); mBluetooth.disconnectRemoteDeviceAcl(info.mAddress); break;case BluetoothHeadset.SE_CONNECTING:/ If we are here, we are in danger of a race condition/ingm connection, but we are also attempting an/ outgoing connection. Lets try anderrupt the outgoing/ connection. mCo

23、nnectThreaderrupt();/ Now continue with new connection, including calling callbackmHeadset=newHeadsetBase(mBluetooth,mHeadsetAddress,info.mSocketFd,mChan,mConnectedSusHandler);mHeadsetType = type;setSe(BluetoothHeadset.SE_CONNECTED, BluetoothHeadset.RESULT_SUCS);mBndsfree.connectHeadset(mHeadset, mHeadsetType);/ Make suret old

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論