gpt4 book ai didi

c# - 如何使用蓝牙套接字从游戏 Controller 接收按钮事件?

转载 作者:行者123 更新时间:2023-11-30 01:26:42 24 4
gpt4 key购买 nike

目前我与游戏 Controller 的蓝牙连接正常。现在我正试图捕捉按钮事件,但这并不容易。我有以下代码:

    private Stream mmInStream;
private Stream mmOutStream;

public BluetoothController()
{

}

public void Read(BluetoothSocket socket)
{
Stream tmpIn = null;
Stream tmpOut = null;

try
{
tmpIn = socket.InputStream;
tmpOut = socket.OutputStream;
}
catch (IOException ex)
{
throw ex;
}

mmInStream = tmpIn;
mmOutStream = tmpOut;

Thread thread = new Thread(new ThreadStart(Run));
thread.Start();
}

protected void Run()
{
byte[] buffer = new byte[1024];
int bytes;

while (true)
{
try
{
// Thanks to @spaceplane
if (mmInStream.IsDataAvailable())
{
// Read from the InputStream
bytes = mmInStream.Read(buffer, 0, buffer.Length);
}
}
catch (Exception ex)
{
throw ex;
}
}
}

当我按下游戏 Controller 上的按钮时,它不会进入下一行:

if (mmInStream.IsDataAvailable())

这里有什么问题吗?

最佳答案

mmInStream.read() 如果流中当前没有数据可供读取,则阻塞。因此,如果没有数据,它就会卡在那里等待。在从流中读取之前,您应该检查是否有任何可用数据。变化

bytes = mmInStream.Read(buffer, 0, buffer.Length);

if(mmInStream.available() > 0) {
bytes = mmInStream.Read(buffer, 0, buffer.Length);
}

关于c# - 如何使用蓝牙套接字从游戏 Controller 接收按钮事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36339631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com