0

我最近在写一个wiimote程序:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WiimoteLib;

namespace WiiTester
{
    public partial class Form1 : Form
    {
        Wiimote wm = new Wiimote();
        public Form1()
        {
            InitializeComponent();


            wm.WiimoteChanged += wm_WiimoteChanged;
            wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;

            wm.Connect();
            wm.SetReportType(InputReport.IRAccel, true);
        }

        void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
        {
            WiimoteState ws = args.WiimoteState;

            if (ws.ButtonState.A == true)
            {
                wm.SetRumble(true);
            }
            else
            {
                wm.SetRumble(false);
            }
        }

        void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs args)
        {
            if (args.Inserted)
            {
                wm.SetReportType(InputReport.IRExtensionAccel, true);
            }
            else
            {
                wm.SetReportType(InputReport.IRAccel, true);
            }
        }
    }
}

我的 wiimote 不断断开连接,并且此错误在 wm.Connect() 上继续运行;等待状态报告超时

有解决办法吗?

4

1 回答 1

0

我对这个库有很多经验,你的问题很可能是因为你经常调用 SetRumble 引起的,这段代码:

    void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
    {
        WiimoteState ws = args.WiimoteState;

        if (ws.ButtonState.A == true)
        {
            wm.SetRumble(true);
        }
        else
        {
            wm.SetRumble(false);
        }
    }

无论 A 是否关闭,都会不断调用 SetRumble,请考虑使用以下代码:

    bool rumbleOn = false;

   void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
    {
      WiimoteState ws = args.WiimoteState;

      bool newRumble = (ws.ButtonState.A == true);

          if (rumbleOn != newRumble) 
      {
            rumbleOn = newRumble;
            wm.SetRumble(rumbleOn);
      }
    }

这样设置的隆隆声方法只在需要时被调用,而不是不断地向 WiiMote 发送输出报告,这会导致蓝牙总线过载。

于 2012-08-12T13:01:24.080 回答