问题描述
enter code here
public class BTReceiver extends BroadcastReceiver {
private static final String TAG = "BTReceiver";
int state = 0;
AudioManager audioManager;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceive(Context context,Intent intent) {
Log.d("Z","Received: Bluetooth");
try {
Bundle extras = intent.getExtras();
if (extras != null) { //Do something
audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
String action = intent.getAction();
Toast.makeText(context,action,Toast.LENGTH_LONG).show();
int state;
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,BluetoothHeadset.STATE_DISCONNECTED);
if (state == BluetoothHeadset.STATE_CONNECTED) {
setModeBluetooth();
} else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
// Calling stopVoiceRecognition always returns false here
// as it should since the headset is no longer connected.
setModeNormal();
Log.d(TAG,"Headset disconnected"); //$NON-NLS-1$
}
} else // audio
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
Log.d(TAG,"\nAction = " + action + "\nState = " + state); //$NON-NLS-1$ //$NON-NLS-2$
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
Log.d(TAG,"\nHeadset audio connected"); //$NON-NLS-1$
setModeBluetooth();
} else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
setModeNormal();
Log.d(TAG,"Headset audio disconnected"); //$NON-NLS-1$
}
}
}
} catch (Exception e) {
Log.d("Exception","Exception " + e.toString());
}
}
private void setModeBluetooth() {
try {
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private void setModeNormal() {
try {
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.stopBluetoothSco();
audioManager.setBluetoothScoOn(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
protected void onResume(){
super.onResume();
registerReceiver(mHeadsetBroadcastReceiver,new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
// Calling startVoiceRecognition does not result in immediate audio connection.
// So register for broadcast of audio connection states. This broadcast will
// only be sent if startVoiceRecognition returns true.
registerReceiver(mHeadsetBroadcastReceiver,new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
}
@Override
protected void onPause(){
unregisterReceiver(mHeadsetBroadcastReceiver);
super.onPause();
}
在这里,我尝试通过蓝牙设备的麦克风而不是音素进行输入。我还在清单中包括了接收机广播,但是我无法通过蓝牙麦克风进行输入。
<receiver
android:name="BTReceiver"
android:enabled="true"
android:permission="android.permission.BLUETOOTH">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
</intent-filter>
</receiver>
我只能从手机麦克风中获取输入
我在活动中使用了语音识别,但我刚刚意识到,如果连接了耳机,识别器仍然只能通过音位来收听。有什么方法可以使其通过耳机收听?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)