Java 字符串消息未通过蓝牙在SPP服务器上完全打印?

Java 字符串消息未通过蓝牙在SPP服务器上完全打印?,java,android,bluetooth,Java,Android,Bluetooth,我的简单SPP服务器似乎只从我的主活动的ArrayList中提取一个项目,为什么它不会打印所有项目?同样奇怪的是,当我从for循环中删除“\n”以设置最终消息时,我得到了“86”,但当没有“\n”时,什么也没有打印 android连接测试类 ArrayList<Integer> temp; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInst

我的简单SPP服务器似乎只从我的主活动的ArrayList中提取一个项目,为什么它不会打印所有项目?同样奇怪的是,当我从for循环中删除“\n”以设置最终消息时,我得到了“86”,但当没有“\n”时,什么也没有打印

android连接测试类

ArrayList<Integer> temp;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connect_test);

    temp = getIntent().getIntegerArrayListExtra("lightInformation");

    out = (TextView) findViewById(R.id.out);

    out.append("\n...In onCreate()...");

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    CheckBTState();
}



@Override
public void onStart() {
    super.onStart();
    out.append("\n...In onStart()...");
}

@Override
public void onResume() {
    super.onResume();



    out.append("\n...In onResume...\n...Attempting client connect...");

    // Set up a pointer to the remote node using it's address.
    BluetoothDevice device = btAdapter.getRemoteDevice(address);

    // Two things are needed to make a connection:
    //   A MAC address, which we got above.
    //   A Service ID or UUID.  In this case we are using the
    //     UUID for SPP.
    try {
        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        AlertBox("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
    }

    // Discovery is resource intensive.  Make sure it isn't going on
    // when you attempt to connect and pass your message.
    btAdapter.cancelDiscovery();

    // Establish the connection.  This will block until it connects.
    try {
        btSocket.connect();
        out.append("\n...Connection established and data link opened...");
    } catch (IOException e) {
        try {
            btSocket.close();
        } catch (IOException e2) {
            AlertBox("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
        }
    }

    // Create a data stream so we can talk to server.
    out.append("\n...Sending message to server...");

    try {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) {
        AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    }



    String finalMessage = "";
    for (int i = 0; i < temp.size(); i++) {
        finalMessage =  finalMessage  + String.valueOf(temp.get(i)) + "\n";
    }

    String message = finalMessage;



    byte[] msgBuffer = message.getBytes();
    try {
        outStream.write(msgBuffer);
    } catch (IOException e) {
        String msg = "In onResume() and an exception occurred during write: " + e.getMessage();

用这个修复了它,不太清楚它为什么工作,但它做到了: 评论如下:

String finalMessage = "";
        for (int i = 0; i < temp1.size(); i++) {
            finalMessage =  finalMessage  + String.valueOf(temp1.get(i)) + "\n";
        }
String finalMessage = "";
        for (int i = 0; i < temp1.size(); i++) {
            finalMessage =  finalMessage  + String.valueOf(temp1.get(i)) + "\n";
        }
  String message = String.valueOf(temp1);