public override bool Initialize() { try { this._Buffer = new CircularBuffer(this._Buffer._Bytes.Length); this.head = 0; this._SBuffer = new CircularBuffer(SEND_BUFFER_SIZE); //Always set the transmission mode on connection for (int i = 0; (i < this._OnConnectionCommands.Count); i++) Write(((Command)this._OnConnectionCommands[i])._Bytes); this._OnConnectionCommands.Clear(); Write(new SET_VTM(this._TMode)._Bytes); //if (CurrentWockets._Configuration._SoftwareMode == Wockets.Data.Configuration.SoftwareConfiguration.DEBUG) // Logger.Debug("RFCOMMReceiver: Initialize: Attempting reconnection for receiver " + this._Address); this.bluetoothStream = NetworkStacks._BluetoothStack.Connect(this._Buffer,this._SBuffer , this.address_bytes, this.pin); if (this.bluetoothStream == null) return false; if (this._TMode== TransmissionMode.Bursty60) this.bluetoothStream._Timeout = 2; this.bluetoothStream._TimeoutEnabled = this._TimeoutEnabled; this._ConnectionTime = this.bluetoothStream._ConnectionTime; this._CurrentConnectionUnixTime = this.bluetoothStream._CurrentConnectionUnixTime; this._SuccessfulConnections++; return true; } catch (Exception e) { return false; } }
public override void Update() { lock (this) { /// If a disconnection is detected for the bluetooth stream, update the status of the /// receiver and flush the send buffer and delete references to the bluetooth stream if ((this.bluetoothStream != null) && (this.bluetoothStream._Status == BluetoothStatus.Disconnected)) { this.bluetoothStream = null; this.status = ReceiverStatus.Disconnected; this._SBuffer._Head = 0; this.ndisc++; this.disconnectionTime = WocketsTimer.GetUnixTime(); } // If the bluetooth stream is null or the receiver is not reconnecting // then instantiate a thread to reconnect if ((this.bluetoothStream == null) && (this.status != ReceiverStatus.Reconnecting)) { this.status = ReceiverStatus.Reconnecting; reconnectionThread = new Thread(new ThreadStart(this.Reconnect)); reconnectionThread.Start(); //if (CurrentWockets._Configuration._SoftwareMode == SoftwareConfiguration.DEBUG) // Logger.Debug("RFCOMMReceiver: Update: Spawning a reconnection thread for "+ this._Address); } if ((this.status != ReceiverStatus.Connected) && (this.bluetoothStream != null) && (this.bluetoothStream._Status == BluetoothStatus.Connected)) { if (this.status == ReceiverStatus.Reconnecting) { reconnectionThread.Join(); reconnectionThread.Abort(); reconnectionThread = null; } //if (CurrentWockets._Configuration._SoftwareMode == SoftwareConfiguration.DEBUG) // Logger.Debug("RFCOMMReceiver: Update: Reconnection successful for "+ this._Address); this.status = ReceiverStatus.Connected; if (this.disconnectionTime!=0) this.disconTime += (int)((WocketsTimer.GetUnixTime() - this.disconnectionTime) / 1000); } } }
public override bool Dispose() { try { if (this.reconnectionThread != null) this.reconnectionThread.Abort(); } catch { } finally { this.reconnectionThread = null; } try { if (this.bluetoothStream != null) this.bluetoothStream._Status = BluetoothStatus.Disconnected; } catch { } finally { this.bluetoothStream = null; } try{ this._Status = ReceiverStatus.Disconnected; this._Reconnections = 0; this._SuccessfulConnections = 0; return true; } catch (Exception) { return false; } }