此 C# 代码是什么意思?谁能为我描述一下



我尝试为我的最终项目修改Wiimote白板应用程序。 但我在编程语言 C# 方面的经验很少。 事实上,我只学习了几个月的C#。

我不知道下面的代码行是什么意思。 谁能帮我?我已经写了一些注释到我在此代码中不理解的部分。请引导我理解它。我猜这个代码可以连接Wiimote设备。

      私有无效连接(bool DisconnectOld)        {            TODO:荣誉断开旧参数            BLUETOOTH_DEVICE_INFO设备 = 新 BLUETOOTH_DEVICE_INFO();            device.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_INFO));            device.szName = ";
         //whether the 9 lines of code below to create a parameter to search the bluetooth device?
        BLUETOOTH_DEVICE_SEARCH_PARAMS searchparams = new BLUETOOTH_DEVICE_SEARCH_PARAMS();
        //what the purpose of one line of code below?
        searchparams.dwSize = Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
        searchparams.fIssueInquiry = true;
        searchparams.fReturnAuthenticated = true;
        searchparams.fReturnConnected = true;
        searchparams.fReturnRemembered = true;
        searchparams.fReturnUnknown = true;
        searchparams.hRadio = IntPtr.Zero;
        searchparams.cTimeoutMultiplier = 1;

        bool connected = false;
        //what the purpose of one line of code below?
        IntPtr handle = BluetoothFindFirstDevice(ref searchparams, ref device);
        //what the meaning of what IntPtr.Zero?
        if (handle == IntPtr.Zero)
        {
            int lasterror = Marshal.GetLastWin32Error();
            if (lasterror != 0)
                LogError("Bluetooth API returned: " + lasterror.ToString());
        }
        else
        {
            while (true)
            {
                if (Cancel)
                    break;
                if (device.szName.StartsWith("Nintendo RVL"))
                {
                    //whether the function "if" below state that the device once connected?
                    if (device.fRemembered)
                    {
                        BluetoothRemoveDevice(ref device.Address);
                    }
                    else
                    {
                        //what the purpose line of code below?
                        if (BluetoothSetServiceState(IntPtr.Zero, ref device, ref HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE) != 0)
                            LogError("Failed to connect to wiimote controller");
                        else
                            connected = true;
                    }
                    break;
                }
                //why the divice.szName set to null again?
                device.szName = "";
                if (!BluetoothFindNextDevice(handle, ref device))
                    break;
            }
        }
        //what the purpose of line of code below?
        BluetoothFindDeviceClose(handle);
        if (connected && Connected != null)
            Connected(this, EventArgs.Empty);
        else if (ConnectionFailed != null)
            ConnectionFailed(this, EventArgs.Empty);
    }
 

很抱歉我的英语非常周。

Wiimote在经过身份验证后,如果没有及时得到响应,则会在一段时间后关闭。如果在此时间之前未安装 Windows 设备驱动程序,则连接将失败。

为了防止这种情况发生,您可以在Wiimote中启用HID服务。然后,Wiimote将不再断开连接,而是继续等待,直到Windows完成安装驱动程序,您可以与Wiimote交换数据。

相关内容

  • 没有找到相关文章

最新更新