Moderators: roger, 99jonathan, imaqine
import lejos.pc.comm.*;
import java.io.*;
/**
* This is a PC sample. It connects to the NXT, and then
* sends an integer and waits for a reply, 100 times.
*
* Compile this program with javac (not nxjc), and run it
* with java.
*
* You need pccomm.jar and bluecove.jar on the CLASSPATH.
* On Linux, you will also need bluecove-gpl.jar on the CLASSPATH.
*
* Run the program by:
*
* java BTSend
*
* Your NXT should be running a sample such as BTReceive or
* SignalTest. Run the NXT program first until it is
* waiting for a connection, and then run the PC program.
*
* @author Lawrie Griffiths
*
*/
public class BTSend {
public static void main(String[] args) {
NXTConnector conn = new NXTConnector();
conn.addLogListener(new NXTCommLogListener(){
public void logEvent(String message) {
System.out.println("BTSend Log.listener: "+message);
}
public void logEvent(Throwable throwable) {
System.out.println("BTSend Log.listener - stack trace: ");
throwable.printStackTrace();
}
}
);
// Connect to any NXT over Bluetooth
boolean connected = conn.connectTo("btspp://");
if (!connected) {
System.err.println("Failed to connect to any NXT");
System.exit(1);
}
DataOutputStream dos = conn.getDataOut();
DataInputStream dis = conn.getDataIn();
for(int i=0;i<100;i++) {
try {
System.out.println("Sending " + (i*30000));
dos.writeInt((i*30000));
dos.flush();
} catch (IOException ioe) {
System.out.println("IO Exception writing bytes:");
System.out.println(ioe.getMessage());
break;
}
try {
System.out.println("Received " + dis.readInt());
} catch (IOException ioe) {
System.out.println("IO Exception reading bytes:");
System.out.println(ioe.getMessage());
break;
}
}
try {
dis.close();
dos.close();
conn.close();
} catch (IOException ioe) {
System.out.println("IOException closing connection:");
System.out.println(ioe.getMessage());
}
}
}
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
/**
* Receive data from another NXT, a PC, a phone,
* or another bluetooth device.
*
* Waits for a connection, receives an int and returns
* its negative as a reply, 100 times, and then closes
* the connection, and waits for a new one.
*
* @author Lawrie Griffiths
*
*/
public class BTReceive {
public static void main(String [] args) throws Exception
{
String connected = "Connected";
String waiting = "Waiting...";
String closing = "Closing...";
while (true)
{
LCD.drawString(waiting,0,0);
LCD.refresh();
BTConnection btc = Bluetooth.waitForConnection();
LCD.clear();
LCD.drawString(connected,0,0);
LCD.refresh();
DataInputStream dis = btc.openDataInputStream();
DataOutputStream dos = btc.openDataOutputStream();
for(int i=0;i<100;i++) {
int n = dis.readInt();
LCD.drawInt(n,7,0,1);
LCD.refresh();
dos.writeInt(-n);
dos.flush();
}
dis.close();
dos.close();
Thread.sleep(100); // wait for data to drain
LCD.clear();
LCD.drawString(closing,0,0);
LCD.refresh();
btc.close();
LCD.clear();
}
}
}
BTSend Log.listener: Failed to load Bluetooth comms driver: Cannot load Bluetooth driver
BTSend Log.listener: Failed to connect to any NXT
Failed to connect to any NXTException in thread "main" java.lang.ClassCastException: javax.microedition.io.Connector$1 cannot be cast to javax.microedition.io.StreamConnection
at lejos.pc.comm.NXTCommBluecove.open(NXTCommBluecove.java:120)
at lejos.pc.comm.NXTConnector.connectTo(NXTConnector.java:229)
at lejos.pc.comm.NXTConnector.connectTo(NXTConnector.java:328)
at lejos.pc.comm.NXTConnector.connectTo(NXTConnector.java:337)
at BTSend.main(BTSend.java:50) NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
NXTInfo[] nxtList = nxtComm.search("Fieral", NXTCommFactory.BLUETOOTH);
System.out.println(nxtList.length + " device(s) found");
for (int i = 0; i < nxtList.length; i++) {
System.out.println("Device found: ");
System.out.println("Name: " + nxtList[i].name);
System.out.println("Address: " + nxtList[i].deviceAddress);
}
NXTInfo nxtInfo = new NXTInfo(nxtList[0]);
nxtComm.open(nxtInfo);
...
...Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: lejos.nxt.NXT.getUserPages()I
at lejos.nxt.NXT.getUserPages(Native Method)
at lejos.nxt.Flash.<clinit>(Flash.java:18)
at lejos.nxt.SystemSettings.<clinit>(SystemSettings.java:30)
at lejos.nxt.comm.NXTCommDevice.loadSettings(NXTCommDevice.java:167)
at lejos.nxt.comm.NXTCommDevice.<clinit>(NXTCommDevice.java:20)
at javax.bluetooth.DiscoveryAgent$1.run(DiscoveryAgent.java:194)
Users browsing this forum: No registered users and 0 guests