/// <inheritdoc />
        internal override void StartListening(EndPoint desiredLocalListenEndPoint, bool useRandomPortFailOver)
        {
            if (IsListening) throw new InvalidOperationException("Attempted to call StartListening when already listening.");
            if(!(desiredLocalListenEndPoint is BluetoothEndPoint)) throw new ArgumentException("Bluetooth connections can only be made from a local BluetoothEndPoint", "desiredLocalListenIPEndPoint");

            try
            {
                ServiceRecordBuilder bldr = new ServiceRecordBuilder();
                bldr.AddServiceClass((desiredLocalListenEndPoint as BluetoothEndPoint).Service);
                if (IsDiscoverable)
                    bldr.AddCustomAttribute(new ServiceAttribute(NetworkCommsBTAttributeId.NetworkCommsEndPoint, ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 1)));

                listenerInstance = new BluetoothListener(desiredLocalListenEndPoint as BluetoothEndPoint, bldr.ServiceRecord);

                listenerInstance.Start();
                listenerInstance.BeginAcceptBluetoothClient(BluetoothConnectionReceivedAsync, null);
            }
            catch (SocketException)
            {
                //If the port we wanted is not available
                if (useRandomPortFailOver)
                {
                    try
                    {
                        Guid service = Guid.NewGuid();

                        ServiceRecordBuilder bldr = new ServiceRecordBuilder();
                        bldr.AddServiceClass(service);
                        if (IsDiscoverable)
                            bldr.AddCustomAttribute(new ServiceAttribute(NetworkCommsBTAttributeId.NetworkCommsEndPoint, ServiceElement.CreateNumericalServiceElement(ElementType.UInt8, 1)));

                        listenerInstance = new BluetoothListener(new BluetoothEndPoint((desiredLocalListenEndPoint as BluetoothEndPoint).Address, service), bldr.ServiceRecord);
                        listenerInstance.Start();
                        listenerInstance.BeginAcceptBluetoothClient(BluetoothConnectionReceivedAsync, null);
                    }
                    catch (SocketException)
                    {
                        //If we get another socket exception this appears to be a bad IP. We will just ignore this IP
                        if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Error("It was not possible to open a random port on " + desiredLocalListenEndPoint + ". This endPoint may not support listening or possibly try again using a different port.");
                        throw new CommsSetupShutdownException("It was not possible to open a random port on " + desiredLocalListenEndPoint + ". This endPoint may not support listening or possibly try again using a different port.");
                    }
                }
                else
                {
                    if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Error("It was not possible to listen on " + desiredLocalListenEndPoint.ToString() + ". This endPoint may not support listening or possibly try again using a different port.");
                    throw new CommsSetupShutdownException("It was not possible to listen on " + desiredLocalListenEndPoint.ToString() + ". This endPoint may not support listening or possibly try again using a different port.");
                }
            }

            this.LocalListenEndPoint = desiredLocalListenEndPoint;

            this.IsListening = true;
        }
Beispiel #2
0
        private void Start()
        {
            try
            {
                listener = new BluetoothListener(MyServiceUuid); // Listen on primary radio
                listener.Start();
                listener.BeginAcceptBluetoothClient(acceptBluetoothClient, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, string.Format("Could not start listener. {0}", ex.Message));
                return;
            }

            status.Text = string.Format("Listening at {0}, HCI version {1}...", 
                BluetoothRadio.PrimaryRadio.Name, BluetoothRadio.PrimaryRadio.HciVersion);
            startButton.Enabled = false;
            stopButton.Enabled = true;
        }
        void StartListener()
        {
            var listener = new BluetoothListener(OurServiceClassId);

            InfoMessage("Starting listener");
            listener.Start();
            listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallback, listener);
            InfoMessage("Started listening...");
        }