I am trying to understand the bluetooth infrastructure of lejos API.
I have two classes; one for my PC (That is the sample program of Lejo )
the other one is running on NXT..
I want to send data to NXT from my PC.
Here is the class working on my PC.
- Code: Select all
import java.io.*;
import lejos.pc.comm.*;
/**
* conneccts to a NXT using either Bluetooth or USB and builds input and output data streams.
*@author Roger Glassey 22/08/2007
*/
public class Connector
{
private boolean _usb = false;
DataInputStream dataIn;
DataOutputStream dataOut;
InputStream is;
OutputStream os;
NXTComm nxtComm;
/**
*
* @param NXT can be the friendly name of the NXT or a 16 character address
* @param useUSB
* @return true if connection was made
*/
public boolean startConnector(String NXT, boolean useUSB)
{
NXTInfo[] nxtInfo ;
_usb = useUSB;
if(_usb)
{
nxtComm = new NXTCommLibnxt();
System.out.println("searching");
nxtInfo = nxtComm.search(null, NXTCommFactory.USB);
if (nxtInfo.length == 0)
{
System.out.println("No NXT Found");
return false;
}
nxtComm.open(nxtInfo[0]);
System.out.println(" Opened "+nxtInfo[0].name);
}
else
{
nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
if(NXT == null || NXT == " ")
{
System.out.println("search for all");
nxtInfo = nxtComm.search(NXT, NXTCommFactory.BLUETOOTH);
}
else if(NXT.length()<8)
{
System.out.println("search for " +NXT);
nxtInfo = nxtComm.search(NXT, NXTCommFactory.BLUETOOTH);
}
else
{
nxtInfo = new NXTInfo[1];
nxtInfo[0] = new NXTInfo("unknown ",NXT);// NXT is actually address
}
if (nxtInfo.length == 0)
{
System.out.println("No NXT Found: is BT adatper on? is NXT on? ");
System.exit(1);
}
System.out.println("Connecting to " + nxtInfo[0].name+" "+nxtInfo[0].btDeviceAddress);
boolean opened = nxtComm.open(nxtInfo[0]);
if (!opened) {
System.out.println("Failed to open " + nxtInfo[0].name+" "+nxtInfo[0].btDeviceAddress);
System.exit(1);
}
System.out.println("Connected to " + nxtInfo[0].name);
}
is = nxtComm.getInputStream();
dataIn = new DataInputStream( nxtComm.getInputStream());
os = nxtComm.getOutputStream();
dataOut = new DataOutputStream(os);
return true;
}
/**
* @return the InputStream for this connection;
*/
public InputStream getInputStream(){return is;}
/**
* @return the DataInputStream for this connection;
*/
public DataInputStream getDataIn(){return dataIn;}
/**
* @return the OutputSteram for this connection;
*/
public OutputStream getOutputStream(){return os;}
/**
* @return the DataOutputStream for this connection
*/
public DataOutputStream getDataOut() {return dataOut;}
public static void main(String[] args)throws Exception
{
Connector con = new Connector();
con.startConnector("NXT",false);
while (true){
con.dataOut.write(new Double(Math.random()*5).intValue());
//con.os.write();
Thread.sleep(1000);
con.dataOut.flush();
}
}
}
and this is the class on my NXT
- Code: Select all
import java.io.DataInputStream;
import lejos.nxt.*;
import lejos.nxt.comm.*;
public class MyMaster {
/**
* @param args
*/
private static BTConnection conn = null;
private static DataInputStream dis = null;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
while (true){
if (conn==null){
conn = Bluetooth.waitForConnection();
if (conn!=null){
/*Baglanti Ok!*/
Sound.beep();
dis = new DataInputStream(conn.openInputStream());
}
}
if (dis!=null){
LCD.drawInt(dis.readInt(), 4, 2 );
Thread.sleep(1000);
LCD.refresh();
}
}
}
}
When i run those 2 class simultaneously the NXT beeps stating that BluetoothConnection is OK...
But there is no print in LCD
When I change the DataInputStreams to InputStreams I get peculiar data printing on LCD.
Can anyone guide what am I doing wrong?
Also how can we use the NXTSocket ? If you could send me information or code sample it will be great.Thanks a lot.
