2014-06-19 181 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)所以調用這樣的事情,應該工作:

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

哪裏5是要發送,換句話說,在調用sendMessage方法僅翻譯您的字符串成字節數組字節值然後發送到藍牙服務。

+0

但是,以這種方式達到127,對吧?我如何發送像這樣的'11001100'這樣的8位二進制數字? – davix10