1

I am writing an app on Android Studio. I communicate from an Android device to an arduino board via Bluetooth. For now everything works but i am starting a new Activity and i need to stop the actual BT connection. so i want to call a stop method. The problem is that it crash when i call it.

here is the code

public class BtInterface {

private BluetoothDevice device = null;
private BluetoothSocket socket = null;
private InputStream receiveStream = null;
private OutputStream sendStream = null;
String GlobalBuff="";
String Right_Buff="";


private ReceiverThread receiverThread;

Handler handler;

public BtInterface(Handler hstatus, Handler h,String Device_Name) {
    Set<BluetoothDevice> setpairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    BluetoothDevice[] pairedDevices = (BluetoothDevice[]) setpairedDevices.toArray(new BluetoothDevice[setpairedDevices.size()]);

    for(int i=0;i<pairedDevices.length;i++) {
        if(pairedDevices[i].getName().contains(Device_Name)) {
            device = pairedDevices[i];
            try {
                socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                receiveStream = socket.getInputStream();
                sendStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }

    handler = hstatus;

    receiverThread = new ReceiverThread(h);
}

public void sendData(String data) {
    sendData(data, false);
}

public void sendData(String data, boolean deleteScheduledData) {
    try {
       sendStream.write(data.getBytes());
        sendStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void connect() {
    new Thread() {
        @Override public void run() {
            try {
                socket.connect();

                Message msg = handler.obtainMessage();
                msg.arg1 = 1;
                handler.sendMessage(msg);

                receiverThread.start();

            } catch (IOException e) {
                Log.v("N", "Connection Failed : " + e.getMessage());
                e.printStackTrace();
            }
        }
    }.start();
}

public void close() {


    try {
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    socket=null; //???
}

public BluetoothDevice getDevice() {
    return device;
}

private class ReceiverThread extends Thread {
    Handler handler;

    ReceiverThread(Handler h) {
        handler = h;
    }

    @Override public void run() {
        while(true) {
            try {
                if(receiveStream.available() > 0) {
                    byte buffer[] = new byte[1000];
                    int k = receiveStream.read(buffer, 0, 1000);

                    if(k > 0) {
                        byte rawdata[] = new byte[k];
                        for(int i=0;i<k;i++)
                            rawdata[i] = buffer[i];

                        String data = new String(rawdata);

                        GlobalBuff= GlobalBuff+data;
                        Right_Buff= GlobalBuff.substring(GlobalBuff.length()-1,GlobalBuff.length());
                        if(Right_Buff.equals("\n")){
                            Message msg = handler.obtainMessage();
                            Bundle b = new Bundle();
                            b.putString("receivedData", GlobalBuff);
                            msg.setData(b);
                            handler.sendMessage(msg);
                            GlobalBuff="";

                        }

                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  }
 }

i try some extra code :

  receiverThread.interrupt();
    receiverThread=null;

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

before closing but the result is the same , it crash. The strange behavior is that it didn't crash immediately as it could happen with a type error or else ( i am talking of the behavior in debug mode...)

If anybody got an idea.

Googling this bring me to people with this issue but no solution that works for my case.

Thanks

UPDATE

what i found as a trace when it crash is that :

06-02 07:45:27.369    9025-9133/fr.icservice.sechage A/libc? Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 9133 (Thread-1436)

I also made a test on a sony Z3 phone under 5.0.2 (compare to my T210 samsung galaxy tab3 under 4.4.2)and it not crash..! maybe it's a ROM bug?! or a android version problem...

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
got_fr
  • 39
  • 8

2 Answers2

1

This is a known problem (or bug?) on Android. If you close the Bluetooth socket and then access it later on, some devices will crash with a segmentation fault (like yours). A common workaround is to check socket.isConnected() before or to synchronize the access to close(), write(), read(), ... by setting a flag like closeWasCalled = true and prevent any further calls to methods of this socket in your code after a close() call.

hgross
  • 690
  • 6
  • 15
0

The problem comes with Socket Input/Output. I faced this problem when disconnecting with peer bluetooth device.

Reason :

From code, we are trying to read() , write() from socket object/connection which is closed.

Solution :

Add checking socket.isConnected() before above operations

You can read more about this problem on Stack Overflow question : Here

Community
  • 1
  • 1
Kushal
  • 8,100
  • 9
  • 63
  • 82