0

I have an android application that connects with a Bluetooth device receives some bytes and disconnect with the device once all the data is received . The application runs well on more advanced phones like the galaxy s3 and galaxy note 2 but when running the application on older models (like the s2) the application freezes when i close the inputstream (i.e) in.close(). .The app freezes and it does not continue after that . I have attached my code here and marked the area where the app freezes. has anyone faced this problem before .

NOTE: The disconnect occurs inside a fragment and not a activity. Not sure if that might be a problem.


public void disconnectBTSocket() {
    try {
        BluetoothSocket btSocket= bluetooth.getBtSocket();
        InputStream in = btSocket.getInputStream();
        OutputStream out = btSocket.getOutputStream();
        Log.d("disconnect",
                "started : "
                        );
        Log.d("tag", "closing input stream");
        if (in != null) {

            in.close();
            //Gets Stuck here

        }
        Log.d("tag", "closed ");
        if (out != null) {
            out.close();
        }

        if (btSocket != null) {
            btSocket.close();
                    }
        Log.d("tag","finished disconnect");

    }

    catch (Exception e) {
        Log.d(tag,"Exception in disconnect with bluetooth socket communication : "
                        + e);
    }
    Log.d("disconnecct",
            "finished: "
                    );
}

my logcot has this and then it freezes .

 04-02 11:23:01.655: D/Metabolism(4900): closing inputstream
04-02 11:23:01.655: D/BluetoothSocket(4900): close(): android.bluetooth.BluetoothSocket@4078a8a0
04-02 11:23:01.655: D/BLZ20_ASOCKWRP(4900): asocket_abort [89,90,91]
04-02 11:23:01.655: I/BLZ20_WRAPPER(4900): blz20_wrp_shutdown: s 89, how 2
04-02 11:23:01.655: D/BLZ20_WRAPPER(4900): blz20_wrp_shutdown:  fd (-1:89), bta -1, rc 1, wflags     0x900, cflags 0x0, port 0
04-02 11:23:01.655: I/BLZ20_WRAPPER(4900): blz20_wrp_shutdown: shutdown socket
04-02 11:23:01.655: D/BLZ20_WRAPPER(4900): blz20_wrp_write: wrote 1 bytes out of 1 on fd 91

1 Answers1

0

I think i solved it . In the new device the inputsocket and outputsocket were null but in old ones they weren't . Not sure why but manually making it null worked .