private void Connect_Click(object sender, EventArgs e) { if (!connect()) { if (blueToothConnect()) { if (connect()) { StatusDat.Text = "Connected"; StatusDat.Update(); StatusContainer.Update(); } else { StatusDat.Text = "Connection Problem. Paired with bluetooth but did not connect. Try again."; StatusDat.Update(); StatusContainer.Update(); } } else { StatusDat.Text = "Bluetooth pairing timed out. Try to connect again, and be sure to press the sync button on Balance Board"; StatusDat.Update(); StatusContainer.Update(); } } else { StatusDat.Text = "Connected"; StatusDat.Update(); StatusContainer.Update(); } }
private void Disconnect_Click(object sender, EventArgs e) { wiiDevice.Disconnect(); Connect.Enabled = true; Start30.Enabled = false; Start60.Enabled = false; Disconnect.Enabled = false; StatusDat.Text = "Not Connected"; StatusDat.Refresh(); StatusContainer.Update(); }
private bool connect() { try { // Find all connected Wii devices. StatusDat.Text = "Looking for Balance Board"; StatusDat.Refresh(); StatusContainer.Update(); var deviceCollection = new WiimoteCollection(); deviceCollection.FindAllWiimotes(); for (int i = 0; i < deviceCollection.Count; i++) { wiiDevice = deviceCollection[i]; // Device type can only be found after connection, so prompt for multiple devices. if (deviceCollection.Count > 1) { var devicePathId = new Regex("e_pid&.*?&(.*?)&").Match(wiiDevice.HIDDevicePath).Groups[1].Value.ToUpper(); //var response = MessageBox.Show("Connect to HID " + devicePathId + " device " + (i + 1) + " of " + deviceCollection.Count + " ?", "Multiple Wii Devices Found", MessageBoxButtons.YesNoCancel); //if (response == DialogResult.Cancel) return; //if (response == DialogResult.No) continue; } // Setup update handlers. // wiiDevice.WiimoteChanged += wiiDevice_WiimoteChanged; // wiiDevice.WiimoteExtensionChanged += wiiDevice_WiimoteExtensionChanged; // Connect and send a request to verify it worked. StatusDat.Text = "Found Device, connecting ..."; StatusDat.Refresh(); StatusContainer.Update(); wiiDevice.Connect(); wiiDevice.SetReportType(InputReport.IRAccel, false); //ALSE = DEVICE ONLY SENDS UPDATES WHEN VALUES CHANGE! wiiDevice.SetLEDs(true, false, false, false); // Enable processing of updates. infoUpdateTimer.Enabled = true; // Prevent connect being pressed more than once. // button_Connect.Enabled = false; // button_BluetoothAddDevice.Enabled = false; break; } } catch (Exception ex) { StatusDat.Text = ex.Message; StatusDat.Refresh(); StatusContainer.Update(); return(false); // MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } Connect.Enabled = false; Start30.Enabled = true; Start60.Enabled = true; Disconnect.Enabled = true; return(true); }
private bool blueToothConnect() { bool success = false; try { using (var btClient = new BluetoothClient()) { // PROBLEM: // false false true: finds only unknown devices, which excludes existing but broken device entries. // false true true: finds broken entries, but even if powered off, so pairing attempts then crash. // WORK-AROUND: // Remove existing entries first, then find powered on entries. var btIgnored = 0; // Find remembered bluetooth devices. //label_Status.Text = "Removing existing bluetooth devices..."; //label_Status.Refresh(); // Remove existing connections var btExistingList = btClient.DiscoverDevices(255, false, true, false); foreach (var btItem in btExistingList) { if (!btItem.DeviceName.Contains("Nintendo")) { continue; } BluetoothSecurity.RemoveDevice(btItem.DeviceAddress); btItem.SetServiceState(BluetoothService.HumanInterfaceDevice, false); } // Find unknown bluetooth devices. StatusDat.Text = "Press the SYNC button (inside battery case of Balance Board)"; StatusDat.Refresh(); var btDiscoveredList = btClient.DiscoverDevices(255, false, false, true); foreach (var btItem in btDiscoveredList) { // Just in-case any non Wii devices are waiting to be paired. if (!btItem.DeviceName.Contains("Nintendo")) { btIgnored += 1; continue; } StatusDat.Text = "Adding: " + btItem.DeviceName + " ( " + btItem.DeviceAddress + " )"; StatusDat.Refresh(); StatusContainer.Update(); // Send special pin for permanent sync. // Install as a HID device and allow some time for it to finish. success = true; btItem.SetServiceState(BluetoothService.HumanInterfaceDevice, true); } // Allow slow computers to finish installation before connecting. if (success) { System.Threading.Thread.Sleep(4000); } // Connect and send a command, otherwise they sleep and the device disappears. //try //{ // if (btDiscoveredList.Length > btIgnored) // { // var deviceCollection = new WiimoteCollection(); // deviceCollection.FindAllWiimotes(); // foreach (var wiiDevice in deviceCollection) // { // wiiDevice.Connect(); // wiiDevice.SetLEDs(true, false, false, false); // wiiDevice.Disconnect(); // } // } //} //catch (Exception) { } // Status report. //label_Status.Text = "Finished - You can now close this window. Found: " + btDiscoveredList.Length + " Ignored: " + btIgnored; //label_Status.Refresh(); } } catch (Exception ex) { //label_Status.Text = "Error: " + ex.Message; } return(success); }