خرید بک لینک

Vote count: 0

I'm coecting 2 devices via bluetooth and when I press the logout button on one device I want to send a message to the other device (telling the other to also logout), close the bluetooth coection, and close the current activity (i.e. go back to my login activity)

The problem is I keep getting this exception which makes me think I'm not closing my coections properly:

java.io.IOException: bt socket closed, read retu: -1
    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:517)
    at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
    at java.io.InputStream.read(InputStream.java:162)
    at com.example.BTService$CoectedThread.run(BTService.java:269)

BTService.java:269 is where the coectedThread is reading from the input stream

In a bit more detail:

When logout is pressed, I basically destroy the MainActivity, and in onDestroy() I stop my bluetooth service:

this.stopService(new Intent(this, BTService.class))

Which should call onDestroy() of my service, which calls stopCoect():

public static void stopCoect(){
        if (coectThread != null) {
            coectThread.cancel();
            coectThread = null;
        }

        if (coectedThread != null) {
            coectedThread.cancel();
            coectedThread = null;
        }
    }

So as soon as the service is destroyed by clicking logout, the cancel() method of my coectedThread should be called:

public static class CoectedThread extends Thread {
        private BluetoothSocket mmSocket;
        private InputStream mmInStream;
        private OutputStream mmOutStream;

        public CoectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;

            //Tell other phone that we have coected
            write("coected".getBytes());
        }

        public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes retued from read()

            // Keep listening to the InputStream until an exception occurs
            while (!this.isInterrupted()) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /* Call this from the main activity to shutdown the coection */
        public void cancel() {
            if (mmInStream != null) {
                try {mmInStream.close();} catch (Exception e) {}
                mmInStream = null;
            }

            if (mmOutStream != null) {
                try {mmOutStream.close();} catch (Exception e) {}
                mmOutStream = null;
            }

            if (mmSocket != null) {
                try {mmSocket.close();} catch (Exception e) {}
                mmSocket = null;
            }

            this.interrupt();
        }
    }

I thought I was doing this correctly by first closing my input and output streams, then closing the socket, and then closing the thread. I got this from an answer here: Discoect a bluetooth socket in Android

However, as soon as logout is pressed, I get the ioexception as its still trying to read from the input stream. I have also tried placing my input/output/socket close code in a method outside of the CoectedThread class but that results in the same issue

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 4 تير 1395 ساعت: 7:47

صفحه بندی