Android连接蓝⽛打开SCO,实现蓝⽛⽿机输⼊在很多应⽤中,都存在使⽤蓝⽛进⾏录⾳。
⾸先我们写⼀个BroadcastReceiver,进⾏监听蓝⽛状态改变。
public class BluetoothStatusRec extends BroadcastReceiver{
private String TAG = "BluetoothStatusRec";
public static AudioManager mAudioManager ;
private TurnOffBtn mTurnOffBtn;
public static boolean mIsScoEnable = fal;
public BluetoothStatusRec(TurnOffBtn ins){
mTurnOffBtn = ins;
}
@Override
大脑学院
public void onReceive(Context context, Intent intent) {
mAudioManager = (AudioManager) SystemService(Context.AUDIO_SERVICE);2022qs世界大学排名公布
mAudioManager.tMode(AudioManager.MODE_IN_COMMUNICATION);
Action() == BluetoothDevice.ACTION_ACL_CONNECTED){
Log.d(TAG,"连接成功");
mTurnOffBtn.turnOn();
mTurnOffBtn.changeBtn();
@Override
public void onReceive(Context context, Intent intent) {
int state = IntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
Log.d(TAG, "Audio SCO state: " + state);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) { //SCO打开后(由⼿机输⼊变为蓝⽛⽿机输⼊),进⾏录⾳ mAudioManager.tBluetoothScoOn(true);
context.unregisterReceiver(this); //记得接收后解除注册
//startRecord(); //进⾏录⾳,此时声源来⾃蓝⽛⽿机
mTurnOffBtn.changeBtn();
英语六级分数计算器
mTurnOffBtn.turnOn();
Log.d("ww","连接sco成功");
}el{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
英语语法学习mAudioManager.startBluetoothSco();
mTurnOffBtn.turnOff();
mTurnOffBtn.changeBtnOn();
}
}
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
mAudioManager.startBluetoothSco();
}Action() == BluetoothDevice.ACTION_ACL_DISCONNECTED){
mTurnOffBtn.turnOff();
noreply
mTurnOffBtn.changeBtnOn();
}
}
public interface TurnOffBtn{
void turnOff();
void turnOn();
void changeBtn();//连接蓝⽛后禁⽤上⾯的start,stop按钮
void changeBtnOn();//连接蓝⽛后启⽤上⾯的start,stop按钮
}
}
然后在清单⽂件中声明这个BroadcastReceiver,
<receiver android:name="com.tanyaping.audio.blueRec.BluetoothStatusRec">
<intent-filter>every dog has his day
<action android:name="BluetoothDevice.ACTION_ACL_CONNECTED" />
<action android:name="BluetoothDevice.ACTION_ACL_DISCONNECTED" /> </intent-filter>
</receiver>
这个蓝⽛的⼴播(BluetoothStatusRec)也需要动态注册,
然后在清单⽂件中声明所需权限:
<us-permission android:name="android.permission.BLUETOOTH"/>
<us-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<us-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/> <us-permission
android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
cba是什么意思<!-- 录⾳权限 -->加班费英文
<us-permission android:name="android.permission.RECORD_AUDIO" />
foxi如果没有声明相应权限,⽿机录⾳会失败。