简体   繁体   English

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

[英]String message not fully printing on SPP server via bluetooth?

My Simple SPP server seems to be only picking up one item from my ArrayList from my MainActivity, how come it won't print it all? 我的Simple SPP服务器似乎只从MainActivity的ArrayList中拾取一项,为什么它不能全部打印出来? Also strangely when I remove the "\\n" from my for loop to set final message i get "86" but when there is no "\\n" nothing is printed??? 同样奇怪的是,当我从for循环中删除“ \\ n”以设置最终消息时,我得到“ 86”,但是当没有“ \\ n”时,什么都没有打印出来?

android ConnectTest class android ConnectTest类

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();

readout on eclipse server 在Eclipse服务器上读出

Server Started. Waiting for clients to connect...
Remote device address: E458E7526EE3
Remote device name: SAMSUNG-SM-G920V
83
BlueCove stack shutdown completed

Fixed it with this, not really sure why it worked but it did: commented out this: 对此进行了修复,但不确定是否能解决问题,但确实做到了:注释掉了:

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

and replaced simply with this: 并简单地替换为:

  String message = String.valueOf(temp1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM