Page 1 of 1

[Lejos] Bluetooth Communication Problems

Posted: 18 Dec 2013, 10:18
by xani92
Hi,

I've got the problem that I can send data via Bluetooth from the PC to the NXT but not from the NXT to the PC.
I wrote a little programm to describe my problem:

NXT:

Code: Select all

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.comm.NXTConnection;

public class BluetoothTest {

	public static void main(String[] args) {

		LCD.drawString("waiting for BT", 0, 0);
		BTConnection btc = Bluetooth.waitForConnection(10000,NXTConnection.RAW);
		LCD.clear();
		LCD.drawString("connected", 0, 0);
		DataInputStream dis = btc.openDataInputStream();
		DataOutputStream dos = btc.openDataOutputStream();
		LCD.drawString("read...", 0, 3);
		int i=-2;
		try {
			i = dis.readInt();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			dos.writeInt(i);
			dos.flush();
		} catch (IOException e){
			e.printStackTrace();
		}
		LCD.drawString("send", 0, 4);
		
		Button.waitForAnyPress();

	}

}
PC:

Code: Select all

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.pc.comm.NXTConnector;

public class BluetoothTest {

	public static void main(String[] args) {
		System.out.println("Connecting");
		NXTConnector conn = new NXTConnector();
		if (!conn.connectTo("btspp://"))
			System.out.println("Connection failed");
		DataInputStream dis = new DataInputStream(conn.getInputStream());
		DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
		System.out.println("send...");
		try {
			dos.writeInt(1234);
			dos.flush();
		} catch (IOException e) {
			System.out.println("error while sending");
			e.printStackTrace();
		}
		System.out.println("read...");
		try {
			int i = dis.readInt();
			System.out.println(i);
		} catch (IOException e) {
			System.out.println("error while reading");
			e.printStackTrace();
		}

	}

}
The NXT recieves the integer "1234" and sends it back but the PC doesn't recieve anything. It's waiting for input via bluetooth.

Can anybody help me please? :)

Thank you.

Re: [Lejos] Bluetooth Communication Problems

Posted: 18 Dec 2013, 14:38
by gloomyandy
Don't use a RAW connection on the NXT. Just use the default connection type (which is PACKET). leJOS Bluetooth PACKET mode streams have headers added to them and the only connection types supported by the leJOS PC side classes have this header. RAW mode is only really used when talking to non leJOS Bluetooth devices (like GPS etc.).

Re: [Lejos] Bluetooth Communication Problems

Posted: 18 Dec 2013, 14:47
by xani92
OK, I'll try it .

Thank you!