当前位置:   article > 正文

Android连接蓝牙打开SCO,实现蓝牙耳机输入_setbluetoothscoon

setbluetoothscoon

在很多应用中,都存在使用蓝牙进行录音。

首先我们写一个BroadcastReceiver,进行监听蓝牙状态改变。

  1. public class BluetoothStatusRec extends BroadcastReceiver{
  2. private String TAG = "BluetoothStatusRec";
  3. public static AudioManager mAudioManager ;
  4. private TurnOffBtn mTurnOffBtn;
  5. public static boolean mIsScoEnable = false;
  6. public BluetoothStatusRec(TurnOffBtn ins){
  7. mTurnOffBtn = ins;
  8. }
  9. @Override
  10. public void onReceive(Context context, Intent intent) {
  11. mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  12. mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
  13. if(intent.getAction() == BluetoothDevice.ACTION_ACL_CONNECTED){
  14. Log.d(TAG,"连接成功");
  15. mTurnOffBtn.turnOn();
  16. mTurnOffBtn.changeBtn();
  17. context.registerReceiver(new BroadcastReceiver() { //动态注册一个接受SCO状态改变的BroadcastReceiver
  18. @Override
  19. public void onReceive(Context context, Intent intent) {
  20. int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
  21. Log.d(TAG, "Audio SCO state: " + state);
  22. if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) { //SCO打开后(由手机输入变为蓝牙耳机输入),进行录音
  23. mAudioManager.setBluetoothScoOn(true);
  24. context.unregisterReceiver(this); //记得接收后解除注册
  25. //startRecord(); //进行录音,此时声源来自蓝牙耳机
  26. mTurnOffBtn.changeBtn();
  27. mTurnOffBtn.turnOn();
  28. Log.d("ww","连接sco成功");
  29. }else{
  30. try {
  31. Thread.sleep(1000);
  32. } catch (InterruptedException e) {
  33. e.printStackTrace();
  34. }
  35. mAudioManager.startBluetoothSco();
  36. mTurnOffBtn.turnOff();
  37. mTurnOffBtn.changeBtnOn();
  38. }
  39. }
  40. }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
  41. mAudioManager.startBluetoothSco();
  42. }if(intent.getAction() == BluetoothDevice.ACTION_ACL_DISCONNECTED){
  43. mTurnOffBtn.turnOff();
  44. mTurnOffBtn.changeBtnOn();
  45. }
  46. }
  47. public interface TurnOffBtn{
  48. void turnOff();
  49. void turnOn();
  50. void changeBtn();//连接蓝牙后禁用上面的start,stop按钮
  51. void changeBtnOn();//连接蓝牙后启用上面的start,stop按钮
  52. }
  53. }

然后在清单文件中声明这个BroadcastReceiver,

  1. <receiver android:name="com.tanyaping.audio.blueRec.BluetoothStatusRec">
  2. <intent-filter>
  3. <action android:name="BluetoothDevice.ACTION_ACL_CONNECTED" />
  4. <action android:name="BluetoothDevice.ACTION_ACL_DISCONNECTED" />
  5. </intent-filter>
  6. </receiver>

这个蓝牙的广播(BluetoothStatusRec)也需要动态注册,

然后在清单文件中声明所需权限:

  1. <uses-permission android:name="android.permission.BLUETOOTH"/>
  2. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  3. <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
  4. <uses-permission
  5. android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
  6. <!-- 录音权限 -->
  7. <uses-permission android:name="android.permission.RECORD_AUDIO" />
如果没有声明相应权限,耳机录音会失败。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/192193
推荐阅读
相关标签
  

闽ICP备14008679号