android bluetooth connection fails after 489 successful connections

后端 未结 4 1033
一整个雨季
一整个雨季 2021-01-02 07:54

unfortunately, I have some problems with android\'s bluetooth. For my test environment I use a Nexus 4 with Android 4.4.2.

I have a Java Application on my PC, which

4条回答
  •  长情又很酷
    2021-01-02 08:38

    My fix for the question i have asked:

    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, "ParcelFileDescriptor could not be cleanly closed.");
            }
        }
    

    So above is the method i have written to close the filedescriptor, and the below when i have used this code: Whenever, socket has to be close:

    private synchronized void cleanClose() {
            if (socket != null) {
                try {
                    clearFileDescriptor();
                    //clearLocalSocket();
                    socket.close();         
                }
                catch (IOException e) {
                    Log.w(SensorTicker.TAG, "Socket could not be cleanly closed.");
                }
            }
        }
    

    I have also tried with the clearLocalSocket() method i have written, but no used for my problem. So i tried to close the FileDescriptor. Hope it will help you and others facing the same issue.

提交回复
热议问题