您的位置:首页 > 移动开发 > Android开发

Android中实现蓝牙录放音

2015-10-22 18:31 513 查看
分类: Android2014-08-23
18:03 2729人阅读 评论(7) 收藏 举报

Bluetooth

环境:Android4.2.2

基层应用:SoundRecorder

蓝牙一般有两种语音相关的模式是A2DP和SCO,前者是高质量音乐播放(俗称:只进不出),后者是语音通话(俗称:有进有出)。要实现语音从蓝牙进,那么它一字得处于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

版权声明:本文为博主原创文章,未经博主允许不得转载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: