socket.isConnected返回false

时间:2013-12-05 04:57:02

标签: c# android windows bluetooth

我正在写一台电脑&手机通讯应用。 pc代码是用C#编写的。当我将我的手机(与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() {}
        }

我在C#中使用32feet库。

以下是我的Android代码:

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相同。

你能指导我做出什么错吗?

PS:在问这个问题之前,我已经花了一整天时间在互联网上搜索解决方案,但这并不好。

此致

0 个答案:

没有答案