Android中实现蓝牙录放音

环境: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

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁保康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值