Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# socket.isConnected返回false_C#_Android_Windows_Bluetooth - Fatal编程技术网

C# socket.isConnected返回false

C# socket.isConnected返回false,c#,android,windows,bluetooth,C#,Android,Windows,Bluetooth,我正在写一个pc和电话通讯应用程序。pc代码是用C#编写的。当我将手机(与pc配对)连接到pc时,手机会连接,但日志显示,插座未连接I-e;socket.isConnected()在我的手机上返回false。我尝试在多部手机上运行此应用程序,但问题仍然存在 我认为问题在于我的C代码,因为我对C不太了解 以下是c代码: BluetoothListener blueListener = new BluetoothListener(mUUID); blueListene

我正在写一个pc和电话通讯应用程序。pc代码是用C#编写的。当我将手机(与pc配对)连接到pc时,手机会连接,但日志显示,插座未连接I-e;socket.isConnected()在我的手机上返回false。我尝试在多部手机上运行此应用程序,但问题仍然存在

我认为问题在于我的C代码,因为我对C不太了解

以下是c代码:

        BluetoothListener blueListener = new BluetoothListener(mUUID);
        blueListener.Start();


        BluetoothClient conn = blueListener.AcceptBluetoothClient();

        if(conn.Connected)
            updateUI("Mobile connected");


        Stream mStream = conn.GetStream();
        while (true)
        {
            try
            {
                byte[] received = new byte[1024];
                mStream.Read(received, 0, received.Length);
                //.....
            }
            catch() {}
        }
BluetoothSocket socket = selectedDevice.createRfcommSocketToServiceRecord(deviceUUID);
try
    {
        socket = openSocketForSendingData();

        try
        {
            BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();

            bAdapter.cancelDiscovery();
            socket.connect();

            Log.e("NP socket", "No Exception");
        }
        catch(Exception e)
        {
            Log.e("NP socket Exception", e.toString());
        }


        if(socket == null)
            Log.e("Socket NULL", "socket is null");


        if(!socket.isConnected())
        {
            Log.e("NP socket", "socket not cconnected");

            try
            {
                socket.close();
            }
            catch(Exception e)
            {
                Log.e("Socket CLOSE",e.toString());
            }

        }
        else
            Log.e("NP socket", "socket  cconnected");   

        if(socket.getOutputStream() == null)
            Log.e("Soccket Outupt", "output stream null");  

        dos = new DataOutputStream(socket.getOutputStream());

        dos.writeChars("Hello World I am android");

    }
    catch(Exception e)
    {
        Log.e("NP writing socket", e.toString());   
    }
我正在使用C#中的32英尺库

以下是我的Android代码:

        BluetoothListener blueListener = new BluetoothListener(mUUID);
        blueListener.Start();


        BluetoothClient conn = blueListener.AcceptBluetoothClient();

        if(conn.Connected)
            updateUI("Mobile connected");


        Stream mStream = conn.GetStream();
        while (true)
        {
            try
            {
                byte[] received = new byte[1024];
                mStream.Read(received, 0, received.Length);
                //.....
            }
            catch() {}
        }
BluetoothSocket socket = selectedDevice.createRfcommSocketToServiceRecord(deviceUUID);
try
    {
        socket = openSocketForSendingData();

        try
        {
            BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();

            bAdapter.cancelDiscovery();
            socket.connect();

            Log.e("NP socket", "No Exception");
        }
        catch(Exception e)
        {
            Log.e("NP socket Exception", e.toString());
        }


        if(socket == null)
            Log.e("Socket NULL", "socket is null");


        if(!socket.isConnected())
        {
            Log.e("NP socket", "socket not cconnected");

            try
            {
                socket.close();
            }
            catch(Exception e)
            {
                Log.e("Socket CLOSE",e.toString());
            }

        }
        else
            Log.e("NP socket", "socket  cconnected");   

        if(socket.getOutputStream() == null)
            Log.e("Soccket Outupt", "output stream null");  

        dos = new DataOutputStream(socket.getOutputStream());

        dos.writeChars("Hello World I am android");

    }
    catch(Exception e)
    {
        Log.e("NP writing socket", e.toString());   
    }
两个代码中的UUID相同

你能告诉我我犯了什么错误吗

附言:在问这个问题之前,我已经花了一整天的时间在互联网上搜索解决方案,但是没有用


您是否能够解决该问题?