[Lejos] Bluetooth Communication Problems

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
xani92
Posts: 2
Joined: 18 Dec 2013, 10:11

[Lejos] Bluetooth Communication Problems

Post 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.
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: [Lejos] Bluetooth Communication Problems

Post 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.).
xani92
Posts: 2
Joined: 18 Dec 2013, 10:11

Re: [Lejos] Bluetooth Communication Problems

Post by xani92 »

OK, I'll try it .

Thank you!
Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests