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.

user3332360
  • 105
  • 8
  • 1
    You've provided a *lot* of code - which *specific* part of it do you not understand? – Jon Skeet Feb 20 '14 at 11:19
  • @Jon Skeet the part that have a comment above it, the comment is my question. can you help me please? – user3332360 Feb 20 '14 at 11:22
  • That's a lot of separate bits, many of which aren't really answerable by us - there's a difference between "what does this code do" and "why is this code here". I suspect that you may have bitten off more than you can chew, and it would be better for you to concentrate on learning more of the fundamentals of C# before looking at code like this. – Jon Skeet Feb 20 '14 at 11:32
  • i think so, but i kinda stuck in this situation. That why i call for help. I think I need to discuss it again with my lecturer. Thank you for the suggestion. – user3332360 Feb 20 '14 at 12:32

1 Answers1

0

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.

user3F31A28
  • 104
  • 2
  • 13