안드로이드 스튜디오 블루투스 데이터 송신 어플입니다. activity_main의 소스는 textview에 어떤 데이터에 글자를 작성해서 senddata를 누르면 연결된곳으로 데이터를 송신하게 되는데 제가 원하는 것은 textview에 따로 글자를 두지않고 버튼을 여러개 만들어서 1번 버튼을 꾹 누르고 있으면 a라는 string문자가 지속적으로 가고 2번 버튼을 누르면 b라는 string문자가 지속적으로 가게끔 만들고 싶습니다. 어떤식으로 구현하면 될까요.. 도와주세요
mBtnSendData.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
if(mThreadConnectedBluetooth != null) {
mThreadConnectedBluetooth.write(mTvSendData.getText().toString());
mTvSendData.setText("");
}
}
mBluetoothHandler = new Handler(){
public void handleMessage(android.os.Message msg){
if(msg.what == BT_MESSAGE_READ){
String readMessage = null;
try {
readMessage = new String((byte[]) msg.obj, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
mTvReceiveData.setText(readMessage);
}
}
};
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) {
byte[] bytes = str.getBytes();
try {
mmOutStream.write(bytes);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "데이터 전송 중 오류가 발생했습니다.", Toast.LENGTH_LONG).show();
}
}