Этот код, который я использовал для решения проблемы, надеюсь, он поможет
общедоступный класс Чат расширяет активность {
Devc devc;
byte[] bytesent, bytesgot;
private BluetoothSocket mmSocket = null;
private InputStream mmInStream;
private OutputStream mmOutStream;
Handler handler;
Button send;
TextView getmessage;
EditText sentmessage;
TextView nowsentmessage;
TextView you;
int bytes = 0;
Thread thread;
private int audioSource = MediaRecorder.AudioSource.MIC;
private int samplingRate = 44100; /* in Hz */
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private int bufferSize = AudioRecord.getMinBufferSize(samplingRate,
channelConfig, audioFormat);
private int sampleNumBits = 16;
private int numChannels = 1;
AudioTrack audioPlayer;
AudioRecord recorder;
Boolean isRecording;
byte[] bytes2;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
getmessage = (TextView) findViewById(R.id.gottext);
sentmessage = (EditText) findViewById(R.id.entertext);
nowsentmessage = (TextView) findViewById(R.id.metext);
you = (TextView) findViewById(R.id.you);
send = (Button) findViewById(R.id.sent);
bufferSize += 2048;
recorder = new AudioRecord(audioSource, samplingRate, channelConfig,
audioFormat, 44100);
audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// display each item in a single line
}
};
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mmSocket = Devc.bluetoothSocket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
recorder.startRecording();
isRecording = true;
while (true) {
int readBytes = 0;
readBytes = recorder.read(bytesent, 0, bufferSize);
if (readBytes > 0) {
write(bytesent);
bufferSize += 2048;
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub\
while (true) {
int readBytes = 0;
try {
bytes = mmInStream.read(bytesgot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
audioPlayer.write(bytesgot, 0, bytes);
audioPlayer.play();
}
}
}).start();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
try {
mmInStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mmOutStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cancel();
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
mmOutStream.flush();
} catch (IOException e) {
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
cancel();
thread.stop();
Chat.this.finish();
// thread.stop();
}
}