Android 为什么蓝牙套接字的文件描述符为空?

Android 为什么蓝牙套接字的文件描述符为空?,android,sockets,bluetooth,Android,Sockets,Bluetooth,有人能帮我弄清楚为什么我只能在通过BluetoothServerSocket.accept()打开的蓝牙套接字中获取空文件描述符吗 我的目标是通过蓝牙在两台设备之间传输视频,将视频写入一侧的文件描述符,然后从另一侧的文件描述符读取。我的蓝牙连接很好,我可以来回发送原始数据,但我只能在客户端获取文件描述符。在服务器端,使用相同的代码,我只能得到一个空文件描述符。在调试器中,我可以在服务器端的mySocket.mSocketIS看到一个文件描述符。这个$0.fd,但我不知道如何访问它。有人能帮忙吗?

有人能帮我弄清楚为什么我只能在通过BluetoothServerSocket.accept()打开的蓝牙套接字中获取空文件描述符吗

我的目标是通过蓝牙在两台设备之间传输视频,将视频写入一侧的文件描述符,然后从另一侧的文件描述符读取。我的蓝牙连接很好,我可以来回发送原始数据,但我只能在客户端获取文件描述符。在服务器端,使用相同的代码,我只能得到一个空文件描述符。在调试器中,我可以在服务器端的mySocket.mSocketIS看到一个文件描述符。这个$0.fd,但我不知道如何访问它。有人能帮忙吗?这是安卓4.4.2,以下是我的代码:

首先是断开的代码(服务器端):


关于这个问题,我找到了一个解决方案,我们也使用蓝牙作为服务器。我在BluetoothSocket中的LocalSocket字段中找到了文件描述符。我的目标是获取文件并关闭它

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());
            }
        }

谢谢你的信息!当我重新开始这个项目时,我会试试这个。
    // Connect to a remote device as the client (we are the client)
class ConnectThread extends Thread
{
    // ctor
    // remoteUUID - The UUID of the remote device that we want to connect to
    public ConnectThread(BluetoothDevice btDevice, UUID remoteUUID)
    {
        // Get a BT socket to connect with the given BluetoothDevice
        try
        {
            // MY_UUID is the app's UUID string, also used by the server code
            btClientSocket = btDevice.createRfcommSocketToServiceRecord(remoteUUID);
        }catch(Exception e){ postUIMessage("ConnectThread exception: " + e.toString());    }
    } // ConnectThread ctor

    public void run()
    {
        // Cancel discovery because it will slow down the connection
        BA.cancelDiscovery();
        try
        {
            // Connect the device through the socket. This will block until it succeeds or throws an exception.  To get out, call cancel() below, which will cause .connect() to throw an exception.
            btClientSocket.connect();

            Field pfdField = btClientSocket.getClass().getDeclaredField("mPfd");
            pfdField.setAccessible(true);
            ParcelFileDescriptor pfd = (ParcelFileDescriptor) pfdField.get(btClientSocket);
            FileDescriptor myFd = pfd.getFileDescriptor();  // Pass this to Recorder.setOutputFile();

            // Yay myFd is good!  
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());
            }
        }