android bluetooth connection fails after 489 successful connections

后端 未结 4 1026
一整个雨季
一整个雨季 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:17

    If you are using bluetooth as a server in you app, I came across with the same problem as this, except my issue is I am getting null on a file descriptor mPfd when I do reflection. I was able to close the file using this method, hope this can help.

    I get the file through a id/index from LoaclSocket in BluetoothSocket using ParceFileDescriptor. int mfd = 0; Field socketField = null; LocalSocket mSocket = null;

        try
        {
            socketField = btSocket.getClass().getDeclaredField("mSocket");
            socketField.setAccessible(true);
    
            mSocket = (LocalSocket)socketField.get(btSocket);
        }
        catch(Exception e)
        {
            Log ( "Exception getting mSocket in cleanCloseFix(): " + e.toString());
        }
    
        if(mSocket != null)
        {
            FileDescriptor fileDescriptor =
                    mSocket.getFileDescriptor();
    
            String in = fileDescriptor.toString();
    
            //regular expression to get filedescriptor index id
            Pattern p = Pattern.compile("\\[(.*?)\\]");
            Matcher m = p.matcher(in);
    
            while(m.find()) {
                Log ( "File Descriptor " + m.group(1));
                mfd = Integer.parseInt(m.group(1));
                break;
            }
    
            //Shutdown the socket properly
            mSocket.shutdownInput();
            mSocket.shutdownOutput();
            mSocket.close();
    
            mSocket = null;
    
            try { socketField.set(btSocket, mSocket); }
            catch(Exception e)
            {
                Log ("Exception setting mSocket = null in cleanCloseFix(): " + e.toString());
            }
    
            //Close the file descriptor when we have it from the Local Socket
            try {
                ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.adoptFd(mfd);
                if (parcelFileDescriptor != null) {
                    parcelFileDescriptor.close();
                    Log ( "File descriptor close succeed : FD = " + mfd);
                }
            } catch (Exception ex) {
                Log ( "File descriptor close exception " + ex.getMessage());
            }
        }
    

提交回复
热议问题