예제 #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);
        }
        static public void SearchDevice()
        {
            if (Manager == null)
            {
                Manager = new wclBluetoothManager();

                //Manager.OnNumericComparison += new wclBluetoothNumericComparisonEvent(Manager_OnNumericComparison);
                //Manager.OnPasskeyNotification += new wclBluetoothPasskeyNotificationEvent(Manager_OnPasskeyNotification);
                //Manager.OnPasskeyRequest += new wclBluetoothPasskeyRequestEvent(Manager_OnPasskeyRequest);
                //Manager.OnPinRequest += new wclBluetoothPinRequestEvent(Manager_OnPinRequest);
                Manager.OnDeviceFound          += new wclBluetoothDeviceEvent(Manager_OnDeviceFound);
                Manager.OnDiscoveringCompleted += new wclBluetoothResultEvent(Manager_OnDiscoveringCompleted);
                //Manager.OnDiscoveringStarted += new wclBluetoothEvent(Manager_OnDiscoveringStarted);

                Manager.Open();

                Radio = GetRadio();
            }

            if (Radio != null)
            {
                Int32 Res = Radio.Discover(3, wclBluetoothDiscoverKind.dkBle);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    CSLibrary.Debug.WriteLine("Error starting discovering: 0x" + Res.ToString("X8"));
                }
            }
        }
예제 #3
0
        private void btStart_Click(object sender, EventArgs e)
        {
            lbLog.Items.Clear();
            lbLog.Items.Add("Open Bluetooth Manager");
            Int32 Res = FManager.Open();

            if (Res != wclErrors.WCL_E_SUCCESS)
            {
                lbLog.Items.Add("Bluetooth Manager open failed: 0x" + Res.ToString("X8"));
            }
        }
예제 #4
0
파일: wclWeDoRobot.cs 프로젝트: leuher/WeDo
        /// <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);
        }
예제 #5
0
 public string ConnectBLE(string portName)
 {
     Manager = new wclBluetoothManager();
     Manager.OnDeviceFound          += new wclBluetoothDeviceEvent(Manager_OnDeviceFound);
     Manager.OnDiscoveringCompleted += new wclBluetoothResultEvent(Manager_OnDiscoveringCompleted);
     Manager.OnDiscoveringStarted   += new wclBluetoothEvent(Manager_OnDiscoveringStarted);
     Client               = new wclGattClient();
     Client.OnConnect    += Client_OnConnect;
     Client.OnDisconnect += Client_OnDisconnect;
     Manager.Open();
     Radio = GetRadio();
     return(Radio != null ? portName : null);
 }
예제 #6
0
파일: main.cs 프로젝트: btframework/WeDo
        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;
                }
            }
        }
예제 #7
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            _manager = new wclBluetoothManager();
            _client  = new wclGattClient();

            _manager.OnDeviceFound          += Manager_OnDeviceFound;
            _manager.OnDiscoveringCompleted += Manager_OnDiscoveringCompleted;
            _manager.OnDiscoveringStarted   += Manager_OnDiscoveringStarted;

            _client.OnCharacteristicChanged += Client_OnCharacteristicChanged;
            _client.OnConnect    += Client_OnConnect;
            _client.OnDisconnect += Client_OnDisconnect;

            // In real application you should always analize the result code.
            // In this demo we assume that all is always OK.
            _manager.Open();

            Cleanup();
        }
예제 #8
0
        /**
         * <summary>When the Simple_Form loads</summary>
         * <param name="sender">Object on which the change happened</param>
         * <param name="e">Additional information about the event</param>
         */
        private void Simple_Form_Load(object sender, EventArgs e)
        {
            lineTracker = new LineTracker(this);
            Manager     = new wclBluetoothManager();
            Watcher     = new wclWeDoWatcher();
            listBox1.Items.Add("Please connect one of the hubs.");

            Watcher.OnHubFound += Watcher_OnHubFound;

            int res = Manager.Open();

            if (res != wclErrors.WCL_E_SUCCESS)
            {
                MessageBox.Show("Error opening the Manager.");
            }
            else
            {
                wclBluetoothRadio radio = null;
                for (int i = 0; i < Manager.Count; i++)
                {
                    if (Manager[i].Available)
                    {
                        radio = Manager[i];
                        break;
                    }
                }
                if (radio != null)
                {
                    res = Watcher.Start(radio);
                    if (res != wclErrors.WCL_E_SUCCESS)
                    {
                        MessageBox.Show("Can't start watching.");
                    }
                }
            }
        }
예제 #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();
                }
            }
        }