简体   繁体   中英

Write an outputstream after reading bluetooth socket android

I want to write after reading from a device (via Bluetooth socket ) The problem is that the while(true) finish when the socket is closed so I can't Write again.

This is the code:

        try {
            // This will block until it succeeds in connecting to the device
            // through the bluetoothSocket or throws an exception
            bluetoothSocket.connect();
            try {
                connectingThread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            outstream = bluetoothSocket.getOutputStream();
            outstream.write("P".getBytes());
            instream = bluetoothSocket.getInputStream();
            byte[] buffer = new byte[128];  // buffer store for the stream
            int bytes; // bytes returned from read()
            String readMessage = "";

             // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = instream.read(buffer);
                    if(bytes!= -1){
                    // Send the obtained bytes to the UI activity
                    readMessage = new String(buffer, 0, bytes);

                    arrayList.add(readMessage);
                    biodyMsg = TextUtils.join("", arrayList);
                    }

                } catch (Exception e) {

                    Log.d("Read Error", e.toString());

                }finally {
                    outstream = bluetoothSocket.getOutputStream();
                    outstream.write("F".getBytes());
                }
                break;
            }

        } catch (IOException connectException) {
            connectException.printStackTrace();
            try {    
                bluetoothSocket.close();
            } catch (IOException closeException) {
                closeException.printStackTrace();
            }
        }

Logcat

Can you help me

The only way a while (true) loop can terminate is via a break statement, or by getting an uncaught exception, and you haven't mentioned getting one. There are two possible exceptions in this code:

  • IOException : but the only way you can get an IOException with this code is if the peer resets the connection.
  • ArrayIndexOutOfBoundsException when constructing the String , if bytes is negative, which it can be: see below; but you haven't mentioned this either.

Your actual problem is that you aren't checking bytes for -1, which would indicate end of stream, which in turn would indicate that the peer has closed the connection, which in turn indicates that you should close the socket and break out of the read loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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