public void Start()
        {
            if (_connectThread != null)
            {
                _connectThread.Cancel();
                _connectThread = null;
            }

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

            if (_secureAcceptThread == null)
            {
                _secureAcceptThread = new AcceptThread(this, true);
                _secureAcceptThread.Start();
            }
            if (_insecureAcceptThread == null)
            {
                _insecureAcceptThread = new AcceptThread(this, false);
                _insecureAcceptThread.Start();
            }

            UpdateUserInterfaceTitle();
        }
Beispiel #2
0
        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 Stop()
        {
            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;
            }

            _state = Constants.STATE_NONE;
            UpdateUserInterfaceTitle();
        }
        /// <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;
            }
        }
        public void Stop()
        {
            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;
            }

            state = STATE_NONE;
            UpdateUserInterfaceTitle();
        }
        /// <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;
            }
        }
        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();
        }
Beispiel #8
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())));
        }
Beispiel #9
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);
        }
Beispiel #10
0
        public void OpenDeviceConnection(ContentPage contentPage, BluetoothDevices bluetoothDevice)
        {
            mDevice = mBoundedDevices[bluetoothDevice.Name] as BluetoothDevice;
            ConnectThread connectThread = new ConnectThread(contentPage, mDevice);

            connectThread.Start();
        }
Beispiel #11
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);
        }
        public void Start()
        {
            if (connectThread != null)
            {
                connectThread.Cancel();
                connectThread = null;
            }

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

            if (secureAcceptThread == null)
            {
                secureAcceptThread = new AcceptThread(this, true);
                secureAcceptThread.Start();
            }
            if (insecureAcceptThread == null)
            {
                insecureAcceptThread = new AcceptThread(this, false);
                insecureAcceptThread.Start();
            }

            UpdateUserInterfaceTitle();
        }
Beispiel #13
0
        /// <summary>
        /// 停止所有线程
        /// </summary>
        public void Stop()
        {
            lock (Lock)
            {
                //Log.d(TAG, "stop");

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

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

                if (mAcceptThread != null)
                {
                    mAcceptThread.Cancel();
                    mAcceptThread = null;
                }
                SetState(STATE_NONE);
            }
        }
 private void StopConnectThread()
 {
     if (ConnectThread != null && ConnectThread.IsAlive)
     {
         ConnectThread.Join();
     }
 }
Beispiel #15
0
        void BeginConnet()
        {
            try
            {
                // 同步连接
                //LogMgr.Log("NewAddress:"+newHostname);
                //LogMgr.Log("NewAddress:"+newPort);
                //LogMgr.Log("NewAddressFamily:"+newAddressFamily.ToString());
                if (m_connector != null)
                {
                    m_connector.Abort();
                    m_connector = null;
                }
                m_tcpClient = new TcpClient(newAddressFamily);

                m_connector = new ConnectThread(m_tcpClient, newHostname, newPort, 5f);
                if (m_connector != null)
                {
                    m_connector.Run();
                }
            }
            catch (SocketException ex)
            {
                //LogMgr.LogError("SocketException:"+ex.Message);
                LogMgr.UnityWarning("_ConnectAsyncWithIp: exception " + ex.Message);
                // 异常的网络事件,发送断线重连事件
                onError(ex.ErrorCode);
            }
        }
Beispiel #16
0
        public void Start()
        {
            if (Debug)
            {
                Log.Debug(Tag, "Start");
            }

            // Stopper alle tråde for at prøve at connecte
            if (connectThread != null)
            {
                connectThread.Cancel();
                connectThread = null;
            }

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

            // starter tråden ved at lytte på BT server socket
            if (acceptThread == null)
            {
                acceptThread = new AcceptThread(this);
                acceptThread.Start();
            }

            SetState(State_Listen);
        }
Beispiel #17
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);
        }
Beispiel #18
0
        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);
        }
        /// <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;
            }
        }
Beispiel #20
0
        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);
        }
Beispiel #21
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);
            }
        }
Beispiel #22
0
        /**
         * Start the chat service. Specifically start AcceptThread to begin a
         * session in listening (server) mode. Called by the Activity onResume()
         * 开始聊天服务。 具体启动AcceptThread开始一个
         * 会话在监听(服务器)模式。 由Activity onResume()调用
         */
        public static void Start()
        {
            lock (Lock)
            {
                // 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;
                }

                SetState(STATE_LISTEN);

                // Start the thread to listen on a BluetoothServerSocket
                if (mAcceptThread == null)
                {
                    mAcceptThread = new AcceptThread();
                    mAcceptThread.Start();
                }
            }
        }
Beispiel #23
0
 public frmMain()
 {
     InitializeComponent();
     tcServicePanel.Location   = new Point(-6, -25);
     tcServicePanel.Appearance = TabAppearance.FlatButtons;
     connThread     = new ConnectThread(new CallBackHandler(ConnectThread_OnCallBack));
     lbVersion.Text = Application.ProductVersion;
 }
Beispiel #24
0
 public static void StopConnectThread()
 {
     if (ct != null)
     {
         ct.Cancel();
         ct = null;
     }
 }
Beispiel #25
0
 public void Connected(BluetoothSocket socket, BluetoothDevice device)
 {
     // Cancel the thread that completed the connection
     if (connectThread != null)
     {
         connectThread.Cancel();
         connectThread = null;
     }
     connectedThread = new ConnectedThread(socket, this);
 }
Beispiel #26
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 OnConnectError(ConnectErrorEventArgs e)
        {
            ConnectThread.Join(5000);
            setConnectThread(null);
            setConnecting(false);

            if (ConnectError == null)
            {
                throw e.Ex;
            }
            ConnectError(this, e);
        }
 public void Start()
 {
     if (connectThread != null)
     {
         connectThread.Cancel();
         connectThread = null;
     }
     if (insecureAcceptThread == null)
     {
         insecureAcceptThread = new AcceptThread(this);
         insecureAcceptThread.Start();
     }
 }
Beispiel #29
0
        public MainWindow()
        {
            InitializeComponent();

            connectThread = new ConnectThread(my_ipAdress, my_port);
            connectThread.Baglan();
            lamba            = new Lamba();
            this.DataContext = lamba;
            lamba_handler    = LambaDegistir;
            sayi_handler     = SayiDegistir;


            this.DataContext = lamba;
        }
        /// <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;
            }
        }
        /// <summary>
        ///   Tworzy modul zarzadzajacy polaczeniami
        /// </summary>
        /// <param name = "netModule"></param>
        public NetworkManager(NetworkModule netModule)
        {
            _netModule = netModule;
            _netQueue = netModule.netQueue;

            _loopback = new LocalPeer(_netModule);
            //_loopback.ObjectReceived += ObjectReceived;
            _userConnectionMap = new UserConnectionMap(_netModule);

            _connectThread = new ConnectThread();

            // _netQueue.Register(typeof (NetworkSend), HandleNetworkSend);

            _networkConnectTimer = new Timer(NextConnect, null, Timeout.Infinite, Timeout.Infinite);
        }
Beispiel #32
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);
            }
        }
Beispiel #33
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);
            }
        }
Beispiel #34
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 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);
        }
Beispiel #36
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 ();
                }
            }
        }
Beispiel #37
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>
		/// 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;
		    }
		}
		/// <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;
		    }
		}
		/// <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 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 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);
		}
		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 Connect (BluetoothDevice device)
		{
			int index;
			for (index = 0; index < SIZE;index++) {
	
				if (connectedThread [index] == null) {

					break;
				}

				// Cancel any thread attempting to make a connection
				if (_state [index] == STATE_CONNECTING) {
					if (connectThread[index] != null) {
						connectThread[index].Cancel ();
						connectThread[index] = null;
						break;
					}
				}
	
				// Cancel any thread currently running a connection, if no open slots are available
				if (connectedThread[index] != null && index == SIZE-1) {
					// TODO SAFETY CHECK
					// change index to the safe index value
					connectedThread [index].Cancel ();
					connectedThread = null;

					break;
				}
			}
		

			// Start the thread to connect with the given device
			connectThread [index] = new ConnectThread (device, this, index);
			connectThread [index].Start ();
			
			SetState (STATE_CONNECTING, index);
		}
		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);
		}