Exemplo n.º 1
0
        private void RunDirectConnect(BackgroundWorker worker, DoWorkEventArgs e)
        {
            IJcwTaskResult result = new JcwTaskResult();

            result.Status   = TaskResultStatus.Failed;
            Error.LastError = null;

            try
            {
                do
                {
                    m_device = m_network.ConnectDevice(new Address(m_deviceAddress), m_deviceName);

                    if (m_device == null)
                    {
                        Thread.Sleep(m_bluetoothDirectConnectIntervalMilliseconds);
                    }
                    else
                    {
                        // before trying to discover services, enter the pin code
                        if (!m_device.Bonded)
                        {
                            m_device.Bond(m_pinCode);
                        }

                        // this will connect without regard for whether the service exists
                        m_service = m_device.ConnectService(m_serviceChannel, m_serviceName);

                        if (m_service == null)
                        {
                            Thread.Sleep(m_bluetoothDirectConnectIntervalMilliseconds);
                        }
                        else
                        {
                            try
                            {
                                m_stream = m_service.Stream;
                                break;
                            }
                            catch (Exception)
                            {
                                Thread.Sleep(m_bluetoothDirectConnectIntervalMilliseconds);
                            }
                        }
                    }
                }while (m_bluetoothStopwatch.Elapsed.Seconds < m_bluetoothDirectConnectTimeoutSeconds);

                if (m_device == null)
                {
                    throw new Exception("Could not connect to device: " + m_deviceName);
                }
                if (m_service == null)
                {
                    throw new Exception("Could not connect to service " + m_serviceName);
                }

                result.Status = TaskResultStatus.Passed;
                result.Text   = "Direct connect completed successfully";
            }
            catch (ThreadAbortException) { }
            catch (BlueToolsException ex)
            {
                Error.LastError = ex.Message + ex.StackTrace;
                result.Text     = Error.LastError;
            }
            catch (Exception ex)
            {
                Error.LastError = ex.Message + ex.StackTrace;
                result.Text     = Error.LastError;
            }
            finally
            {
                e.Result = result;
            }
        }
Exemplo n.º 2
0
        private void RunDiscover(BackgroundWorker worker, DoWorkEventArgs e)
        {
            IJcwTaskResult result = new JcwTaskResult();

            result.Status   = TaskResultStatus.Failed;
            Error.LastError = null;

            try
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                Device[] devices = m_network.DiscoverDevices();
                if (devices.Length == 0)
                {
                    throw new Exception("No devices found. Verify that " + m_deviceName + " is on and in range");
                }

                bool exactMatch = true;
                if (m_deviceName.Contains("%"))
                {
                    m_deviceName = m_deviceName.Replace("%", "");
                    exactMatch   = false;
                }

                // go through the devices we found, looking for our gcog
                bool foundOurDevice = false;
                foreach (Device d in devices)
                {
                    RemoteDevice rd = d as RemoteDevice;

                    // verify that the device name we found matches the one we were looking for
                    if ((exactMatch && rd.Name == m_deviceName) || rd.Name.Contains(m_deviceName))
                    {
                        m_device        = rd;
                        m_deviceAddress = rd.Address.ToString();
                        foundOurDevice  = true;
                        break;
                    }
                }
                if (!foundOurDevice)
                {
                    throw new Exception("Could not find device " + m_deviceName);
                }

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                // before trying to discover services, enter the pin code
                if (!m_device.Bonded)
                {
                    m_device.Bond(m_pinCode);
                }

                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                // discover all services offered by the device
                Service[] services = m_device.DiscoverServices(ServiceType.SerialPort);

                if (services.Length == 0)
                {
                    // try to discover services a second time
                    services = m_device.DiscoverServices(ServiceType.SerialPort);
                    if (services.Length == 0)
                    {
                        throw new Exception("No services found");
                    }
                }

                exactMatch = true;
                if (m_serviceName.Contains("%"))
                {
                    m_serviceName = m_serviceName.Replace("%", "");
                    exactMatch    = false;
                }

                bool foundOurService = false;
                foreach (Service s in services)
                {
                    RemoteService rs = s as RemoteService;

                    // verify that the service name we found matches the one we were looking for
                    if ((exactMatch && rs.Name == m_serviceName) || rs.Name.Contains(m_serviceName))
                    {
                        m_service        = rs;
                        m_stream         = rs.Stream;
                        m_serviceAddress = rs.Address.ToString();
                        m_serviceChannel = rs.Address.ServiceChannelNumber;
                        foundOurService  = true;
                        break;
                    }
                }

                if (!foundOurService)
                {
                    throw new Exception("Could not find service " + m_serviceName);
                }

                result.Status = TaskResultStatus.Passed;
                result.Text   = "Found bluetooth device";
            }
            catch (ThreadAbortException) { }
            catch (BlueToolsException ex)
            {
                Error.LastError = ex.Message + ex.StackTrace;
                result.Text     = Error.LastError;
            }
            catch (Exception ex)
            {
                Error.LastError = ex.Message + ex.StackTrace;
                result.Text     = Error.LastError;
            }
            finally
            {
                e.Result = result;
            }
        }