I need that app will be able to record audio using bluetooth device mic. I found code from @Stephan, but I can't get it to work:
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
Log.d(TAG, "Audio SCO state: " + state);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
/*
* Now the connection has been established to the bluetooth device.
* Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
* new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
* AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
*
* After finishing, don't forget to unregister this receiver and
* to stop the bluetooth connection with am.stopBluetoothSco();
*/
unregisterReceiver(this);
}
}
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
Log.d(TAG, "starting bluetooth");
am.startBluetoothSco();
LINK: How to record sound using bluetooth headset
Can somebody please show me how to implement this code to activity or service code below, because I don't know much about programming.
CODE (activity):
public class MainActivity extends Activity {
public ResponseReceiver receiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent msgIntent = new Intent(MainActivity.this, SimpleIntentService.class);
startService(msgIntent);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.exit(0);
}
});
IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new ResponseReceiver();
registerReceiver(receiver, filter);
}
public class ResponseReceiver extends BroadcastReceiver {
public static final String ACTION_RESP = "alar.alar.com.rahelividinakes2.MESSAGE_PROCESSED";
@Override
public void onReceive(Context context, Intent intent) {
TextView result = (TextView) findViewById(R.id.textView);
String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);
result.setText(Html.fromHtml(text + "<br>" + result.getText()));
{
}
}
}
}
CODE (service):
public class SimpleIntentService extends IntentService {
public static final String PARAM_OUT_MSG = "omsg";
public SimpleIntentService() {
super("SimpleIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
final int RECORDER_SAMPLERATE = 8000;
final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
int frequency;
AudioRecord recorder;
int numCrossing, p, numSamples;
short audioData[];
boolean recording;
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
recording = true;
audioData = new short[bufferSize];
int[] values;
int k = 0, t = 0;
values = new int[2];
while (recording) {
numCrossing = 0;
numSamples = 0;
recorder.read(audioData, 0, bufferSize);
int mod = (bufferSize / 4) * 4;
for (p = 0; p < mod; p += 4) {
if (audioData[p] > 0 && audioData[p + 1] <= 0) numCrossing++;
if (audioData[p] < 0 && audioData[p + 1] >= 0) numCrossing++;
if (audioData[p + 1] > 0 && audioData[p + 2] <= 0) numCrossing++;
if (audioData[p + 1] < 0 && audioData[p + 2] >= 0) numCrossing++;
if (audioData[p + 2] > 0 && audioData[p + 3] <= 0) numCrossing++;
if (audioData[p + 2] < 0 && audioData[p + 3] >= 0) numCrossing++;
if (audioData[p + 3] > 0 && audioData[p + 4] <= 0) numCrossing++;
if (audioData[p + 3] < 0 && audioData[p + 4] >= 0) numCrossing++;
numSamples += 4;
}
for (p = 0; p < bufferSize; p++) {
if (audioData[p] > 0 && audioData[p + 1] <= 0) numCrossing++;
if (audioData[p] < 0 && audioData[p + 1] >= 0) numCrossing++;
numSamples++;
}
frequency = (8000 / numSamples) * numCrossing;
Log.d("proov", String.valueOf(frequency));
...(while loop continues)
Since the bluetooth should be detected once the apps started, it should be in the MainActivity, under OnCreate.
Hope it helps.