Пример #1
0
 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     button2.Enabled = true;
     selectedDevice  = info[listBox1.SelectedIndex];
     if (MessageBox.Show(String.Format("Would you like to attempt to pair with {0}?", selectedDevice.DeviceName), "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
     {
         if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, "1234"))
         {
             listBox1.Items.Add("paired with " + selectedDevice.DeviceName);
             timer1.Enabled = true;
             InTheHand.Net.BluetoothEndPoint ep = new InTheHand.Net.BluetoothEndPoint(selectedDevice.DeviceAddress, BluetoothService.SerialPort);
             BluetoothClient cli = new BluetoothClient();
             cli.Connect(ep);
             peerStream = cli.GetStream();
         }
         else
         {
             listBox1.Items.Add("Failed to pair with " + selectedDevice.DeviceName);
         }
     }
 }
Пример #2
0
        public static bool InterfaceConnect(string port, object parameter)
        {
            if (IsInterfaceOpen())
            {
                return(true);
            }
            FastInit            = false;
            ConvertBaudResponse = false;
            AutoKeyByteResponse = false;
            AdapterType         = -1;
            AdapterVersion      = -1;
            LastCommTick        = DateTime.MinValue.Ticks;

            if (!port.StartsWith(PortId, StringComparison.OrdinalIgnoreCase))
            {
                InterfaceDisconnect();
                return(false);
            }
            _connectPort       = port;
            _reconnectRequired = false;
            try
            {
                string portData = port.Remove(0, PortId.Length);
                if ((portData.Length > 0) && (portData[0] == ':'))
                {
                    // special id
                    string   portName   = portData.Remove(0, 1);
                    string[] stringList = portName.Split('#', ';');
                    if (stringList.Length == 1)
                    {
                        SerialPort.PortName    = portName;
                        SerialPort.BaudRate    = 115200;
                        SerialPort.DataBits    = 8;
                        SerialPort.Parity      = System.IO.Ports.Parity.None;
                        SerialPort.StopBits    = System.IO.Ports.StopBits.One;
                        SerialPort.Handshake   = System.IO.Ports.Handshake.None;
                        SerialPort.DtrEnable   = false;
                        SerialPort.RtsEnable   = false;
                        SerialPort.ReadTimeout = 1;
                        SerialPort.Open();
                    }
#if BLUETOOTH
                    else if (stringList.Length == 2)
                    {
                        InTheHand.Net.BluetoothEndPoint ep =
                            new InTheHand.Net.BluetoothEndPoint(InTheHand.Net.BluetoothAddress.Parse(stringList[0]), InTheHand.Net.Bluetooth.BluetoothService.SerialPort);
                        InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient();
                        cli.SetPin(stringList[1]);
                        cli.Connect(ep);
                        BtStream             = cli.GetStream();
                        BtStream.ReadTimeout = 1;
                    }
#endif
                    else
                    {
                        InterfaceDisconnect();
                        return(false);
                    }
                }
                else
                {
                    InterfaceDisconnect();
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Ediabas?.LogFormat(EdiabasNet.EdLogLevel.Ifh, "*** Connect failure: {0}", ex.Message);
                InterfaceDisconnect();
                return(false);
            }
            return(true);
        }