2014-06-19 141 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