Esempio n. 1
0
        public void Connect(BluetoothDevice device, bool secure, string code)
        {
            MY_UUID_SECURE   = UUID.FromString(MY_UUID + code);
            MY_UUID_INSECURE = UUID.FromString(MY_UUID + code);

            if (state == STATE_CONNECTING)
            {
                if (connectThread != null)
                {
                    connectThread.Cancel();
                    connectThread = null;
                }
            }

            // Cancel any thread currently running a connection
            if (connectedThread != null)
            {
                connectedThread.Cancel();
                connectedThread = null;
            }

            // Start the thread to connect with the given device
            connectThread = new ConnectThread(device, this, secure);
            connectThread.Start();

            UpdateUserInterfaceTitle();
        }
Esempio n. 2
0
        public void Connect(BluetoothDevice device)
        {
            if (Debug)
            {
                Log.Debug(TAG, "connect to: " + device);
            }

            // Cancel any thread attempting to make a connection
            if (_state == STATE_CONNECTING)
            {
                if (connectThread != null)
                {
                    connectThread.Cancel();
                    connectThread = null;
                }
            }

            // Cancel any thread currently running a connection
            if (connectedThread != null)
            {
                connectedThread.Cancel();
                connectedThread = null;
            }

            // Start the thread to connect with the given device
            connectThread = new ConnectThread(device, this);
            connectThread.Start();

            SetState(STATE_CONNECTING);
        }
Esempio n. 3
0
        /// <summary>
        /// 连接打印设备
        /// </summary>
        /// <param name="device">设备</param>
        public void Connect(BluetoothDevice device)
        {
            lock (Lock)
            {
                //Log.d(TAG, "connect to: " + device);

                //EventBus.getDefault().post(new PrintMsgEvent(PrinterMsgType.MESSAGE_TOAST, "正在连接蓝牙设备"));

                //如果设备正在连接蓝牙设备,则取消连接线程
                if (mState == STATE_CONNECTING)
                {
                    if (mConnectThread != null)
                    {
                        mConnectThread.Cancel();
                        mConnectThread = null;
                    }
                }

                // 已经连接,取消当前运行连接的任何线程
                if (mConnectedThread != null)
                {
                    mConnectedThread.Cancel();
                    mConnectedThread = null;
                }

                //重新启动线程以连接给定的设备
                mConnectThread = new ConnectThread(device);
                mConnectThread.Start();

                //更改连接状态
                SetState(STATE_CONNECTING);
            }
        }
Esempio n. 4
0
        public void Connect(BluetoothDevice device)
        {
            if (Debug)
            {
                Log.Debug(Tag, "Connect to: " + device);
            }

            // Igen sørger vi for at der ikke er andre tråde der prøver at få forbindelse
            if (_state == State_Connecting)
            {
                if (connectThread != null)
                {
                    connectThread.Cancel();
                    connectThread = null;
                }
            }

            if (connectedThread != null)
            {
                connectedThread.Cancel();
                connectedThread = null;
            }

            connectThread = new ConnectThread(device, this);
            connectThread.Start();

            SetState(State_Connecting);
        }
Esempio n. 5
0
        /// <summary>
        /// Start the ConnectThread to initiate a connection to a remote device. </summary>
        /// <param name="device">  The BluetoothDevice to connect </param>
        /// <param name="secure"> Socket Security type - Secure (true) , Insecure (false) </param>
        public void Connect(BluetoothDevice device, bool secure)
        {
            lock (this)
            {
                if (D)
                {
                    Log.D(TAG, "connect to: " + device);
                }

                // Cancel any thread attempting to make a connection
                if (mState == STATE_CONNECTING)
                {
                    if (mConnectThread != null)
                    {
                        mConnectThread.Cancel();
                        mConnectThread = null;
                    }
                }

                // Cancel any thread currently running a connection
                if (mConnectedThread != null)
                {
                    mConnectedThread.Cancel();
                    mConnectedThread = null;
                }

                // Start the thread to connect with the given device
                mConnectThread = new ConnectThread(this, device, secure);
                mConnectThread.Start();
                State = STATE_CONNECTING;
            }
        }
Esempio n. 6
0
        public void OpenDeviceConnection(ContentPage contentPage, BluetoothDevices bluetoothDevice)
        {
            mDevice = mBoundedDevices[bluetoothDevice.Name] as BluetoothDevice;
            ConnectThread connectThread = new ConnectThread(contentPage, mDevice);

            connectThread.Start();
        }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        protected static Socket GetSocket(String host, int port, int timeout)
        {
            ConnectThread thread = new ConnectThread(host, port);

            thread.Start();

            int timer = 0;
            int sleep = 25;

            while (timer < timeout)
            {
                if (thread.IsConnected)
                {
                    return(thread.Socket);
                }

                if (thread.IsError)
                {
                    throw new IOException();
                }

                try
                {
                    Thread.Sleep(sleep);
                }
                catch (ThreadInterruptedException) { }

                timer += sleep;
            }

            throw new IOException(GetLocalizedString("connect timeout").Replace("$$timeout$$", timeout.ToString(new System.Globalization.NumberFormatInfo())));
        }
        /// <summary> Connects both <code>TransportLayer</code>s in separate threads,
        /// and returns when both have been connected, or when cancelConnect()
        /// is called.
        /// </summary>
        public virtual void  connect()
        {
            isConnecting = true;
            ConnectThread c1 = new ConnectThread(myTransportA);
            ConnectThread c2 = new ConnectThread(myTransportB);

            c1.Start();
            c2.Start();

            while (isConnecting && (!c1.Connected || !c2.Connected) && c1.Exception == null && c2.Exception == null)
            {
                try
                {
                    System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 1));
                }
                catch (System.Threading.ThreadInterruptedException)
                {
                }
            }

            if (c1.Exception != null)
            {
                throw c1.Exception;
            }
            if (c2.Exception != null)
            {
                throw c2.Exception;
            }
        }
Esempio n. 9
0
        protected void Init()
        {
            base.Init();
            mReferrerTv    = FindViewById <TextView>(Resource.Id.install_referrer_tv);
            mClickTimeTv   = FindViewById <TextView>(Resource.Id.click_time_tv);
            mInstallTimeTv = FindViewById <TextView>(Resource.Id.install_time_tv);

            ConnectThread connectThread = new ConnectThread(this);

            connectThread.Start();
        }
        public void ReConnect()
        {
            if (TCP.Connected)
            {
                throw new Exception("Al connected");
            }
            if (ConnectThread != null)
            {
                throw new Exception("Nog aan het connecten.");
            }
            setConnecting(true);

            setConnectThread(new Thread(ConnectToServer));
            ConnectThread.Name         = "TCPReconnectThread";
            ConnectThread.IsBackground = true;
            ConnectThread.Start();
        }
        public void Connect(IPEndPoint endPoint)
        {
            if (TCP.Connected)
            {
                throw new Exception("Al connected");
            }
            if (ConnectThread != null)
            {
                throw new Exception("Kan niet");
            }
            setTempEndPoint(endPoint);
            setConnecting(true);

            setConnectThread(new Thread(ConnectToServer));
            ConnectThread.Name         = "TCPConnectThread";
            ConnectThread.IsBackground = true;
            ConnectThread.Start();
        }
Esempio n. 12
0
        private void setMailBoxes()
        {
            MessagingCenter.Subscribe <Object, String>(this, "Request",
                                                       (sender, arg) =>
            {
                String request = arg;

                if (request.Equals("Server"))
                {
                    ruolo = false;
                    System.Console.WriteLine("Richiesta di Server");
                    server = new AcceptThread();
                    server.Start();
                }
                else if (request.Equals("Client"))
                {
                    ruolo = true;
                    System.Console.WriteLine("Richiesta di Client");
                    client = new ConnectThread(Bth.serverDevice, MY_UUID);
                    client.Start();
                }
                else if (request.Equals("Discovery"))
                {
                    System.Console.WriteLine("Richiesta di Discoverability");
                    AbilitaDiscoverabilty();
                }
                else if (request.Equals("Reset"))
                {
                    System.Console.WriteLine("Richiesta di Reset");
                    reset();
                }
            });

            MessagingCenter.Subscribe <Object, string>(this, "IncomingMessage",
                                                       (sender, arg) =>
            {
                System.Console.WriteLine("posizione cliccata: " + arg[0]);
                System.Console.WriteLine("esito: " + arg[1]);
                byte[] bytes = Encoding.Unicode.GetBytes(arg);
                mConnectedThread.Write(bytes);
            });
        }
Esempio n. 13
0
        /// <summary>
        /// Method which spawns thread to get a connection and then enforces a timeout on the initial
        /// connection.
        ///
        /// This should be backed by a thread pool.  Any volunteers?
        /// </summary>
        /// <param name="host">host to establish connection to</param>
        /// <param name="port">port on that host</param>
        /// <param name="timeout">connection timeout in ms</param>
        /// <returns>connected socket</returns>
        protected static Socket GetSocket(String host, int port, int timeout)
        {
            // Create a new thread which will attempt to connect to host:port, and start it running
            ConnectThread thread = new ConnectThread(host, port);

            thread.Start();

            int timer = 0;
            int sleep = 25;

            while (timer < timeout)
            {
                // if the thread has a connected socket
                // then return it
                if (thread.IsConnected)
                {
                    return(thread.Socket);
                }

                // if the thread had an error
                // then throw a new IOException
                if (thread.IsError)
                {
                    throw new IOException();
                }

                try
                {
                    // sleep for short time before polling again
                    Thread.Sleep(sleep);
                }
                catch (ThreadInterruptedException) { }

                // Increment timer
                timer += sleep;
            }

            // made it through loop without getting connection
            // the connection thread will timeout on its own at OS timeout
            throw new IOException(GetLocalizedString("connect timeout").Replace("$$timeout$$", timeout.ToString(new System.Globalization.NumberFormatInfo())));
        }
Esempio n. 14
0
        public void Connect(BluetoothDevice device)
        {
            if (state == ConnectionState.Connecting)
            {
                if (connectThread != null)
                {
                    connectThread.Cancel();
                    connectThread = null;
                }
            }

            if (connectedThread != null)
            {
                connectedThread.Cancel();
                connectedThread = null;
            }

            connectThread = new ConnectThread(device, this);
            connectThread.Start();

            UpdateUserInterfaceTitle();
        }
        public void Connect(BluetoothDevice device, Boolean secure)
        {
            if (_state == Constants.STATE_CONNECTING)
            {
                if (_connectThread != null)
                {
                    _connectThread.Cancel();
                    _connectThread = null;
                }
            }

            // Cancel any thread currently running a connection
            if (_connectedThread != null)
            {
                _connectedThread.Cancel();
                _connectedThread = null;
            }

            // Start the thread to connect with the given device
            _connectThread = new ConnectThread(device, this, secure);
            _connectThread.Start();

            UpdateUserInterfaceTitle();
        }
		/// <summary>
		/// Start the ConnectThread to initiate a connection to a remote device. </summary>
		/// <param name="device">  The BluetoothDevice to connect </param>
		/// <param name="secure"> Socket Security type - Secure (true) , Insecure (false) </param>
		public void Connect(BluetoothDevice device, bool secure)
		{
		    lock (this)
		    {
		        if (D)
		        {
		            Log.D(TAG, "connect to: " + device);
		        }

		        // Cancel any thread attempting to make a connection
		        if (mState == STATE_CONNECTING)
		        {
		            if (mConnectThread != null)
		            {
		                mConnectThread.Cancel();
		                mConnectThread = null;
		            }
		        }

		        // Cancel any thread currently running a connection
		        if (mConnectedThread != null)
		        {
		            mConnectedThread.Cancel();
		            mConnectedThread = null;
		        }

		        // Start the thread to connect with the given device
		        mConnectThread = new ConnectThread(this, device, secure);
		        mConnectThread.Start();
		        State = STATE_CONNECTING;
		    }
		}
		public void Connect (BluetoothDevice device)
		{
			if (Debug)
				Log.Debug (TAG, "connect to: " + device);
	
			// Cancel any thread attempting to make a connection
			if (_state == STATE_CONNECTING) {
				if (connectThread != null) {
					connectThread.Cancel ();
					connectThread = null;
				}
			}
	
			// Cancel any thread currently running a connection
			if (connectedThread != null) {
				connectedThread.Cancel ();
				connectedThread = null;
			}
	
			// Start the thread to connect with the given device
			connectThread = new ConnectThread (device, this);
			connectThread.Start ();
			
			SetState (STATE_CONNECTING);
		}
Esempio n. 18
0
        public static void StartConnectThread(BluetoothDevice bd, BluetoothAdapter adapter)
        {
            ConnectThread ct = new ConnectThread(bd, adapter);

            ct.Start();
        }
Esempio n. 19
0
        /// <summary>
        /// Method which spawns thread to get a connection and then enforces a timeout on the initial
        /// connection.
        /// 
        /// This should be backed by a thread pool.  Any volunteers?
        /// </summary>
        /// <param name="host">host to establish connection to</param>
        /// <param name="port">port on that host</param>
        /// <param name="timeout">connection timeout in ms</param>
        /// <returns>connected socket</returns>
        protected static Socket GetSocket(String host, int port, int timeout)
        {
            // Create a new thread which will attempt to connect to host:port, and start it running
            ConnectThread thread = new ConnectThread(host, port);
            thread.Start();

            int timer = 0;
            int sleep = 25;

            while (timer < timeout)
            {

                // if the thread has a connected socket
                // then return it
                if (thread.IsConnected)
                    return thread.Socket;

                // if the thread had an error
                // then throw a new IOException
                if (thread.IsError)
                    throw new IOException();

                try
                {
                    // sleep for short time before polling again
                    Thread.Sleep(sleep);
                }
                catch (ThreadInterruptedException) { }

                // Increment timer
                timer += sleep;
            }

            // made it through loop without getting connection
            // the connection thread will timeout on its own at OS timeout
            throw new IOException(GetLocalizedString("connect timeout").Replace("$$timeout$$", timeout.ToString(new System.Globalization.NumberFormatInfo())));
        }
 public void StartClient(BluetoothDevice dev, UUID uuid)
 {
     connectThread = new ConnectThread(this, dev, uuid);
     connectThread.Start();
 }