Another thread a bit like this, viewtopic.php?t=439.
We wanted to do a multithreaded instance of several bluetooth connections. First off, we're not even sure that lejOS accepts more than one bluetooth connection, but so far we're getting static.
How this code (should) work:
1. When main class (class Factory) initializes create a new thread (class ListenComm) that waits for any bluetooth connection.
2. When connection is made, create a new thread (class CommThread), that takes a BTConnection and then binds this thread to that BTConnection (i.e, the one that just connected)
3. In that new thread, whenever the client sends anything to the CommThread, then put that in to a QUEUE that out main class (class Factory) then reads.
But something does not work, because we create the connection and all the new thread fine. The thing is just that the chars sent from the client does not match what we're seing as output on the screen.
We have tried before with just a simple connection test and there we easily managed to send chars, but in this example it doesn't work.
Does anybody know anything or have any ideas to why this might be? Is it because we have multiple threads that sort of doesn't work well with the bluetooth connection? We had a suspicion that we might run into problems when connecting more devices, but the problem occurs with just one device connecting.
- Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.util.*;
import java.io.*;
public class Factory {
public static void main(String[] args) {
Factory f = new Factory();
f.start();
}
private final Motor CONVEYER_BELT = Motor.A;
private final Motor TOWER = Motor.B;
private Queue BUFFER;
public Factory() {
BUFFER = new Queue();
Thread listenForBTConns = new ListenComm();
listenForBTConns.setDaemon(true);
listenForBTConns.start();
}
public void start() {
int counter = 0;
while (true) {
counter++;
LCD.drawInt(counter, 0, 7);
LCD.refresh();
// if (!BUFFER.isEmpty()) {
// LCD.drawString((String)BUFFER.pop(), 0, 0);
// LCD.drawString("bleh", 0, 1);
// LCD.refresh();
// } else {
// }
try {
Thread.sleep(1000);
}
catch(Exception e) { }
}
}
private class CommThread extends Thread {
private DataInputStream in;
public CommThread(BTConnection conn) throws Exception {
super();
in = conn.openDataInputStream();
}
public void run() {
String chr_buff = "";
while (true) {
try {
LCD.drawString("read 1 ", 0, 2);
LCD.refresh();
char c = in.readChar();
LCD.drawString("read 2 [" + c + "]", 0, 2);
LCD.refresh();
Thread.sleep(500);
if (c == '\n') {
// BUFFER.push(chr_buff);
LCD.drawString(chr_buff, 0, 3);
LCD.refresh();
chr_buff = "";
} else {
chr_buff += c;
}
}
catch (Exception e) {
LCD.drawString("Exception 1", 0, 4);
LCD.refresh();
}
}
}
}
private class ListenComm extends Thread {
public void run() {
while (true) {
LCD.drawString("listen 1 ", 0, 1);
LCD.refresh();
BTConnection conn = Bluetooth.waitForConnection();
LCD.drawString("listen 2", 0, 1);
LCD.refresh();
try {
Thread.sleep(1000);
Thread thread = new CommThread(conn);
thread.setDaemon(true);
thread.start();
}
catch (Exception e) {
LCD.drawString("Exception 2", 0, 5);
LCD.refresh();
}
}
}
}
}
