简体   繁体   中英

What this C# code mean? can anyone decribe it for me?

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.

The Wiimote, when authenticated, does turn off after a while, if not getting a response in time. If the Windows Device Driver is not installed before this time the connection fails.

To prevent this from happening, you can enable the HID service in the Wiimote. The Wiimote will then no longer disconnect, but keeps waiting until Windows is done installing the driver and you can exchange data with the Wiimote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM