Esempio n. 1
0
        static void Main(string[] args)
        {
            Guid serviceUUID = BluetoothService.TcpProtocol; // new Guid("00000004-0000-1000-8000-00805F9B34FB");

            BluetoothEndPoint bep          = new BluetoothEndPoint(BluetoothAddress.None, BluetoothService.TcpProtocol);
            BluetoothListener blueListener = new BluetoothListener(bep);

            blueListener.Start();

            Console.WriteLine("accepting...");

            while (true)
            {
                Socket socket = blueListener.AcceptSocket();

                string text = socket.ReadString();
                Console.WriteLine("socket accepted: " + text);
                socket.WriteString("Echo: " + text);
                socket.Close();
                Console.WriteLine("Closed");
            }
        }
Esempio n. 2
0
        private void ListenerLoop()
        {
            if (_stringBuilder == null)
            {
                return;
            }

            BluetoothListener listener = new BluetoothListener(new Guid(c_socket_Guid))
            {
                Authenticate   = false
                , ServiceClass = ServiceClass.Network | ServiceClass.Information
            };

            try {
                listener.Start();
                while (!_needCancel)
                {
                    if (listener.Pending())
                    {
                        _clientSocket = listener.AcceptSocket();
                        if (_clientSocket == null)
                        {
                            throw new ArgumentNullException(nameof(_clientSocket));
                        }

                        UpdateMessages($"Получено клиентское соединение для {_clientSocket.RemoteEndPoint}");

                        byte[] buffer = new byte[4096];
                        while (_clientSocket.Connected)
                        {
                            try {
                                int bytes = _clientSocket.Receive(buffer);
                                if (bytes == 0)
                                {
                                    continue;
                                }

                                string message = Encoding.UTF8.GetString(buffer, 0, bytes);

                                _stringBuilder.Append(message);
                                string fullMessage = _stringBuilder.ToString();
                                int    endIndex    = fullMessage.IndexOf("\r\n", StringComparison.Ordinal);
                                if (endIndex < 0)
                                {
                                    continue;
                                }

                                _stringBuilder.Clear();
                                UpdateMessages(@"Принята команда: " + fullMessage);

                                dxCommand command = dxCommand.Deserialize(fullMessage);
                                if (command == null)
                                {
                                    continue;
                                }

                                // ReSharper disable once SwitchStatementMissingSomeCases
                                switch (command.Name)
                                {
                                case CommandNames.Get_Attributes:
                                {
                                    dxCommand result = new dxCommand(_mainForm.Data.Attributes)
                                    {
                                        Name = CommandNames.Get_Attributes
                                    };

                                    string reply  = result.Serialize() + "\r\n";
                                    byte[] toSend = Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(reply));
                                    _clientSocket.Send(toSend, toSend.Length, SocketFlags.None);
                                    UpdateMessages("Отправлен ответ: " + reply);
                                    break;
                                }

                                case CommandNames.Get_Parameters:
                                {
                                    dxCommand result = new dxCommand(_mainForm.Data.Parameters)
                                    {
                                        Name = CommandNames.Get_Parameters
                                    };

                                    string reply  = result.Serialize() + "\r\n";
                                    byte[] toSend = Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(reply));
                                    _clientSocket.Send(toSend, toSend.Length, SocketFlags.None);
                                    UpdateMessages("Отправлен ответ: " + reply);
                                    break;
                                }

                                case CommandNames.Set_Parameters:
                                {
                                    foreach (dxParameter parameter in command.Parameters.ParameterItem)
                                    {
                                        foreach (dxParameter deviceParameter in _mainForm.Data.Parameters.ParameterItem.Where(deviceParameter => deviceParameter.Id.Equals(parameter.Id)))
                                        {
                                            switch (deviceParameter.Type)
                                            {
                                            case ParameterType.Bool:
                                            {
                                                deviceParameter.BoolValue = parameter.BoolValue;
                                                break;
                                            }

                                            case ParameterType.Int:
                                            {
                                                deviceParameter.IntValue = parameter.IntValue;
                                                break;
                                            }

                                            default:
                                            {
                                                continue;
                                            }
                                            }

                                            UpdateParameterValue(_mainForm.Data.Parameters.ParameterItem.IndexOf(deviceParameter), deviceParameter);
                                            UpdateMessages(@"Установить значение параметра " + deviceParameter.Name);
                                        }
                                    }
                                    break;
                                }
                                }
                            }
                            catch (IOException ex) {
                                UpdateMessages(@"Соединение разорвано: " + ex);
                                break;
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                }
            }
            catch (Exception ex) {
                UpdateMessages(ex.ToString());
            }
            finally {
                CloseCommunication();
                listener.Stop();
            }
        }