Connecting to Bluetooth device fails in deep sleep

送分小仙女□ 提交于 2019-12-05 13:09:25

I have found the workaround after going through the Bluetooth library classes, and found it doesn't close the FileDescriptor while calling bluetoothsocket.close(); despite it calls the dispatch() method over the FileDescriptor, which doesn't close it.

Just call this method before the bluetoothsocket.close();

private synchronized void clearFileDescriptor(){
        try{
            Field field = BluetoothSocket.class.getDeclaredField("mPfd");
            field.setAccessible(true);
            ParcelFileDescriptor mPfd = (ParcelFileDescriptor)field.get(socket);
            if(null == mPfd){
                return;
            }

            mPfd.close();
        }catch(Exception e){
            Log.w(SensorTicker.TAG, "LocalSocket could not be cleanly closed.");
        }
    }

Hope this will resolve others problem as well. But it's should be handled by the BluetoothSocket class over the close method.

Wookie

This looks like the BluetoothSocket.close() bug in Android 4.2 - 4.4.4, which seems to be fixed in 5.0. Basically, it's not releasing the underlying file descriptor, so it leaks until you hit your device's limit, then weird bad things happen. You need to get the ParcelFileDescriptor from the BluetoothSocket and close it yourself.

Check out my answer here for the workaround code: https://stackoverflow.com/a/27873675/1893015

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