Exemple #1
0
        public void Connected(BluetoothSocket socket, BluetoothDevice device, string socketType)
        {
            // Cancel the thread that completed the connection
            if (connectThread != null)
            {
                connectThread.Cancel();
                connectThread = null;
            }

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


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

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

            // Start the thread to manage the connection and perform transmissions
            connectedThread = new ConnectedThread(socket, this, socketType);
            connectedThread.Start();

            // Send the name of the connected device back to the UI Activity
            var    msg    = handler.ObtainMessage(Constants.MESSAGE_DEVICE_NAME);
            Bundle bundle = new Bundle();

            bundle.PutString(Constants.DEVICE_NAME, device.Name);
            msg.Data = bundle;
            handler.SendMessage(msg);

            UpdateUserInterfaceTitle();
        }
Exemple #2
0
        public void Connected(BluetoothSocket socket, BluetoothDevice device)
        {
            if (Debug)
            {
                Log.Debug(TAG, "connected");
            }

            // Cancel the thread that completed the connection
            if (connectThread != null)
            {
                connectThread.Cancel();
                connectThread = null;
            }

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

            // Cancel the accept thread because we only want to connect to one device
            if (acceptThread != null)
            {
                acceptThread.Cancel();
                acceptThread = null;
            }

            // Start the thread to manage the connection and perform transmissions
            connectedThread = new ConnectedThread(socket, this);
            connectedThread.Start();

            // Send the name of the connected device back to the UI Activity
            var    msg    = _handler.ObtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);
            Bundle bundle = new Bundle();

            bundle.PutString(BluetoothChat.DEVICE_NAME, device.Name);
            msg.Data = bundle;
            _handler.SendMessage(msg);

            SetState(STATE_CONNECTED);
        }
Exemple #3
0
        private static void reset()
        {
            System.Console.WriteLine("Reset Chiamata");

            if (mConnectedThread != null)
            {
                mConnectedThread.Cancel();
                mConnectedThread = null;
            }
            if (client != null)
            {
                client.Cancel();
                client = null;
            }
            if (server != null)
            {
                server.Cancel();
                server = null;
            }
        }
Exemple #4
0
        public void Connect(BluetoothDevice device)
        {
            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.Run();
        }
Exemple #5
0
        /// <summary>
        /// 启动ConnectedThread开始管理蓝牙连接
        /// </summary>
        /// <param name="socket">建立连接的蓝牙插座</param>
        /// <param name="device">已连接的蓝牙设备</param>
        /// <param name="socketType"></param>
        public static void Connected(BluetoothSocket socket, BluetoothDevice device, string socketType)
        {
            lock (Lock)
            {
                //Log.d(TAG, "connected, Socket Type:" + socketType);

                // 取消完成连接的线程
                if (mConnectThread != null)
                {
                    mConnectThread.Cancel();
                    mConnectThread = null;
                }

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

                // 取消接受线程,因为我们只想连接到一个设备
                if (mAcceptThread != null)
                {
                    mAcceptThread.Cancel();
                    mAcceptThread = null;
                }

                // 启动线程来管理连接并执行传输
                mConnectedThread = new ConnectedThread(socket);
                mConnectedThread.Start();

                // 将连接设备的名称发送回UI活动
                //EventBus.getDefault().post(new PrintMsgEvent(PrinterMsgType.MESSAGE_TOAST, "蓝牙设备连接成功"));

                SetState(STATE_CONNECTED);

                // 调用打印队列打印
                PrintQueue.GetQueue(mContext).Print();
            }
        }
Exemple #6
0
        public void Connected(BluetoothSocket socket, BluetoothDevice device)
        {
            StopRunningConnectThread();

            // Cancel the accept thread because we only want to connect to one device
            if (mSecureAcceptThread != null)
            {
                mSecureAcceptThread.Cancel();
                mSecureAcceptThread = null;
            }

            // Start the thread to manage the connection and perform transmissions
            mConnectedThread = new ConnectedThread(socket, this);

            // Send the name of the connected device back to the UI Activity
            Message msg    = MessageHandler.ObtainMessage(Constants.MESSAGE_DEVICE_NAME);
            Bundle  bundle = new Bundle();

            bundle.PutString(Constants.DEVICE_NAME, device.Name);
            msg.Data = bundle;
            MessageHandler.SendMessage(msg);
        }
        /// <summary>
        /// Start the chat service. Specifically start AcceptThread to begin a
        /// </summary>
        public void Start()
        {
            lock (this)
            {
                if (D)
                {
                    Log.D(TAG, "start");
                }

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

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

                State = STATE_LISTEN;

                // Start the thread to listen on a BluetoothServerSocket
                if (mSecureAcceptThread == null)
                {
                    mSecureAcceptThread = new AcceptThread(this, true);
                    mSecureAcceptThread.Start();
                }
                if (mInsecureAcceptThread == null)
                {
                    mInsecureAcceptThread = new AcceptThread(this, false);
                    mInsecureAcceptThread.Start();
                }
            }
        }
            public void dinlemeyeBasla()
            {
                Socket istemciSocket = null;

                tcpListener.Start();

                while (true)
                {
                    try
                    {
                        istemciSocket = tcpListener.AcceptSocket();
                    }
                    catch (Exception e)
                    {
                        System.Windows.MessageBox.Show(e.Message);
                        break;
                    }

                    if (istemciSocket != null)
                    {
                        connectedThread = new ConnectedThread(istemciSocket);
                        connectedThread.thread.Start();
                        //İlk deger gonderme
                        Dictionary <string, string> dict = new Dictionary <string, string>();
                        dict.Add("mesaj", mesaj_1);
                        dict.Add("lamba", lamba.AcikMi.ToString());
                        dict.Add("sayi", lamba.Sayi.ToString());

                        string s = string.Join(";", dict.Select(x => x.Key + "=" + x.Value).ToArray());
                        connectedThread.Yaz(s);

                        // System.Windows.MessageBox.Show("istemci baglandi");
                        istemciSocket = null;
                    }
                }
            }
Exemple #9
0
            public void Run()
            {
                // Cancel discovery because it will slow down the connection
                mmBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
                mmBluetoothAdapter.CancelDiscovery();

                try
                {
                    // Connect the device through the socket. This will block
                    // until it succeeds or throws an exception
                    mmSocket.Connect();
                    Debug.WriteLine("BluetoothSocket: CONNECTED");
                    // Do work to manage the connection (in a separate thread)
                    // TODO manageConnectedSocket(mmSocket);
                    ConnectedThread connectedThread = new ConnectedThread(mmContentPage, mmSocket);
                    connectedThread.Start();
                }
                catch (Java.IO.IOException e)
                {
                    mmSocket.Close();
                    Debug.WriteLine("BluetoothSocket: NOT CONNECTED");
                    e.PrintStackTrace();
                }
            }
Exemple #10
0
        public void connect(BluetoothDevice device, bool secure)
        {
            lock (mutex) {

                if (_state == STATE_CONNECTING) {

                    if (_connectThread != null) {

                        _connectThread.cancel ();
                        _connectThread = null;
                    }
                }

                if (_connectedThread != null) {

                    _connectedThread.cancel ();
                    _connectedThread = null;
                }

                _connectThread = new ConnectThread (device, secure);
                //_connectThread.Start ();
                setState (STATE_CONNECTING);
            }
        }
        public void Connect(BluetoothDevice device, bool secure)
        {
            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();
        }
Exemple #12
0
            public void Run()
            {
                // Cancel discovery because it otherwise slows down the connection.
                Bth.mBluetoothAdapter.CancelDiscovery();

                try
                {
                    // Connect to the remote device through the socket. This call blocks
                    // until it succeeds or throws an exception.
                    System.Console.WriteLine("Tentativo di Conessione come Client");
                    mmSocket.Connect();
                }
                catch (Java.IO.IOException connectException)
                {
                    System.Console.WriteLine("Errore: " + connectException);
                    // Unable to connect; close the socket and return.
                    try
                    {
                        mmSocket.Close();
                    }
                    catch (Java.IO.IOException closeException)
                    {
                        System.Console.WriteLine("Errore: " + closeException);
                    }
                    return;
                }

                System.Console.WriteLine("Connesso come Client");



                // The connection attempt succeeded. Perform work associated with
                // the connection in a separate thread.
                mConnectedThread = new ConnectedThread(mmSocket);
                mConnectedThread.Start();
            }
        public void Start()
        {
            if (Debug)
                Log.Debug (TAG, "start");

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

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

            SetState (STATE_LISTEN);
        }
		/// <summary>
		/// Stop all threads
		/// </summary>
		public void Stop()
		{
		    lock (this)
		    {
		        if (D)
		        {
		            Log.D(TAG, "stop");
		        }

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

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

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

		        if (mInsecureAcceptThread != null)
		        {
		            mInsecureAcceptThread.Cancel();
		            mInsecureAcceptThread = null;
		        }
		        State = STATE_NONE;
		    }
		}
Exemple #15
0
        public void stop()
        {
            lock (mutex) {

                if (_connectThread != null) {

                    _connectThread.cancel ();
                    _connectThread = null;
                }

                if (_connectedThread != null) {

                    _connectedThread.cancel ();
                    _connectedThread = null;
                }

                if (_secureAcceptThread != null) {

                    _secureAcceptThread.cancel ();
                    _secureAcceptThread = null;
                }

                if (_insecureAcceptThread != null) {

                    _insecureAcceptThread.cancel ();
                    _insecureAcceptThread = null;
                }

                setState (STATE_NONE);
            }
        }
		/// <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;
		    }
		}
		/// <summary>
		/// Start the ConnectedThread to begin managing a Bluetooth connection </summary>
		/// <param name="socket">  The BluetoothSocket on which the connection was made </param>
		/// <param name="device">  The BluetoothDevice that has been connected </param>
		public void Connected(BluetoothSocket socket, BluetoothDevice device, string socketType)
		{
		    lock (this)
		    {
		        if (D)
		        {
		            Log.D(TAG, "connected, Socket Type:" + socketType);
		        }

		        // Cancel the thread that completed the connection
		        if (mConnectThread != null)
		        {
		            mConnectThread.Cancel();
		            mConnectThread = null;
		        }

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

		        // Cancel the accept thread because we only want to connect to one device
		        if (mSecureAcceptThread != null)
		        {
		            mSecureAcceptThread.Cancel();
		            mSecureAcceptThread = null;
		        }
		        if (mInsecureAcceptThread != null)
		        {
		            mInsecureAcceptThread.Cancel();
		            mInsecureAcceptThread = null;
		        }

		        // Start the thread to manage the connection and perform transmissions
		        mConnectedThread = new ConnectedThread(this, socket, socketType);
		        mConnectedThread.Start();

		        // Send the name of the connected device back to the UI Activity
		        Message msg = mHandler.ObtainMessage(global::BluetoothChat.BluetoothChat.MESSAGE_DEVICE_NAME);
		        Bundle bundle = new Bundle();
		        bundle.PutString(global::BluetoothChat.BluetoothChat.DEVICE_NAME, device.GetName());
		        msg.SetData(bundle);
		        mHandler.SendMessage(msg);

		        State = STATE_CONNECTED;
		    }
		}
Exemple #18
0
        public void connected(BluetoothSocket socket, BluetoothDevice device, String socketType)
        {
            lock (mutex) {
                if (_connectThread != null) {

                    _connectThread.cancel ();
                    _connectThread = null;
                }

                if (_connectedThread != null) {

                    _connectedThread.cancel ();
                    _connectedThread = null;
                }

                if (_secureAcceptThread != null) {

                    _secureAcceptThread.cancel ();
                    _secureAcceptThread = null;
                }

                if (_insecureAcceptThread != null) {

                    _insecureAcceptThread.cancel ();
                    _insecureAcceptThread = null;
                }

                _connectedThread = new ConnectedThread (socket, socketType);
                //_connectThread.start ();

                Message msg = _handler.ObtainMessage (Utility.MESSAGE_DEVICE_NAME);
                Bundle bundle = new Bundle ();
                bundle.PutString (Utility.DEVICE_NAME, device.Name);
                msg.Data = bundle;
                _handler.SendMessage (msg);

                setState (STATE_CONNECTED);
            }
        }
		/// <summary>
		/// Start the chat service. Specifically start AcceptThread to begin a
		/// </summary>
		public void Start()
		{
		    lock (this)
		    {
		        if (D)
		        {
		            Log.D(TAG, "start");
		        }

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

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

		        State = STATE_LISTEN;

		        // Start the thread to listen on a BluetoothServerSocket
		        if (mSecureAcceptThread == null)
		        {
		            mSecureAcceptThread = new AcceptThread(this, true);
		            mSecureAcceptThread.Start();
		        }
		        if (mInsecureAcceptThread == null)
		        {
		            mInsecureAcceptThread = new AcceptThread(this, false);
		            mInsecureAcceptThread.Start();
		        }
		    }
		}
		public void Stop ()
		{
			if (Debug)
				Log.Debug (TAG, "stop");
	
			if (connectThread != null) {
				connectThread.Cancel ();
				connectThread = null;
			}
	
			if (connectedThread != null) {
				connectedThread.Cancel ();
				connectedThread = null;
			}
	
			if (acceptThread != null) {
				acceptThread.Cancel ();
				acceptThread = null;
			}
			
			SetState (STATE_NONE);
		}
		public void Connected (BluetoothSocket socket, BluetoothDevice device)
		{
			if (Debug)
				Log.Debug (TAG, "connected");
	
			// Cancel the thread that completed the connection
			if (connectThread != null) {
				connectThread.Cancel ();
				connectThread = null;
			}
	
			// Cancel any thread currently running a connection
			if (connectedThread != null) {
				connectedThread.Cancel ();
				connectedThread = null;
			}
	
			// Cancel the accept thread because we only want to connect to one device
			if (acceptThread != null) {
				acceptThread.Cancel ();
				acceptThread = null;
			}
			
			// Start the thread to manage the connection and perform transmissions
			connectedThread = new ConnectedThread (socket, this);
			connectedThread.Start ();
	
			// Send the name of the connected device back to the UI Activity
			var msg = _handler.ObtainMessage (BluetoothChat.MESSAGE_DEVICE_NAME);
			Bundle bundle = new Bundle ();
			bundle.PutString (BluetoothChat.DEVICE_NAME, device.Name);
			msg.Data = bundle;
			_handler.SendMessage (msg);
	
			SetState (STATE_CONNECTED);
		}
		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);
		}
		public void Start ()
		{	
			if (Debug)
				Log.Debug (TAG, "start");
	
			// Cancel any thread attempting to make a connection
			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 listen on a BluetoothServerSocket
			if (acceptThread == null) {
				acceptThread = new AcceptThread (this);
				acceptThread.Start ();
			}
			
			SetState (STATE_LISTEN);
		}
Exemple #24
0
        public void start()
        {
            lock (mutex) {

                if (_connectThread != null) {

                    _connectThread.cancel ();
                    _connectThread = null;
                }

                if (_connectedThread != null) {

                    _connectedThread.cancel ();
                    _connectedThread = null;
                }

                setState (STATE_LISTEN);

                if (_secureAcceptThread == null) {

                    _secureAcceptThread = new AcceptThread (true);
                    //_secureAcceptThread.Start ();
                }

                if (_insecureAcceptThread == null) {

                    _insecureAcceptThread = new AcceptThread (false);
                    //_insecureAcceptThread.Start ();
                }
            }
        }