/// <summary> /// A static method that opens a connection to a Bluetooth serial service and spawns a processing thread /// </summary> /// <param name="buffer">Handle to a circular receive buffer</param> /// <param name="sbuffer">Handle to a circular send buffer</param> /// <param name="address">A machine independent (Little or Big Endian) byte array of the bluetooth address</param> /// <param name="pin">A pin for the remote bluetooth device</param> /// <returns>A BluetoothStream object on success, otherwise a null</returns> public static BluetoothStream Open(CircularBuffer buffer, CircularBuffer sbuffer, byte[] address, string pin) { //Initialize the Bluetooth stack if (!NetworkStacks._BluetoothStack.Initialize()) { return(null); } try { // Initialize the microsoft bluetooth stream MicrosoftBluetoothStream btStream = new MicrosoftBluetoothStream(buffer, sbuffer, address, pin); btStream._Status = BluetoothStatus.Reconnecting; // Critical section: Allow one connection at a time lock (mylock) { btStream.socket = new Socket(BluetoothStream._AddressFamily, SocketType.Stream, BluetoothStream._ProtocolType); //btStream.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout,10000); btStream.socket.Blocking = true; btStream._ConnectionTime = WocketsTimer.GetUnixTime(DateTime.Now); btStream.socket.Connect(btStream._RemoteEP); btStream._CurrentConnectionUnixTime = WocketsTimer.GetUnixTime(DateTime.Now); btStream._ConnectionTime = btStream._CurrentConnectionUnixTime - btStream._ConnectionTime; btStream.nstream = new NetworkStream(btStream.socket, true); } // Spawn the processing thread btStream.processingThread = new Thread(new ThreadStart(btStream.Process)); btStream.processingThread.Priority = ThreadPriority.AboveNormal; btStream.processingThread.Start(); //if (CurrentWockets._Configuration._SoftwareMode == SoftwareConfiguration.DEBUG) // Logger.Debug("MicrosoftBluetoothStream: Open: Successful connection to" + btStream._HexAddress + "."); return(btStream); } catch (Exception e) { // Exception failed to connect CurrentWockets._LastError = ErrorCodes.CONNECTION_FAILED_TO_OPEN; string hex = ""; for (int i = 0; i < address.Length; i++) { hex += address[i].ToString("X2"); } CurrentWockets._LastErrorMessage = "Failed to open bluetooth connection to " + hex + ". " + e.ToString(); Logger.Error("MicrosoftBluetoothStream: Open: <" + ErrorCodes.CONNECTION_FAILED_TO_OPEN + ">: Failed to open connection to " + hex + ". " + e.ToString()); return(null); } }
/// <summary> /// Open a serial connection to a remote device /// </summary> /// <param name="buffer">A handle to a circular receive buffer</param> /// <param name="sbuffer">A handle to a circular send buffer</param> /// <param name="address">A machine independent address of the remote bluetooth device</param> /// <param name="pin">A pin for the remote bluetooth device</param> /// <returns>A bluetooth stream on success, otherwise null</returns> public override BluetoothStream Connect(CircularBuffer buffer, CircularBuffer sbuffer, byte[] address, string pin) { try { lock (this) { return(MicrosoftBluetoothStream.Open(buffer, sbuffer, address, pin)); } } catch (Exception) { return(null); } }