All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lejos.remote.nxt.BTConnection Maven / Gradle / Ivy

Go to download

leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.

The newest version!
package lejos.remote.nxt;

import java.io.IOException;

import com.sun.jna.LastErrorException;

import lejos.internal.io.NativeSocket;

public class BTConnection extends NXTConnection {
	NativeSocket socket;
	int mode;
	byte[] header = new byte[2];
	byte[] buffer = new byte[256];
	boolean eof = false;
	
	BTConnection(NativeSocket socket, int mode) {
		this.socket = socket;
		this.mode = mode;
		//System.out.println("Mode is " + mode);
	}

	@Override
	public void close() throws IOException {
		socket.close();	
	}

	// TODO : Need robust versions of read and write
	@Override
	public int read(byte[] buf, int length) {
		if (eof) return -1;
		try {
			//System.out.println("Reading ...");
			if (mode != NXTConnection.RAW) {
				int len = socket.read(buffer, length);
				//System.out.println("Read " + len + " bytes");
				if (len > 2) {
					for(int i=0;i> 8);
			socket.write(header,0,2);
		}
		socket.write(buffer, 0, numBytes);
		return 0;
	}

	@Override
	public int read(byte[] buf, int length, boolean b) {
		if (eof) return -1;
		try {
			//System.out.println("Reading  with wait = " + b);
			if (mode != NXTConnection.RAW) {
				int len = socket.read(buffer, length);
				//System.out.println("Read " + len + " bytes");
				if (len > 2) {
					for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy