Android中实现蓝牙录放音

来源:互联网 发布:js获取svg元素属性值 编辑:程序博客网 时间:2024/05/17 12:06

环境:Android4.2.2

基层应用:SoundRecorder


蓝牙一般有两种语音相关的模式是A2DPSCO,前者是高质量音乐播放(俗称:只进不出),后者是语音通话(俗称:有进有出)。要实现语音从蓝牙进,那么它一字得处于SCO模式下,也是通话模式下。另外是一个问题是如果使用MediaPlayer播放音乐,会被重新切回到A2DP模式下,那么就会模式错乱。所以使用AudioPlayer是一个不错的选择。总体的工作有两个调整模式,使用AudioPlayer播放音频。就可以实现同时从蓝牙音频进出了。


输出到蓝牙耳机:

AudioManager mAm = ( AudioManager ) getSystemService(Context.AUDIO_SERVICE);

mAm.setMode(AudioManager.MODE_IN_CALL);

mAm.setBluetoothScoOn(true);

mAm.startBluetoothSco();


 

从蓝牙耳机输入

 

再次整理需求:

1.入口:蓝牙连接状态android HEADSET onServiceConnected

状态改变(只更改 配置文件 不会发送状态改变广播) ->

根据蓝牙名字或者MAC地址以及是否支持sco需要处理

->

连接上:开启录放

断开:关闭录放

 

只监听SCO:

AndroidManifest.xml

android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED

BTBroadcastReceiver.java

    private int mBluetoothHeadsetState = 0;

    @Override

    public void onReceive(Context context, Intent intent) {

        mContext = context;

        Log.e(LOG_TAG"BT connect changed!");

        

    if(intent.getAction().equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {

            mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,

                    BluetoothHeadset.STATE_DISCONNECTED);

            Log.d(LOG_TAG"==> new state: " + mBluetoothHeadsetState);

            updateBluetoothIndication();  // Also update any visible UI if necessary

        }

    }

 

    public void updateBluetoothIndication() {

        if (mBluetoothHeadsetState == BluetoothProfile.STATE_CONNECTED) {

            Log.i(LOG_TAG"BluetoothProfile.STATE_CONNECTED");

        } else if (mBluetoothHeadsetState == BluetoothProfile.STATE_DISCONNECTING) {

            Log.i(LOG_TAG"BluetoothProfile.STATE_DISCONNECTING");

        } else {

            Log.i(LOG_TAG"BluetoothProfile.OTHER");

        }

    }

from packages/apps/Phone/src/com/android/phone/PhoneGlobals.java:1449

 

总合来说就是:切换蓝牙模式+Android边录边播应用 =BTScoPlayback。

0 0