public void initialize(byte[] addr, string pin) { this.bs = BluetoothStream.OpenConnection(addr, pin); }
/* private static void readingLoop() { List<BluetoothStream> streamsToRemove = new List<BluetoothStream>(); while (true) { foreach (BluetoothStream stream in openStreams) { if (usingWidcomm) { //TODO FIXME } else { if (stream.btClient.Connected) { int bytesReceived; try { bytesReceived = stream.btSocket.Receive(stream.singleReadBuffer); } catch (Exception e) { throw e; } //this is a timeout. If we get too many of them, we classify that //as a socket that has been disconnected if (bytesReceived == 0) { List<DateTime> newTimeouts = stream.timeoutTimestamps.FindAll(oldEnoughPredicate); newTimeouts.Add(DateTime.Now); if (newTimeouts.Count > MAX_TIMEOUTS) { stream.socketDead = true; streamsToRemove.Add(stream); } stream.timeoutTimestamps = newTimeouts; } lock (stream) { int currentBytes = stream.tail - stream.head; if (currentBytes < 0) currentBytes = DEFAULT_BUFFER_SIZE + currentBytes; int ii; for (ii = 0; ii < bytesReceived && ii < (DEFAULT_BUFFER_SIZE - currentBytes); ii++) { stream.localBuffer[stream.tail++] = stream.singleReadBuffer[ii]; stream.tail %= DEFAULT_BUFFER_SIZE; } if (ii == DEFAULT_BUFFER_SIZE - currentBytes && ii < bytesReceived) { while (ii < bytesReceived) { stream.localBuffer[stream.tail++] = stream.singleReadBuffer[ii++]; stream.head++; stream.tail %= DEFAULT_BUFFER_SIZE; stream.head %= DEFAULT_BUFFER_SIZE; } stream.head++; stream.head %= DEFAULT_BUFFER_SIZE; } } } } } foreach (BluetoothStream stream in streamsToRemove) { openStreams.Remove(stream); } streamsToRemove.Clear(); } } */ /// <summary> /// Opens a Bluetooth connection with the specified address and returns /// a BluetoothStream object which can be used to communicate over that /// connection /// </summary> /// <param name="addr">The MAC address of the remote bluetooth device. /// It <b>MUST</b> be in most-significant-byte first /// order (i.e. the bluetooth address 00:f1:ad:34:3d:f3 would be /// { 0x00, 0xf1, ...} and NOT {0xf3, 0x3d, ...})</param> /// <param name="pin">An optional pin for the bluetooth device</param> /// <returns></returns> public static BluetoothStream OpenConnection(byte[] addr, string pin) { BluetoothStream newStream = new BluetoothStream(); try { if (usingWidcomm) { bool canStart = initializeWidcommBluetooth(); if (!canStart) throw new Exception("Couldn't instantiate the Widcomm object in C++"); IntPtr stringPtr = prepareCOMportWidcomm(addr); if (stringPtr != IntPtr.Zero) newStream.comPortName = Marshal.PtrToStringUni(stringPtr); else throw new Exception("Got a null pointer from the WIDCOMM code"); //now open the port newStream.comPort = new SerialPort(newStream.comPortName); newStream.comPort.Open(); } else { newStream.btClient = new BluetoothClient(); byte[] reverseAddr = new byte[addr.Length]; for (int ii = 0; ii < addr.Length; ii++) { reverseAddr[reverseAddr.Length - 1 - ii] = addr[ii]; } newStream.timeoutTimestamps = new List<DateTime>(); newStream.localBuffer = new byte[DEFAULT_BUFFER_SIZE]; newStream.singleReadBuffer = new byte[DEFAULT_BUFFER_SIZE]; lock (lockObject) { BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable; BluetoothAddress bt_addr = new BluetoothAddress(reverseAddr); if (pin != null) BluetoothSecurity.SetPin(bt_addr, pin); newStream.btClient.Connect(bt_addr, BluetoothService.SerialPort); newStream.btSocket = newStream.btClient.Client; newStream.btSocket.Blocking = true; } } // if (newStream.readingThread != null) // newStream.readingThread.Abort(); // newStream.readingThread = new Thread(new ThreadStart(newStream.readingFunction)); //newStream.readingThread.Start(); } catch { newStream.disposed = true; throw; } return newStream; }