// Path: IOIO/iOIOLibAndroid/src/main/java/ioio/lib/api/IOIOConnection.java
// public interface IOIOConnection {
//     void waitForConnect() throws ConnectionLostException;
// 
//     void disconnect();
// 
//     InputStream getInputStream() throws ConnectionLostException;
// 
//     OutputStream getOutputStream() throws ConnectionLostException;
// 
//     /**
//      * Can this connection be closed. Normally the answer would be "true", but
//      * some weird connections cannot be closed and need the higher layer to do
//      * a "soft close" instead.
//      *
//      * @return true This connection can be closed.
//      */
//     boolean canClose();
// }
// 
// Path: IOIO/iOIOLibAndroid/src/main/java/ioio/lib/impl/FixedReadBufferedInputStream.java
// public class FixedReadBufferedInputStream extends InputStream {
//     private int bufferIndex_ = 0;
//     private int validData_ = 0;
//     private final byte[] buffer_;
//     private final InputStream source_;
// 
//     public FixedReadBufferedInputStream(InputStream source, int size) {
//         buffer_ = new byte[size];
//         source_ = source;
//     }
// 
//     @Override
//     public int available() throws IOException {
//         return validData_ - bufferIndex_;
//     }
// 
//     @Override
//     public void close() throws IOException {
//         source_.close();
//     }
// 
//     @Override
//     public int read(byte[] buffer, int offset, int length) throws IOException {
//         fillIfEmpty();
//         if (validData_ == -1) {
//             return -1;
//         }
//         length = Math.min(length, validData_ - bufferIndex_);
//         System.arraycopy(buffer_, bufferIndex_, buffer, offset, length);
//         bufferIndex_ += length;
//         return length;
//     }
// 
//     @Override
//     public int read(byte[] buffer) throws IOException {
//         return read(buffer, 0, buffer.length);
//     }
// 
//     @Override
//     public int read() throws IOException {
//         fillIfEmpty();
//         if (validData_ == -1) {
//             return -1;
//         }
//         return buffer_[bufferIndex_++] & 0xFF;
//     }
// 
//     @Override
//     public long skip(long byteCount) throws IOException {
//         long skipped = 0;
//         while (byteCount > 0) {
//             fillIfEmpty();
//             if (validData_ == -1) {
//                 return skipped;
//             }
//             int count = (int) Math.min(available(), byteCount);
//             byteCount -= count;
//             bufferIndex_ += count;
//             skipped += count;
//         }
//         return skipped;
//     }
// 
//     private void fillIfEmpty() throws IOException {
//         while (available() == 0 && validData_ != -1) {
//             fill();
//         }
//     }
// 
//     private void fill() throws IOException {
//         bufferIndex_ = 0;
//         validData_ = source_.read(buffer_);
//     }
// }


import android.os.Build;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import ioio.lib.api.IOIOConnection;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.impl.FixedReadBufferedInputStream;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
            if (disconnect_) {
                throw new ConnectionLostException();
            }
            try {
                socket_ = createSocket(device_);
            } catch (IOException e) {
                throw new ConnectionLostException(e);
            }
        }
        // keep trying to connect as long as we're not aborting
        while (true) {
            try {
                Log.v(TAG, "Attempting to connect to Bluetooth device: " + name_);
                inputStream_ = socket_.getInputStream();
                outputStream_ = socket_.getOutputStream();
                socket_.connect();
                Log.v(TAG, "Established connection to device " + name_
                        + " address: " + address_);
                break; // if we got here, we're connected
            } catch (Exception e) {
                if (disconnect_) {
                    throw new ConnectionLostException(e);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
            }
        }
        // Success! Wrap the streams with a properly sized buffers.
// Path: IOIO/iOIOLibAndroid/src/main/java/ioio/lib/api/IOIOConnection.java
// public interface IOIOConnection {
//     void waitForConnect() throws ConnectionLostException;
// 
//     void disconnect();
// 
//     InputStream getInputStream() throws ConnectionLostException;
// 
//     OutputStream getOutputStream() throws ConnectionLostException;
// 
//     /**
//      * Can this connection be closed. Normally the answer would be "true", but
//      * some weird connections cannot be closed and need the higher layer to do
//      * a "soft close" instead.
//      *
//      * @return true This connection can be closed.
//      */
//     boolean canClose();
// }
// 
// Path: IOIO/iOIOLibAndroid/src/main/java/ioio/lib/impl/FixedReadBufferedInputStream.java
// public class FixedReadBufferedInputStream extends InputStream {
//     private int bufferIndex_ = 0;
//     private int validData_ = 0;
//     private final byte[] buffer_;
//     private final InputStream source_;
// 
//     public FixedReadBufferedInputStream(InputStream source, int size) {
//         buffer_ = new byte[size];
//         source_ = source;
//     }
// 
//     @Override
//     public int available() throws IOException {
//         return validData_ - bufferIndex_;
//     }
// 
//     @Override
//     public void close() throws IOException {
//         source_.close();
//     }
// 
//     @Override
//     public int read(byte[] buffer, int offset, int length) throws IOException {
//         fillIfEmpty();
//         if (validData_ == -1) {
//             return -1;
//         }
//         length = Math.min(length, validData_ - bufferIndex_);
//         System.arraycopy(buffer_, bufferIndex_, buffer, offset, length);
//         bufferIndex_ += length;
//         return length;
//     }
// 
//     @Override
//     public int read(byte[] buffer) throws IOException {
//         return read(buffer, 0, buffer.length);
//     }
// 
//     @Override
//     public int read() throws IOException {
//         fillIfEmpty();
//         if (validData_ == -1) {
//             return -1;
//         }
//         return buffer_[bufferIndex_++] & 0xFF;
//     }
// 
//     @Override
//     public long skip(long byteCount) throws IOException {
//         long skipped = 0;
//         while (byteCount > 0) {
//             fillIfEmpty();
//             if (validData_ == -1) {
//                 return skipped;
//             }
//             int count = (int) Math.min(available(), byteCount);
//             byteCount -= count;
//             bufferIndex_ += count;
//             skipped += count;
//         }
//         return skipped;
//     }
// 
//     private void fillIfEmpty() throws IOException {
//         while (available() == 0 && validData_ != -1) {
//             fill();
//         }
//     }
// 
//     private void fill() throws IOException {
//         bufferIndex_ = 0;
//         validData_ = source_.read(buffer_);
//     }
// }


// Path: IOIO/iOIOLibBT/src/main/java/ioio/lib/android/bluetooth/BluetoothIOIOConnection.java
import android.os.Build;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import ioio.lib.api.IOIOConnection;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.impl.FixedReadBufferedInputStream;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

            if (disconnect_) {
                throw new ConnectionLostException();
            }
            try {
                socket_ = createSocket(device_);
            } catch (IOException e) {
                throw new ConnectionLostException(e);
            }
        }
        // keep trying to connect as long as we're not aborting
        while (true) {
            try {
                Log.v(TAG, "Attempting to connect to Bluetooth device: " + name_);
                inputStream_ = socket_.getInputStream();
                outputStream_ = socket_.getOutputStream();
                socket_.connect();
                Log.v(TAG, "Established connection to device " + name_
                        + " address: " + address_);
                break; // if we got here, we're connected
            } catch (Exception e) {
                if (disconnect_) {
                    throw new ConnectionLostException(e);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                }
            }
        }
        // Success! Wrap the streams with a properly sized buffers.
        inputStream_ = new FixedReadBufferedInputStream(inputStream_, 64);