Android - MIC external for speechRecognition - android

I need to know which event is triggered when connecting a microphone or a headset to your device, and I am using speech recognition and have observed that does not follow the same flow as when nothing is connected to the device.
I would like to know if there is some kind of solution because the application I'm creating at the moment is tested in a Smartphone, but in the future will require connecting a microphone or a headset.
A greeting and I hope your answers
PS: At the moment in the code I have nothing on external microphone or headset, but at the moment everything is running according to device microphone and speaker.
EDIT
I see the way of knowing whether the headset is connected is as follows:
private class MusicIntentReceiver extends BroadcastReceiver {
#Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(TAG, "Headset is unplugged");
break;
case 1:
Log.d(TAG, "Headset is plugged");
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
}
But how to redirect the audio input to the microphone of the headset? and the audio output to the headphone output?

Related

check whether NFC Is active when camera is on in Android

I've made a screen where camera and nfc is used together but have found out that some devices or after Android 11, NFC seems to get blocked when camera is used.
Therefore, I was trying to implement where if NFC is blocked, I was trying to show a text in the screen that NFC is blocked due to camera but have a hard time checking if NFC is active or not.
Is there a way I can check is NFC is blocked or not when using a camera?
Finding a way the check if NFC is active or not when using a camera
I've not had any Android 11 device to find this myself and it depends on what the camera is doing to block/disable NFC but there is a Broadcast Receiver for the state of NFC
e.g.
// Listen to NFC setting changes
this.registerReceiver(mReceiver, filter);
}
// Listen for NFC being turned on while in the App
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
NfcAdapter.STATE_OFF);
switch (state) {
case NfcAdapter.STATE_OFF:
// Tell the user to turn NFC on if App requires it
break;
case NfcAdapter.STATE_TURNING_OFF:
break;
case NfcAdapter.STATE_ON:
// Enabled NFC;
break;
case NfcAdapter.STATE_TURNING_ON:
break;
}
}
}
};

Using a Bluetooth Headset as a Mic and Speaker in a Voice Chat App

I have a voice chat app, which works just fine. Currently I'm trying to make the app support bluetooth headsets in case any was connected. There are two cases that I need to handle:
The bluetooth headset is connected before the call starts.
The bluetooth headset connects to the device during the call.
For the first case, the app should automatically starts using the headset as its default input/output audio device. On the other hand, the app, upon headset connection to the device, should switch from the current input/output audio device to the bluetooth headset.
I was able to handle the first case successfully using the following code:
mAudioManager.setMode(0);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
As for the second case, I created a BroadcastReciever that listens when a bluetooth headset is connected as follows:
mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
mAudioManager.setMode(0);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
mAudioManager.setMode(0);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
}
}
};
The BroadcastReciever was able to detect headset connections/disconnections, and the call was directing the sound to the headset rather than the phone's earpiece. The issue is that the app keeps using the device's mic as the input audio device rather than the headset's. After a very long inspection, I realized that when the BroadcastReciever gets a notification that a headset is connected, I need to wait a little bit before calling mAudioManager.startBluetoothSco(); to get the app to use the headset's mic.
The question is, what kind of event should I be listening to, after knowing that the bluetooth headset is connected, so that I can start capturing the voice from the headset's mic?
It turns out the I shouldn't be listening to BluetoothDevice.ACTION_ACL_CONNECTED, rather the one I should consider is BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED. The BroadcastReciever should be initialized as follows:
mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1);
boolean result = state == BluetoothHeadset.STATE_CONNECTED;
mCallback.onConnected(result);
}
}
};

Bluetooth enable time

I am using the below code as per requirement from client to internally enable Bluetooth and disable it when exit the application.
if (!bluetoothAdapter.isEnabled()) {
MMLogger.logInfo(MMLogger.LOG_BLUETOOTH, "BluetoothSyncController - Bluetooth was OFF, so Turn it ON");
bluetoothAdapter.enable();
try {
Thread.sleep(WAIT_FOR_SOMETIME_TO_START_BLUETOOTH);
} catch (InterruptedException ignore) {
}
MMLogger.logInfo(MMLogger.LOG_BLUETOOTH, "BluetoothSyncController - Bluetooth turned ON");
}
IS there any standard time for WAIT_FOR_SOMETIME_TO_START_BLUETOOTH ? I mean any documentation ?
You might try this answer. There seem to be some standard bluetooth events and handlers out there.
From that source: There are events that your activity can manage such as
STATE_OFF, STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF
and you can catch these with a BroadcastReciever. First you want to make sure that you grant permissions for bluetooth inside of your manifest with:
<uses-permission android:name="android.permission.BLUETOOTH" />
Then you can create a custom broadcast receiver that has the following onReceive():
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch(state) {
case BluetoothAdapter.STATE_OFF:
..
break;
case BluetoothAdapter.STATE_TURNING_OFF:
..
break;
case BluetoothAdapter.STATE_ON:
..
break;
case BluetoothAdapter.STATE_TURNING_ON:
..
break;
}
}
}
Then instead of making a thread to wait you can have a receive event trigger the code you want to run. For more info on using a BroadcastReciever, see the link I provided or go straight to the android documentation.

Android - PhoneStateListener onCallStateChanged() not working

I want to implement onCallStateChanged() of PhoneStateListener and use it's functionality to change audio settings when a call is received.
private final class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
Utils.log("CallStateListener");
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Utils.log("Idle");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Utils.log("OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(prefs.getBoolean(Constants.PREF_OPTION_1, false)){
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxStreamValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
int volumeLevel = prefs.getInt(Constants.PREF_VOLUME_LEVEL, Constants.PREF_VOLUME_LEVEL_DEFAULT);
int volume = (int) Math.ceil(maxStreamValue * (volumeLevel / 100.0));
if(prefs.getBoolean(Constants.PREF_OPTION_3, false)){
Utils.log("Option3 true ringing");
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
Utils.changeNotificationVolume(audioManager, volume);
Utils.log("RINGING");
}
break;
}
callState = state;
}
}
I want to silent just the notifications if Option 1 and Option 3 are on, but a lots of new companies provide only one option for notifications and calls.
If Option 1 and Option 3 are on, i have set my device to SILENT MODE, but whenever a call is received i want my device to ring. Therefore in CALL_STATE_RINGING, i have my code to turn off SILENT MODE and ring the device.
This things only works sometime. After trying a lots of devices i have found out that usually this thing doesn't work on mobiles which lags a lot( are really slow).
I think this is because if audio settings are changed before mobile starts ringing this work, and if not this doesn't work even if settings are changed. Am i right? Is there any solution to this problem? Is there a way to delay ringing of android device? Or can i do something to make it work every time?

How to play sound when headphone connected in android programatically?

I am working on one of the project which need to play sound simultaneously when headphone is connected.
I am using below code but no luck
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
am.setSpeakerphoneOn(true);
You need to use a BroadcastReceiver to handle the action sent when the headset is plugged. You need after that to check if the action sent by the broadcast equals Intent.ACTION_HEADSET_PLUG in the onReceive method, then you can play your sound using MediaPlayer
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
//Headset is unplugged
break;
case 1:
//Headset is plugged
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
Please see this answer https://stackoverflow.com/a/13610712/2354845

Categories

Resources