0
我試圖修改我的最終項目的Wiimote白板應用程序。但我在編程語言C#中的經驗非常少。事實上,我只用了幾個月就學習了C#。這個C#代碼是什麼意思?任何人都可以爲我描述它嗎?
我不知道下面的代碼行是什麼意思。誰能幫我? 我已經寫了一些評論,我不明白在這段代碼中的部分。 請指導我理解它。 我想這個代碼來連接Wiimote設備。
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);
}
我'對不起,我的英語很星期。
你已經提供了很多代碼 - 哪些*特定*部分你不明白? –
@Jon Skeet上面有評論的部分,評論是我的問題。你能幫我嗎? – user3332360
這是很多獨立的部分,其中很多不是我們真正負責的 - 「這段代碼做了什麼」和「爲什麼在這裏寫這段代碼」有區別。我懷疑你可能比你咀嚼的東西咬得更厲害了,而且在查看這樣的代碼之前,你最好集中精力學習更多C#的基礎知識。 –