All pastes #1512322 Raw Edit

Stuff

public text v1 · immutable
#1512322 ·published 2009-07-30 15:32 UTC
rendered paste body
        private void btDalej_Click(object sender, EventArgs e)
        {
            BluetoothDeviceInfo bdi = (BluetoothDeviceInfo)lbDevices.SelectedValue;
            bool has_serial_port = false;
            string serial_port = string.Empty;

            Guid[] services = bdi.InstalledServices;

            foreach (Guid serv in services)
            {
                if (serv == BluetoothService.SerialPort)
                {
                    has_serial_port = true;
                    serial_port = GPSConfig.GetBluetoothPortName(bdi.DeviceAddress);
                    break;
                }
            }

            using (GPSBluetoothSerialPortForm portform = new GPSBluetoothSerialPortForm(has_serial_port, serial_port))
            {
                portform.Owner = this;
                if (portform.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    Cursor.Current = Cursors.WaitCursor;
                    string SelectedPort = (string)portform.cbPortName.SelectedItem;
                    string PinValue = (string)portform.tbPin.Text;
                    int SelectedBaudRate = (int)portform.cbBaud.SelectedItem;

                    // 1. setup selected COM port for bluetooth device
                    if (bdi.Authenticated == false)
                    {
                        bool ret = BluetoothSecurity.PairRequest(bdi.DeviceAddress, PinValue);
                        if (ret == false)
                        {
                            Cursor.Current = Cursors.Default;
                            MessageBox.Show("Connection to selected bluetooth device failed. If your GPS receiver was turned off, turn it on and try again, or try other device");
                            return;
                        }
                    }

                    if (GPSConfig.SetBluetoothPortName(bdi.DeviceAddress, SelectedPort) == false)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Failed to open selected COM port. Choose other COM port or restart your phone and try again");
                        return;
                    }

                    // 2. set selected COM port as GPS Intermediate Driver Hardware port
                    IntermediateDriver.SetExternalInPort(SelectedPort);
                    IntermediateDriver.SetExternalInPortBaudRate(SelectedBaudRate);

                    string GPSIDOutPort = IntermediateDriver.GetOutPort();
                    if (GPSIDOutPort == null || GPSIDOutPort.Length == 0)
                    {
                        // there is no software COM port set in GPS Intermediate Driver

                        // choose first free COM port
                        GPSIDOutPort = GPSConfig.GetFreeGPSIDPort();
                        if (GPSIDOutPort == null || GPSIDOutPort.Length == 0)
                        {
                            MessageBox.Show("Can't find free COM port. GPS configuration failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                            Cursor.Current = Cursors.Default;
                            DialogResult = DialogResult.Cancel;
                            Close();
                        }

                        IntermediateDriver.SetOutPort(GPSIDOutPort);
                    }
                    IntermediateDriver.EnableIntermediateDriver();
                    IntermediateDriver.Update();

                    // 3. save configuration settings
                    Program.cfg.GPSPortName = IntermediateDriver.GetOutPort();
                    Program.cfg.GPSBaudRate = SelectedBaudRate;

                    Cursor.Current = Cursors.Default;
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
        }