Use Bluetooth for audio I/O

Along with its unique see-through display and head-mount camera, SmartEyeglass is also a Bluetooth headset for the host smart phone. Your app can receive and play back audio data with the microphone and speaker that are built into the SmartEyeglass device. To do this, you use the Android Bluetooth Hands-Free Profile (BT HFP) API to capture sound and play back audio as you would for any standard Bluetooth headset.

Required permission

You will have to add a permission request to your app’s Android manifest to use these functions.

  • In order to be allowed to make changes to the way your application handles audio preferences, add this line to your app’s AndroidManifest.xml file:
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
  • To record audio from the microphone, add this permission:
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

Audio I/O mode

An AudioManager instance provides the functions you need to enable the device to send and receive audio data.

  • The default audio mode, MODE_NORMAL, uses the host phone for audio I/O. To use the headset, set the mode to MODE_IN_CALL:
    AudioManager mAudioManager = 
        (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    // Switch to headset
    mAudioManager.setMode(AudioManager.MODE_IN_CALL); 
    

    You can now start the Bluetooth background process running:

    // Start audio I/O operation
    mAudioManager.startBluetoothSco();
  •  To switch off audio I/O, change to the normal audio mode:
    // Switch back to phone
    mAudioManager.setMode(AudioManager.MODE_NORMAL); 
    // Stop audio I/O operation
    mAudioManager.stopBluetoothSco();
    

How to record and play audio through the headset

These code snippets show how to record audio to a file through the headset microphone, and play audio files through the headset speaker using standard players such as MediaPlayer and SoundPool.

Record audio

Create a MediaRecorder object and set its source to the current microphone. If you have set the audio mode correctly, this is the Bluetooth microphone in the SmartEyeglass headset.

mRecorder mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

// Set file extension for the recorded audio file
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// Set a file path for the recorded audio file
mRecorder.setOutputFile(mFilePath);
// Set encoding of the audio
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.prepare();
// Start recording
mRecorder.start();

To clean up on termination:

mRecorder.stop();
mRecorder.reset();
mRecorder.release();
mRecorder = null;

Play back an audio file using MediaPlayer

Create a MediaPlayer object in your app’s initialization code, and use it to play audio files. If you have set the audio mode correctly, it plays through the headset speaker.

MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(mFileName);
// Audio file to be played
mPlayer.prepare();
mPlayer.start();
// Start playing audio file

To clean up on termination:

mPlayer.stop();
mPlayer.reset();
mPlayer.release();
mPlayer= null;

Play back an audio file using SoundPool

Create a SoundPool object in your app’s initialization code, and use it to play audio files. Set the audio mode in the object constructor, so that the object always plays through the headset speaker.

// (assume variables have been set)
SoundPool mSoundPool  = new SoundPool(num, AudioManager.MODE_IN_CALL, quality);
// Use the play() method to request for playback of an audio file.
mSoundPool.play(mSoundPool.load(mContext, R.raw.win, 1), leftVolume, rightVolume, 
    priority, loop, rate);

To clean up on termination:

mSoundPool.unload(soundId);
mSoundPool.release();
mSoundPool = null;

Comments 0

Sort by:

Showing 0 of 0 comments. Show all comments