Exemple #1
0
        /// <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);
        }
Exemple #2
0
 private void Stop()
 {
     // Stop discovering.
     FWatcher.Stop();
     // Close Bluetooth Manager.
     FManager.Close();
 }
Exemple #3
0
        /// <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)
            {
                if (FManager.Count == 0)
                {
                    Res = wclBluetoothErrors.WCL_E_BLUETOOTH_API_NOT_FOUND;
                }
                else
                {
                    // Look for first available radio.
                    for (int i = 0; i < FManager.Count; i++)
                    {
                        if (FManager[i].Available)
                        {
                            FRadio = FManager[i];
                            break;
                        }
                    }

                    if (FRadio == null)
                    {
                        Res = wclBluetoothErrors.WCL_E_BLUETOOTH_RADIO_UNAVAILABLE;
                    }
                    else
                    {
                        // 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);
        }
Exemple #4
0
        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;
                }
            }
        }
Exemple #5
0
        private void FClient_OnConnect(object Sender, int Error)
        {
            if (Error != wclErrors.WCL_E_SUCCESS)
            {
                lbLog.Items.Add("Connect failed: 0x" + Error.ToString("X8"));
                FManager.Close();
            }
            else
            {
                wclGattService[] Services;
                Int32            Res = FClient.ReadServices(wclGattOperationFlag.goReadFromCache, out Services);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    lbLog.Items.Add("Read services failed: 0x" + Res.ToString("X8"));
                }
                else
                {
                    foreach (wclGattService s in Services)
                    {
                        lbLog.Items.Add(s.ToString());
                    }

                    FClient.Disconnect();
                }
                FManager.Close();
            }
        }
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _client.Disconnect();
            _client = null;

            _manager.Close();
            _manager = null;

            Cleanup();
        }
Exemple #7
0
 private void FHub_OnDisconnected(Object Sender, Int32 Reason)
 {
     EnableConnect(false);
     FManager.Close();
 }
Exemple #8
0
 public void DisconnectBLE()
 {
     Manager.Close();
     FEvent.Close();
     FEvent = null;
 }
Exemple #9
0
        private void BtStart_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;

                // Check that at least one Bluetooth Radio exists (or at least Bluetooth drivers installed).
                if (FManager.Count == 0)
                {
                    // No one, even drivers?
                    MessageBox.Show("No Bluetooth Hardware installed");
                }
                else
                {
                    // Ok, at least one Bluetooth Radio module should be available.
                    for (Int32 i = 0; i < FManager.Count; i++)
                    {
                        // Check if current Radio module is available (plugged in and turned ON).
                        if (FManager[i].Available)
                        {
                            // Looks like we have Bluetooth on this PC!
                            Radio = FManager[i];
                            // Terminate the loop.
                            break;
                        }
                    }

                    // Check that we found the Bluetooth Radio module.
                    if (Radio == null)
                    {
                        // 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"));
                            // Also clean up found Radio variable so we can check it later.
                            Radio = null;
                        }
                    }
                }

                // Again, check the found Radio.
                if (Radio == null)
                {
                    // And if it is null (not found or discovering was not started
                    // close the Bluetooth Manager to release all the allocated resources.
                    FManager.Close();
                }
            }
        }
 private void CloseBluetoothManager()
 {
     Trace("Closing Bluetooth Manager");
     FManager.Close();
 }