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

本文关键字:连接 | 更新日期: 2023-09-27 18:32:12

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

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 次 + 才能使播放器 1Connection 工作。请帮忙。谢谢。

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

试试这个

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

}