package xavier.btkom; import android.bluetooth.BluetoothSocket; import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.io.OutputStream; import java.lang.reflect.Array; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.Random; import static android.R.attr.id; public class BTKOM{ private final int SIZE_MESSAGE = 255; private final int SIZE_HEADER = 48; private BluetoothSocket socket; private String senderName = "Android"; private ArrayList messages; private int caltoID; private int androidID; private boolean isSynchronizing = false; private OutputStream output; private InputStream input; private int available = 0; BTKOM(BluetoothSocket socket){ messages = new ArrayList(); this.androidID = new Random().nextInt(); try { socket.connect(); output = socket.getOutputStream(); input = socket.getInputStream(); //TODO generating id + SYN ACK listen(); connect(); }catch(IOException e){ throw new IllegalArgumentException("Error binding socket"); } } void listen(){ Thread listening = new Thread(new Runnable(){ int id; Message msg; String str; public void run(){ ArrayList packets = new ArrayList(); byte[] buffer = new byte[5120]; int bytes; while(!Thread.currentThread().isInterrupted()){ try { if(input.available() > 0 && input.available() == available){ bytes = input.read(buffer); int part = (buffer[44] << 8) + buffer[45]; int number = (buffer[46] << 8) + buffer[47]; if(part < number && part==1){ str = new String(buffer, 0, SIZE_MESSAGE); for(int i=1; i