Select microphone source from headset instead of handy mic - java

is it possible to record audio from my Bluetooth headset instead of using the integrated android smartphone microphone?
When i am connecting my headset to my phone I can use the mic while I am phoning but in any other case it does not record anything, for example when I am trying to record audio with a recording app it uses the mic from the phone. SO is there any solution for selecting the headset mic instead or can I write an application for it?

You have to handle the following intent.
https://developer.android.com/reference/android/speech/RecognizerIntent.html#ACTION_VOICE_SEARCH_HANDS_FREE
You can use the following code to use bluetooth headset mic.
AudioManager audiomanager= (AudioManager)mContext.getSystemService(Context
.AUDIO_SERVICE);
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
mContext.registerReceiver(mBluetoothScoReceiver, intentFilter);
audiomanager.startBluetoothSco();
and listen for the broadcast receiver.
private BroadcastReceiver mBluetoothScoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
// Start recording audio
log("audio connected");
startRecording();
} else if(state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED){
log("audio disconnected");
}
}
};
and for switching(to disconnect bluetooth headset mic) you can use this code.
mAudioManager.stopBluetoothSco();

Related

Is it possible to scan for Bluetooth devices that are bonded with another device?

I am using the Android Bluetooth API to scan RSSI on nearby devices. I need to get the signal intensity of the devices even if they're connected with another device, i.e a smartwatch that's connected to another cellphone. Is it possible to scan this?
To scan the devices I'm using:
private final BroadcastReceiver bReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Create a new device item
DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
// Add it to our adapter
mAdapter.add(newDevice);
}
}
};

Is it possible to ignore plugged in Headset and still use phone speakers?

I would like to use the Phone Speakers while a Headset is plugged-in, instead of the Headset-Speakers.
Is it possible to do that? if yes, how can I achieve it?
I'm developing an App in Java for Android 9.
Detection if Headset is Plugged in works so far:
public class HeadsetIntentReceiver extends BroadcastReceiver {
private String TAG = "HeadSet";
public HeadsetIntentReceiver() {
Log.d(TAG, "Created");
}
#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 unplugged");
break;
case(1):
Log.d(TAG, "Headset plugged");
break;
default:
Log.d(TAG, "Error");
}
}
}
}
EDIT:
I tried the following Solutions, but Nothing happend:
How to mute audio in headset but let it play on speaker programmatically?
How to disable the wired headset programmatically in Java
Thanks in Advance for any helpful advice.
(Please don't answer "Unplug the Headset")
I think you are looking for AudioManager
Attach AUDIO_SERVICE to AudioManager, set AudioManager as MODE_IN_COMMUNICATION and then set your speaker phone on. You can use the speaker as if you were talking on the phone.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setSpeakerphoneOn(true);
Remember to restore it after use.
for example,
protected void onPause() {
super.onPause();
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(false);
}

Android Service to check whether the user screen on or off

Is there any way to create a service that runs forever on a background for Android user to check whether their screen on or off, etc?
I'm about to create an analytics, so I need to know when the user turn on or turn off their screen.
Thanks, I will appreciate all the input
You may use Android broadcast receiver to detect screen on and off.
Here is a good example of it
https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
you may also follow this thread
https://stackoverflow.com/a/9478013/2784838
You need to create broadcast receiver and manage screen on or off status.
Declare receiver in manifest:
<receiver android:name=".DeviceWakeUpReceiver" />
public class DeviceWakeUpReceiver extends BroadcastReceiver {
private static final String TAG = "DeviceWakeUpService";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive() called");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//End service when user phone screen off
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//Start service when user phone screen on
}
}
}
You cannot use a BroadcastReceiver for receiving screen off/on events.
Write a intent service which is started via boot complete listener and register for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON.
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(new ScreenOffOnReceiver(), filter);
class ScreenOffOnReiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// Screen OFF
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// Screen ON
}
}
}

Android Bluetooth detect when device is connected issue

I'm currently making an app that adapt the volume depending on the bluetooth device you are connected to.
I am having issue when I want to change the volume of the music
My app detect when a bluetooth device is connected and it can change the volume but it is like it goes too quickly
Here is some of the code:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice d = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
deviceConnected(d);
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
deviceDisconnected(d);
}
}
};
private void deviceConnected(BluetoothDevice bluetoothDevice) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean showToasts = prefs.getBoolean("show_toasts", false);
if (showToasts) { Toast.makeText(this, getResources().getString(R.string.connected_to) + " " + bluetoothDevice.getName(), Toast.LENGTH_SHORT).show(); }
mDeviceDAO = new DeviceDAO(this);
mDeviceDAO.open();
DeviceOptions device = mDeviceDAO.select(bluetoothDevice.getAddress());
if (device == null) { return; }
if(device.getActivated() == 0) {
return;
}
int v = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
saveLastVolume(v);
int volume = device.getVolume();
int flag = 0;
if(prefs.getBoolean("show_am_ui", false)) {
flag = AudioManager.FLAG_SHOW_UI;
}
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
volume,
flag);
}
If add a delay of around 3000ms when I change the volume, it will work properly but it's not a clean solution. In fact the toast for the connection will be displayed before the bluetooth device is "completely" connected
I also found an app that do the same thing but it seems that I'm doing the same thing.
Here is its githublink
What's your capture is the ACL connected or ACL disconnected, yes you are right once your toast appears i.e. the ACL connected does not mean the Bluetooth profile connected(exactly what you said "completely" connected), it only means the physical connection established between two device, you might as well to receive other event, e.g. HFP/A2dp connected, that's the real profile connected.
However the ACL disconnected is the milestone that the Bluetooth connection is really disconnected.

Android audioReceiver BroadcastReceiver (Android OS Bug)

I tried to create a audioReceiver Broadcast. To allow my user to ONLY listen to music if the HeadSet in plug-in.
Here is the code :
private BroadcastReceiver audioReceiver = new BroadcastReceiver()
{ #Override
public void onReceive(Context context, Intent intent)
{ AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
headsetIsPluggedIn = (intent.getExtras().getInt("state")==1);
if(headsetIsPluggedIn) //plugged
{ Log.d("", "BroadcastReceiver - Unmute sound");
audio.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
else //unplugged
{ Log.d("", "BroadcastReceiver - Mute sound");
audio.setStreamMute(AudioManager.STREAM_MUSIC, true);
// Inform user
Toast.makeText(DuplicatedPlayerActivity.this, "Please plug in your headset to enjoy the sound.", Toast.LENGTH_SHORT).show();
}
}
};
#Override
public void onResume() {
super.onResume();
if (HEADSET_ONLY) { registerReceiver(audioReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); }
Manifest
<receiver android:name="com.juno.brheadset.HeadsetStateReceiver">
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
OK, Here is the fun part. If you restart the device, run my app. This function will not work.
If you insert the headset jack in the phone at lest once! quit my app, restart the app. The function will work fine, until the user restart the phone again.
Now, why does this function only works if the user insert the jack in the phone?
"Now, Why does this function only works if the user insert the jack in the phone ?" --> Because you have written your code inside the receiver of action "android.intent.action.HEADSET_PLUG". Your code will only execute when you receive the headset_plug broadcast.

Categories