0

I tried to modify the Wiimote Whiteboard app for my final project. but my experience in the programming language C # is very little. in fact I learn C # just a few months.

I do not know what is the meaning of lines of code below. can anyone help me? I've written a few comments to the parts that I do not understand in this code. Please guided me in understanding it. I guess this code to connect the Wiimote device.

       private void Connect(bool DisconnectOld)
        {
            //TODO: honour disconnectold parameter
            BLUETOOTH_DEVICE_INFO device = new 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); }

i'am sorry that my english is very week.

4

1 回答 1

0

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

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

于 2015-01-05T11:22:10.793 回答