Exemple #1
0
        public void Connected(BluetoothSocket socket, BluetoothDevice device)
        {
            if (Debug)
            {
                Log.Debug(Tag, "Connected");
            }

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

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

            // Canceller accepttråden fordi vi kun vil have et device på vores connection.
            if (acceptThread != null)
            {
                acceptThread.Cancel();
                acceptThread = null;
            }

            connectedThread = new ConnectedThread(socket, this);
            connectedThread.Start();

            // Her skal der være noget der sender vores connected device til gui.

            SetState(State_Connected);
        }
Exemple #2
0
            public void Run()
            {
                BluetoothSocket socket = null;

                // Keep listening until exception occurs or a socket is returned.
                while (true)
                {
                    try
                    {
                        System.Console.WriteLine("Tentativo di Conessione come Server");

                        socket = mmServerSocket.Accept();
                    }
                    catch (Java.IO.IOException e)
                    {
                        System.Console.WriteLine("Errore: " + e);
                        break;
                    }
                    System.Console.WriteLine("Connesso come Server");

                    if (socket != null)
                    {
                        mConnectedThread = new ConnectedThread(socket);
                        mConnectedThread.Start();
                        break;
                    }
                }
            }
        public void Connected(BluetoothSocket socket, BluetoothDevice device)
        {
            if (connectThread != null)
            {
                connectThread.Cancel();
                connectThread = null;
            }

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


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

            connectedThread = new ConnectedThread(socket, this);
            connectedThread.Start();

            var    msg    = handler.ObtainMessage((int)BluetoothState.DeviceName);
            Bundle bundle = new Bundle();

            bundle.PutString("DeviceName", device.Name);
            msg.Data = bundle;
            handler.SendMessage(msg);

            UpdateUserInterfaceTitle();
        }
 public static void StartConnectedThread(BluetoothSocket socket)
 {
     cnt = new ConnectedThread(socket);
     presenter.onActivityResult(Constants.CONN_OK, Result.Ok, new Intent().PutExtra("msg", "Connected to " + socket.RemoteDevice.Name));
     Bluetooth.connectedDevice = socket.RemoteDevice;
     cnt.Start();
 }
        public void Connected(BluetoothSocket socket)
        {
            Logger.Log("Connected: Starting");
            connectedThread = new ConnectedThread(this, socket);
            connectedThread.Start();

            //var msg = handler.ObtainMessage(device_name);
            //Bundle bundle = new Bundle();
            //bundle.PutString(device_name, device.Name);
            //msg.Data = bundle;
            //handler.SendMessage(msg);
        }
        /// <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 #7
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.Dispose();
                connectThread = null;
            }

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

            // Cancel the accept thread because we only want to connect to one device
            if (acceptThread != null)
            {
                acceptThread.Cancel();
                acceptThread.Dispose();
                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(MainActivity.MESSAGE_DEVICE_NAME);
            Bundle bundle = new Bundle();

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

            SetState(STATE_CONNECTED);
        }
        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 #9
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 #10
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();
            }
Exemple #11
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();
                }
            }
		/// <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;
		    }
		}
		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);
		}