public void Connect(int retries) { Guid serviceClass = BluetoothService.SerialPort; var ep = new BluetoothEndPoint(deviceInfo.DeviceAddress, serviceClass); NetworkStream strm = null; do { try { client.Connect(ep); strm = client.GetStream() as NetworkStream; } catch (SocketException) { // A socket exception indicates the connection has failed, retry a specified number of times } catch { this.sphero = null; return; } }while (retries-- > 0 && strm == null); // If strm is null, we have run out of retries if (strm == null) { this.sphero = null; return; } this.sphero = new Sphero(strm); }
public SpheroListener(Sphero sphero) { if (sphero == null || sphero.Stream == null) { throw new InvalidOperationException("Cannot attach listener to invalid sphero object"); } NetworkStream strm = sphero.Stream; if (strm.CanRead) { strm.BeginRead(readBuffer, 0, 1024, ReadCallback, strm); } }
public void Close() { if (sphero != null) { sphero.Close(); } if (client != null && client.Connected) { client.Close(); } this.deviceInfo = null; this.sphero = null; }