/// <summary> Starts connection to the WeDo Hubs. </summary> /// <returns> If the method completed with success the returning value is /// <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is /// one of the Bluetooth Framework error code. </returns> /// <remarks> The method starts searching for WeDo Hubs and to connect to each found. Once the Hub found /// the <c>OnHubFound</c> event fires. An application may accept connection to this Hub by setting /// the <c>Connect</c> parameter to <c>true</c>. </remarks> public Int32 Start() { if (FRadio != null) { return(wclConnectionErrors.WCL_E_CONNECTION_ACTIVE); } Int32 Res = FManager.Open(); if (Res == wclErrors.WCL_E_SUCCESS) { Res = FManager.GetLeRadio(out FRadio); if (Res == wclErrors.WCL_E_SUCCESS) { // Try to start watching for HUBs. Res = FWatcher.Start(FRadio); // If something went wrong we must clear the working radio objecy. if (Res != wclErrors.WCL_E_SUCCESS) { FRadio = null; } } // If something went wrong we must close Bluetooth Manager if (Res != wclErrors.WCL_E_SUCCESS) { FManager.Close(); } } return(Res); }
private void BtConnect_Click(Object Sender, EventArgs e) { // The very first thing we have to do is to open Bluetooth Manager. // That initializes the underlying drivers and allows us to work with Bluetooth. // Always check result! Int32 Res = FManager.Open(); if (Res != wclErrors.WCL_E_SUCCESS) { // It should never happen but if it does notify user. MessageBox.Show("Unable to open Bluetooth Manager: 0x" + Res.ToString("X8")); } else { // Assume that no one Bluetooth Radio available. wclBluetoothRadio Radio = null; Res = FManager.GetLeRadio(out Radio); if (Res != wclErrors.WCL_E_SUCCESS) { // If not, let user know that he has no Bluetooth. MessageBox.Show("No available Bluetooth Radio found"); } else { // If found, try to start discovering. Res = FWatcher.Start(Radio); if (Res != wclErrors.WCL_E_SUCCESS) { // It is something wrong with discovering starting. Notify user about the error. MessageBox.Show("Unable to start discovering: 0x" + Res.ToString("X8")); } else { btConnect.Enabled = false; btDisconnect.Enabled = true; laStatus.Text = "Searching..."; } } // Again, check the found Radio. if (Res != wclErrors.WCL_E_SUCCESS) { // And if it is null (not found or discovering was not started // close the Bluetooth Manager to release all the allocated resources. FManager.Close(); // Also clean up found Radio variable so we can check it later. Radio = null; } } }