Bluetooth socket freeze phone
Asked Answered
T

2

9

I am developing an application for Android. This app should communicate with a Bluetooth (BT) device (sending some bytes). I have a problem with debugging/running this app on my device (Samsung Galaxy mini). When I create a BT socket and stop debugging, phone freeze and I have to restart it by getting out the battery. In case of running this app (from Eclipse) everything is OK, but when I try to run it again, phone freeze and app is not installed. If I try to unninstall this app manualy before second run, phone freeze again. Here is a problematic code:

private final BluetoothDevice mmDevice;
private UUID uuid;

public ConnectionThread(BluetoothDevice device) {
    Log.d(TAG, "create ConnectionThread");

    uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    BluetoothSocket tmp = null;
    mmDevice = device;

    try {
        tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);
    } catch (IOException e) { }
    mmSocket = tmp;
    socketConnected = true;
}

This is a constructor of thread. When I comment the line

    tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);

the phone doesn´t freeze so problem is with creating socket (not connecting). Restarting phone after each debugging or running is pretty annoying and I have to do a lot of work yet.

If I run this app from a phone (disconnected from Eclipse), it works without any problems. Any ideas where could be a problem or how to fix it? Thank you.

Titrate answered 13/3, 2013 at 8:12 Comment(9)
Sounds like a firmware bug, doesn't it?Mesosphere
@CodePainters: firmware or IDE bug. I found a same topic: #4408787. So if I turn BT off in onDestroy callback, everything is OK.Titrate
IDE? Unlikely. And Android is full of bugs anyway...Mesosphere
I'd personally try regular debugging with a rooted device - from top to strace to gdb. That's the power of open source, if you have time for that, of course.Mesosphere
Well if I run this app from phone directly, it is OK. But if I run or debug it from Eclipse, phone freeze. It is a true that only a few developers use BT API so there could be a lot of unhidden bugs...Titrate
If it only freezes when you debug it from Eclipse, could you perhaps have accidentally set a breakpoint that the app is hitting?Nichellenichol
All breakpoints are deleted... I already tried IntelliJIDEA IDE and the problem was still there so it isn´t IDE bug.Titrate
I've used the bluetooth rfcomm socket on android and i didn't had these issues. try running the BluetoothChat sample (from the sample codes) and see if it's a problem in your code.Quesenberry
Does it "work" or simply not freeze? You're eating the IOException and not reporting anything. Not that it should stop there, but still. Just wondering.Neuroblast
S
0

I am using SGSIII mini as well for development. The following code works well for me:

    private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;

        BluetoothSocket tmp = null;

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            //tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(LOG_TAG, "create() failed", e);
        }
        mmSocket = tmp;

        Main.myBluetoothSocket = mmSocket;
        Main.myBluetoothDevice = mmDevice;
    }

    @Override
    public void run() {
        Log.i(LOG_TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Send a failure message back to the Activity
        Message msg = mHandler.obtainMessage(MESSAGE_TOAST);
        Log.e(LOG_TAG, "Attempting connection to " + mmSocket.getRemoteDevice().getName());
        String ss = "Attempting connection to " + mmSocket.getRemoteDevice().getName();
        Bundle bundle = new Bundle();
        bundle.putString(TOAST, ss);
        msg.setData(bundle);
        mHandler.sendMessage(msg);

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            Log.e(LOG_TAG, "*+*+*+*   Connection Failed");
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(LOG_TAG, "unable to close() socket during connection failure", e2);
            }
            // Start the service over to restart listening mode
            BluetoothCommandService.this.start();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothCommandService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(LOG_TAG, "close() of connect socket failed", e);
        }
    }
}
Sycophant answered 6/4, 2013 at 0:23 Comment(0)
B
0

I am also facing same problem you can use Reflection method it will work

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1);
Biz answered 2/4, 2014 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.