簡體   English   中英

字符串消息無法通過藍牙在SPP服務器上完全打印?

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

我的Simple SPP服務器似乎只從MainActivity的ArrayList中拾取一項,為什么它不能全部打印出來? 同樣奇怪的是,當我從for循環中刪除“ \\ n”以設置最終消息時,我得到“ 86”,但是當沒有“ \\ n”時,什么都沒有打印出來?

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

在Eclipse服務器上讀出

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

對此進行了修復,但不確定是否能解決問題,但確實做到了:注釋掉了:

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

並簡單地替換為:

  String message = String.valueOf(temp1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM