2014-06-19 3 views
-1

나는 블루투스를 통해 바이너리 형식으로 바이트를 보내는 방법을 알고 싶었습니다. 순간 나는이 코드를 사용합니다에만이 방법으로블루투스를 통해 바이트 보내기

mSendButton = (Button) findViewById(R.id.button_send); 
    mSendButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // Send a message using content of the edit text widget 
      TextView view = (TextView) findViewById(R.id.edit_text_out); 
      String message = view.getText().toString(); 
      sendMessage(message); 
    } 
}); 

를 바이트 문자열과하지를 보냅니다.

도움 주셔서 감사합니다. 소스 코드에 따르면

private void sendMessage(String message) { 
    // Check that we're actually connected before trying anything 
    if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) { 
     Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); 
     return; 
    } 

    // Check that there's actually something to send 
    if (message.length() > 0) { 
     // Get the message bytes and tell the BluetoothChatService to write 
     byte[] send = message.getBytes(); 
     mChatService.write(send); 

     // Reset out string buffer to zero and clear the edit text field 
     mOutStringBuffer.setLength(0); 
     mOutEditText.setText(mOutStringBuffer); 
    } 
} 
+0

'sendMessage' 메소드의 소스는 무엇입니까? –

+0

@ Juan-devtopia.coop 여기에 소스가 있습니다 – davix10

답변

0

, 당신은 write 방법으로 이미 바이트 배열을 보내는 :

는 소스입니다. 그렇게 이런 식으로 뭔가를 호출 mChatService.write(byte[]data)라고하면 작동합니다 : 5 당신이 바이트 배열에, 즉, sendMessage 메소드의 호출 만 문자열을 번역되어 보낼 바이트 값이

mChatService.write(new byte[]{5}); 

그런 다음 블루투스 서비스로 전송됩니다.

+0

그러나이 방법으로 최대 127 개가 도착합니다. 맞습니까? 이 '11001100'과 같은 8 비트 2 진수를 보내려면 어떻게해야합니까? – davix10

관련 문제