Page 87 - MDP2022-2
P. 87
private class ConnectedBluetoothThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedBluetoothThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "소켓 연결 중 오류가 발생했습니
다.", Toast.LENGTH_LONG).show();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = mmInStream.available();
if (bytes != 0) {
SystemClock.sleep(100);
bytes = mmInStream.available();
bytes = mmInStream.read(buffer, 0, bytes);
mBluetoothHandler.obtainMessage(BT_MESSAGE_READ, bytes, -
1, buffer).sendToTarget();
}
} catch (IOException e) {
break;
}
}
}
public void write(String str) {