蓝牙设备有时连接,有时不连接

时间:2014-11-26 02:57:36

标签: c# android wpf bluetooth

我一直在使用这款蓝牙。这是我的代码:

public void createServer()
{
    Thread ServerThread = new Thread(new ThreadStart(ServerConnectThread));
    ServerThread.Start();
}

public void ServerConnectThread()
{

    updateUI("Server started");
    updateUI("Waiting for client..");
    BluetoothListener BTListener = new BluetoothListener(UUID);
    BTListener.Stop();
    BTListener.Start();
    ServerRunning = true;
    BluetoothClient conn = BTListener.AcceptBluetoothClient();
    MessageBoxResult connected = System.Windows.MessageBox.Show("Phone has connected", "Confirmation", MessageBoxButton.OK);
    if (connected == MessageBoxResult.OK)
    {
        this.Dispatcher.Invoke((Action)(() =>
        {
            player1Connection.Visibility = Visibility.Visible;
            player1Connectiondc.Visibility = Visibility.Hidden;
        }));


    Stream mStream = conn.GetStream();
    while (ServerRunning)
    {
        try
        {
            byte[] received = new byte[1];
            mStream.Read(received, 0, received.Length);
            updateUI("Message Received. " + Encoding.ASCII.GetString(received) + System.Environment.NewLine);
            byte[] sent = Encoding.ASCII.GetBytes(Encoding.ASCII.GetString(received) + " u too");
            mStream.Write(sent, 0, sent.Length);
            updateUI("The code is " + Encoding.ASCII.GetString(received));
            string result = System.Text.Encoding.ASCII.GetString(received).ToString();
            updateUI("Key is " + result);

            if (Game != null)
            {

                SinglePlayGame sg = (IceGame.Class.SinglePlayGame)Game;
                force.Y = -force.Y;
                if (result == "A")
                {
                    force += new Vector2(-forceAmount, 0);
                }

                else if (result == "D")
                {
                    force += new Vector2(forceAmount, 0);
                }
                else if (result == "W")
                {
                    force += new Vector2(0, -forceAmount);
                }

                else if (result == "S")
                {
                    force += new Vector2(0, forceAmount);
                }


                sg.BodyPlayer1.ApplyForce(force);
                force = new Vector2(0, 0);
            }


        }
        catch (IOException ex)
        {
            Console.WriteLine("Client disconnected");
        }
    }
}

仍然会说手机已连接。然而,player1Connection有时也不可见,也导致设备无法读取我所说的命令。它正在工作但是我需要大约3倍+来使player1Connection工作。请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个

if (Game != null)
{

    SinglePlayGame sg = (IceGame.Class.SinglePlayGame)Game;
    force.Y = -force.Y;
    if ("A".equals(result))
    {
        force += new Vector2(-forceAmount, 0);
    }

    else if ("D"equals(result))
    {
            force += new Vector2(forceAmount, 0);
    }
    else if ("W".equals(result))
    {
            force += new Vector2(0, -forceAmount);
    }

    else if ("S".equals(result))
    {
            force += new Vector2(0, forceAmount);
    }


        sg.BodyPlayer1.ApplyForce(force);
        force = new Vector2(0, 0);
    }


}