我是android新手 . 我正在尝试将手机的麦克风音频路由到蓝牙耳机(Microlab T2) . 我的代码如下
public class MainActivity extends AppCompatActivity {
BluetoothAdapter bluetoothAdapter;
BluetoothDevice blueToothDevice;
BluetoothHeadset bluetoothHeadset;
AudioManager audioManager;
BluetoothManager bluetoothManager;
BluetoothSocket bluetoothSocket;
int b;
final Context con = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final StringBuilder stringBuilder = new StringBuilder();
final TextView txt2 = (TextView) findViewById(R.id.textView2);
try {
final TextView txt = (TextView) findViewById(R.id.textView);
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
Set<BluetoothDevice> getDevices = bluetoothAdapter.getBondedDevices();
String remoteDevice = null;
for (BluetoothDevice getDevice : getDevices) {
stringBuilder.append(getDevice);
remoteDevice = getDevice.toString();
}
// txt.setText(stringBuilder);
blueToothDevice = bluetoothAdapter.getRemoteDevice(remoteDevice);
bluetoothAdapter.getProfileProxy(this, listener, BluetoothProfile.HEADSET);
} catch (Exception e) {
TextView txt = (TextView) findViewById(R.id.textView);
txt.setText(e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int i, BluetoothProfile bluetoothProfile) {
final BluetoothProfile bluetoothProfileNew ;
final TextView txt = (TextView) findViewById(R.id.textView);
bluetoothHeadset = (BluetoothHeadset) bluetoothProfile;
Record record = new Record(bluetoothProfile);
record.start();
}
@Override
public void onServiceDisconnected(int i) {
final TextView txt = (TextView) findViewById(R.id.textView);
txt.setText(String.valueOf(i));
}
};
class Record extends Thread {
BluetoothProfile recordProfile;
final int bufferSize = 200000;
final short[] buffer = new short[bufferSize];
short[] readBuffer = new short[bufferSize];
public Record(BluetoothProfile profile) {
recordProfile = profile;
android.os.Process.setThreadPriority
(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
}
public void run() {
final TextView txt2 = (TextView) findViewById(R.id.textView2);
b = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioManager.startBluetoothSco();
audioManager.setSpeakerphoneOn(false);
audioManager.setBluetoothScoOn(true);
audioManager.setMicrophoneMute(false);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 50 , 2);
final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 44100);
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, b*10, AudioTrack.MODE_STREAM);
int buffersize = b;
audioRecord.startRecording();
//audioTrack.setPlaybackRate(44100) ;
audioTrack.play();
// txt2.setText(String.valueOf(audioManager.isBluetoothScoOn()));
final BluetoothProfile b = recordProfile;
runOnUiThread(new Runnable() {
@Override
public void run() {
/// TextView txt2 = (TextView) findViewById(R.id.textView2);
try {
// final String m = String.valueOf(audioTrack.getRoutedDevice());
// txt2.setText(String.valueOf(bluetoothHeadset.isAudioConnected(blueToothDevice)));
txt2.setText(String.valueOf(audioRecord.getRecordingState()));
} catch (Exception e) {
txt2.setText(e.toString());
}
}
});
byte[] buffer2 = new byte[buffersize];
while (true) {
audioRecord.read(buffer2, 0, buffersize);
audioTrack.write(buffer2, 0, buffer2.length);
}
}
}
}
运行此代码后,我听不到耳机的任何音频 . 但当我调试(通过BluetoothHeadset,AudioManager,AudioRecord,AudioTrack类的方法)代码时,我发现了
-
成功启动SCO连接
-
正在播放SCO音频
-
MIC音频正在录音
-
正在播放音频
但最后我听不到耳机的任何声音 . 我尝试了各种可能的原因,但没有运气 . 现在我完全无能为力 .
期待您的建议 .