2011-07-09 61 views
4

我使用的是android:BluetoothChat的示例应用程序。 但是,当我尝试发送字符串,它的大小更大的1024字节消息不传输。 我尝试改变下面的代码发送更多然后1024字节,但我没有在这个成功。 请帮帮我。在Android上通过蓝牙发送长文本信息

读码:

public void run() { 
      Log.i(TAG, "BEGIN mConnectedThread"); 
      byte[] buffer = new byte[1024]; 
      int bytes; 

      // Keep listening to the InputStream while connected 
      while (true) { 
       try { 
        // Read from the InputStream 
        bytes = mmInStream.read(buffer); 

        // Send the obtained bytes to the UI Activity 
        mHandler.obtainMessage(SmallWorld.MESSAGE_READ, bytes, -1, 
          buffer).sendToTarget(); 

       } catch (IOException e) { 
        Log.e(TAG, "disconnected", e); 
        connectionLost(); 
        break; 
       } 
      } 
     } 

发送代码:

public void write(byte[] buffer) { 
     try { 
      mmOutStream.write(buffer); 

      // Share the sent message back to the UI Activity 
      mHandler 
        .obtainMessage(SmallWorld.MESSAGE_WRITE, -1, -1, buffer) 
        .sendToTarget(); 
     } catch (IOException e) { 
      Log.e(TAG, "Exception during write", e); 
     } 
    } 

调用write:

String message="blabla"; 
byte[] send = message.getBytes(); 
     mChatService.write(send); 
+0

什么是您得到的错误消息? –

+0

我没有收到错误消息,它只发送1024字节的字符串(它切断了字符串),即使我改变了缓冲区的大小。 – Beno

+0

其中是发送字符串的代码 –

回答

2

写之后,你可能要刷新流数据强制被发送出去,因为在实际发送数据之前,数据流可能会缓冲数据并等待更多数据。 试试..

mmOutStream.write(buffer); 
mmOutStream.flush(); 
+0

谢谢,我明天会检查。 – Beno

+0

它的工作,但现在我有一个新问题。数组的最大大小是多少:new byte [1024]; 我得到内存泄漏 – Beno