Android Bluetooth socket freeze application

╄→гoц情女王★ 提交于 2019-12-03 13:14:16

A solution I found working (for a samsung galaxy mini) - that is quite unfriendly to the user, and not good "design" (but the broadcom firmware bug is not good "design" anyway) - but it's better than letting the user's phone freeze - is to turn OFF the bluetooth after we're done:

In both my onDestroy() and onBackPressed() - I call my cleanup() function that has something like this:

if(mBluetoothAdapter != null)
{           
mBluetoothAdapter.disable();            
}    
mBluetoothAdapter = null;

Did the socket get successfully created before calling the Close() method ? I would try initializing the socket to null before the call to createRfcommSocketToServiceRecord(UUID) The Bluetooth Chat example does this .. here is a snippet .

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);
        } catch (IOException e) {
            Log.e(TAG, "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }

Take a look at the code that calls cancel(). In particular, look at how it handles runtime exceptions thrown by cancel(). The code above will throw a NPE if cancel() is called after ConnectThread() catches an exception.

Also, check any loops where you perform an operation on socket. Once the close() is called, connect(), getInputStream(), and getOutputStream() will throw IOEs.

Did you try looking into traceview ? which method is stalling the cpu ? That may give you some insights on this problem. Even if this happens only on a particular device, you should be able to find with traceview.

I am facing the same issue but in server mode when I used BluetoothServerSocket. I was using system.exit(0) to quit the application which I read somewhere this is not recommended. I removed the system.exit(0) call and I don't have the freeze issue. (But if I kill the app then it does exhibit the freeze).

Does anybody has an issue in Server mode when sometimes the SDP record doesn't get deleted?

OK, it's an Samsung GALAXY Tab bug, so waiting Android update...

UPDATE: fixed in new firmware!

odhinn

Although I can't determine how to actually prevent this problem from occurring, I am having the same issue. After some research, there seems to be an issue with the Broadcom Bluetooth stack. To answer the question of which process is holding the cpu hostage, it is btld.

See my other post, which no one has yet answered: Btld consuming resources after application stop?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!