3

i'm using SPP profile for connect to my device:

    Set<BluetoothDevice> devices = ba.getBondedDevices();
    for(BluetoothDevice bd : devices)
    {
        String name = bd.getName();
        if(name.equals("CELLMETER"))
        {
            try
            {
                BluetoothSocket bs = bd.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                bs.connect();
            } catch (IOException e)
            {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

All seems okay, i created function where i'm closing input output buffers and close socket. But when application crashes or i'm stopping application when breakpoints arrives socket doesn't closes, even after i kill process manually and it's not avalible for new connection from new instance of app.

What i'm doing wrong? For each crash/debug operation i have to reboot phone :(

It's manifested only to Android 2.3.5 (Samsung 5830i) and on Android 4.0.4 (Freelander P10). On my Android 4.2.1 (Galaxy Nexus) all okay, after app crash connection closes automatically. (it seems because there is new Bluetooth stack)

Evgeny E
  • 145
  • 2
  • 11
  • You should close the socket manually, are you doing this? – Egor Jan 11 '13 at 15:05
  • Yep! I'm doing this in normal case when app shutdown, but what i have to do if application crashes/closed by debugger? – Evgeny E Jan 11 '13 at 16:22
  • Post the stacktrace of the exception thrown and specify the places in code where you close the socket. – Egor Jan 11 '13 at 16:31
  • Egor, you are not understand me. Now i haven't exceptions, and they are not apply bluetooth! But if i stop application unexpectedly bluetooth socket will not be closed! How to handle this case? – Evgeny E Jan 11 '13 at 16:37
  • The socket must be closed in the Activity's onStop() method. – Egor Jan 12 '13 at 17:25
  • onStop() is not calling if activity exited unexpectedly :( – Evgeny E Jan 12 '13 at 20:11
  • What do you mean by unexpected exit? – Egor Jan 12 '13 at 22:12
  • 1
    Wow, here it is spelled out: Android kills the app to reclaim memory. A user runs "kill -9 " on the app pid, an unexpected crash occurs within the app, outside the scope of bluetooth... When that happens, any opened bluetooth socket are not closed, and there is no way to handle that within the code as the app exits without anything called. Sort of though ;) – 3c71 Jan 21 '13 at 09:21
  • Did you find a solution to this? – Questioner Jul 28 '17 at 12:04

2 Answers2

2

I can see 2 options to work that out: 1- Add an UncaughtExceptionHandler in your app, best in Application-derived class:

        mUEHandler = new Thread.UncaughtExceptionHandler()
        {
            @Override
            public void uncaughtException(Thread t, Throwable e)
            {
                // Close any opened sockets here

                defaultUEH.uncaughtException(t, e);
            }
        };

        Thread.setDefaultUncaughtExceptionHandler(mUEHandler);

But that only takes care of app crashes. If user kills the app, won't get in there at all.

2- Store some socket identification that allow you to close it when app restarts.

It's not perfect, but that could work-around your issue.

3c71
  • 4,313
  • 31
  • 43
  • >> 2- Store some socket identification that allow you to close it when app restarts. << Is it possible? Socket returns createRfcommSocketToServiceRecord function D: – Evgeny E Jan 23 '13 at 14:12
2

I solved this problem by letting my BluetoothSockets be managed by a Service running in its own process. I open, close, read, and write the sockets by passing Messages to and from the Service. If the app crashes, the Service shuts down cleanly, closing the sockets. (It does not shut down cleanly if it's running in the same process as the app.)

Brodo Fraggins
  • 628
  • 5
  • 14
  • 1
    Hm, can you show me how implement or where i can read about services and communications between app and service? – Evgeny E Aug 08 '13 at 19:35