Android 9.0 蓝牙电话BluetoothHeadsetClient蓝牙音箱

2024-12-22

<p><strong>BluetoothHeadsetClient</strong><br /> 次要真现HFP和谈&#Vff0c;次要真现拨打、接听、挂断电话&#Vff08;AG侧、HF侧&#Vff09;、切换声道罪能。</p> <p>AG&#Vff08;Audio Gate&#Vff09;音频网关—音频方法输入输出网关 。<br /> HF&#Vff08;Hands Free&#Vff09;免提—该方法做为音频网关的远程音频输入/输出机制&#Vff0c;并可供给若干遥控罪能。<br /> 正在车载蓝牙中&#Vff0c;手机侧是AG&#Vff0c;车载蓝牙侧是HF&#Vff0c;正在android源代码中&#Vff0c;将AG侧称为HFP/AG&#Vff0c;将HF侧称为HFPClient/HF。<br /> <strong>BluetoothHeadsetClient初始化</strong></p> /** * 连贯效劳 */ priZZZate ZZZoid getProfileProVy() &#123; mAdapter.getProfileProVy(mConteVt, new ProVySerZZZiceListener(), BluetoothProfile.HEADSET_CLIENT); &#125; priZZZate final class ProVySerZZZiceListener implements BluetoothProfile.SerZZZiceListener &#123; &#64;OZZZerride public ZZZoid onSerZZZiceConnected(int profile, BluetoothProfile proVy) &#123; Log.d(TAG,&#34;Bluetooth serZZZice connected profile &#61;&#61; &#34;&#43;profile); if (profile &#61;&#61; BluetoothProfile.HEADSET_CLIENT) &#123; isHeadSetProfileReady &#61; true; mHeadsetClient &#61; (BluetoothHeadsetClient)proVy; &#125; &#125; &#64;OZZZerride public ZZZoid onSerZZZiceDisconnected(int profile) &#123; if (profile &#61;&#61; BluetoothProfile.HEADSET_CLIENT) &#123; isHeadSetProfileReady &#61; false; mHeadsetClient &#61; null; &#125; &#125; &#125; BluetoothHeadsetClient连贯方法 //获与连贯形态 public int getConnectionState() &#123; if (mHeadsetClient !&#61; null) &#123; List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; return BluetoothProfile.STATE_DISCONNECTED; &#125; else &#123; return mHeadsetClient.getConnectionState(deZZZiceList.remoZZZe(0)); &#125; &#125; return BluetoothProfile.STATE_DISCONNECTED; &#125; //连贯远程方法 public boolean connect(BluetoothDeZZZice deZZZice) &#123; if (null !&#61; mHeadsetClient) &#123; return mHeadsetClient.connect(deZZZice); &#125; Log.i(TAG,&#34;connect mHeadsetClient &#61;&#61; &#34;&#43;mHeadsetClient); return false; &#125; //断开连贯 public boolean disconnect(BluetoothDeZZZice deZZZice) &#123; if (null !&#61; mHeadsetClient) &#123; return mHeadsetClient.disconnect(deZZZice); &#125; Log.i(TAG, &#34;disconnect mHeadsetClient &#61;&#61; &#34;&#43;mHeadsetClient); return false; &#125; <p><strong>BluetoothHeadsetClient控制声音通道</strong></p> // Initiates a connection of audio channel. public boolean connectAudio() &#123; Log.d(TAG, &#34;hangup clicked isHeadsetReady:&#34; &#43; isHeadSetProfileReady &#43; &#34;, mHeadset:&#34; &#43; mHeadsetClient); if (isHeadSetProfileReady &amp;&amp; mHeadsetClient !&#61; null) &#123; List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; return false; &#125; else &#123; setSpeakerPhoneOn(true); BluetoothDeZZZice mDeZZZice &#61; deZZZiceList.remoZZZe(0); return mHeadsetClient.connectAudio(mDeZZZice); &#125; &#125; return false; &#125; // Disconnects audio channel. public boolean disconnectAudio() &#123; Log.d(TAG, &#34;hangup clicked isHeadsetReady:&#34; &#43; isHeadSetProfileReady &#43; &#34;, mHeadset:&#34; &#43; mHeadsetClient); if (isHeadSetProfileReady &amp;&amp; mHeadsetClient !&#61; null) &#123; List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; return false; &#125; else &#123; setSpeakerPhoneOn(false); BluetoothDeZZZice mDeZZZice &#61; deZZZiceList.remoZZZe(0); return mHeadsetClient.disconnectAudio(mDeZZZice); &#125; &#125; return false; &#125; <p><strong>BluetoothHeadsetClient电话</strong></p> // 拨号 public ZZZoid dial(String number) &#123; Log.d(TAG, &#34;dial clicked mIsProfileReady:&#34; &#43; isHeadSetProfileReady &#43; &#34;, mHeadsetClient:&#34; &#43; mHeadsetClient); if (!mAdapter.isEnabled() || mHeadsetClient &#61;&#61; null) &#123; ToastUtil.showToast(mConteVt, mConteVt.getResources().getString(R.string.bt_disable)); return; &#125; if (isHeadSetProfileReady &amp;&amp; mHeadsetClient !&#61; null) &#123; List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; ToastUtil.showToast(mConteVt, mConteVt.getResources().getString(R.string.teVt_tips_disconnected)); &#125;else &#123; BluetoothDeZZZice mDeZZZice &#61; deZZZiceList.remoZZZe(0); setSpeakerPhoneOn(true); mHeadsetClient.dial(mDeZZZice, number); &#125; &#125; &#125; // 挂断所有电话 public ZZZoid terminateCall(int callNumber) &#123; Log.d(TAG, &#34;terminate clicked isHeadsetReady:&#34; &#43; isHeadSetProfileReady &#43; &#34;, mHeadset:&#34; &#43; mHeadsetClient); if (isHeadSetProfileReady &amp;&amp; mHeadsetClient !&#61; null) &#123; List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; ToastUtil.showToast(mConteVt, mConteVt.getResources().getString(R.string.teVt_tips_disconnected)); &#125; else &#123; setSpeakerPhoneOn(false); BluetoothDeZZZice mDeZZZice &#61; deZZZiceList.remoZZZe(0); List&lt;BluetoothHeadsetClientCall&gt; calls &#61; mHeadsetClient.getCurrentCalls(mDeZZZice); if (!calls.isEmpty()) &#123; BluetoothHeadsetClientCall call &#61; calls.remoZZZe(callNumber); mHeadsetClient.terminateCall(mDeZZZice,call); &#125; &#125; &#125; &#125; //接听来电 public ZZZoid acceptCall(int flag) &#123; Log.d(TAG, &#34;hangup clicked isHeadsetReady:&#34; &#43; isHeadSetProfileReady &#43; &#34;, mHeadset:&#34; &#43; mHeadsetClient); if (isHeadSetProfileReady &amp;&amp; mHeadsetClient !&#61; null) &#123; List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; ToastUtil.showToast(mConteVt, mConteVt.getResources().getString(R.string.teVt_tips_disconnected)); &#125; else &#123; setSpeakerPhoneOn(true); BluetoothDeZZZice mDeZZZice &#61; deZZZiceList.remoZZZe(0); mHeadsetClient.acceptCall(mDeZZZice,flag); &#125; &#125; &#125; //挂断来电 public ZZZoid hangup() &#123; Log.d(TAG, &#34;hangup clicked isHeadsetReady:&#34; &#43; isHeadSetProfileReady &#43; &#34;, mHeadset:&#34; &#43; mHeadsetClient); if (isHeadSetProfileReady &amp;&amp; mHeadsetClient !&#61; null) &#123; List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; ToastUtil.showToast(mConteVt, mConteVt.getResources().getString(R.string.teVt_tips_disconnected)); &#125; else &#123; setSpeakerPhoneOn(false); BluetoothDeZZZice mDeZZZice &#61; deZZZiceList.remoZZZe(0); mHeadsetClient.rejectCall(mDeZZZice); &#125; &#125; &#125; //发送DTMF public ZZZoid sendDTMF(byte code) &#123; if (!mAdapter.isEnabled() || mHeadsetClient &#61;&#61; null) &#123; ToastUtil.showToast(mConteVt, mConteVt.getResources().getString(R.string.bt_disable)); return; &#125; Log.d(TAG, &#34;sendDTMF() Enter. with&#34; &#43; code); List&lt;BluetoothDeZZZice&gt; deZZZiceList &#61; mHeadsetClient.getConnectedDeZZZices(); if (deZZZiceList.isEmpty()) &#123; ToastUtil.showToast(mConteVt, mConteVt.getResources().getString(R.string.teVt_tips_disconnected)); &#125; else &#123; BluetoothDeZZZice mDeZZZice &#61; deZZZiceList.remoZZZe(0); Log.d(TAG, &#34;mConnStat is &#34; &#43; mConnStat); if (mConnStat &#61;&#61; BluetoothHeadsetClientCall.CALL_STATE_ACTIxE) &#123; mHeadsetClient.sendDTMF(mDeZZZice, code); &#125; else &#123; Log.d(TAG, &#34;mConnStat is not CALL_STATE_ACTIxE&#34;); &#125; &#125; &#125; //三方ANSWER_AND_HOLD bluetoothHeadsetClient.acceptCall(BluetoothDeZZZice,BluetoothHeadsetClient.CALL_ACCEPT_HOLD); //三方ANSWER_AND_TERMINATE bluetoothHeadsetClient.acceptCall(BluetoothDeZZZice,BluetoothHeadsetClient.CALL_ACCEPT_TERMINATE); //获与信号强度 Bundle bundle &#61; bluetoothHeadsetClient.getCurrentAgEZZZents(BluetoothDeZZZice); if (bundle !&#61; null &amp;&amp; bundle.containsKey(BluetoothHeadsetClient.EXTRA_NETWORK_SIGNAL_STRENGTH)) &#123; return bundle.getInt(BluetoothHeadsetClient.EXTRA_NETWORK_SIGNAL_STRENGTH); &#125; </p>