-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a class to remotely control a Lego Mindstorms NXT
- Loading branch information
Miguel Duarte
committed
Nov 24, 2012
1 parent
b1375ba
commit 526da43
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package client; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import lejos.pc.comm.NXTComm; | ||
import lejos.pc.comm.NXTCommFactory; | ||
import lejos.pc.comm.NXTInfo; | ||
|
||
public class LegoConnection extends Thread{ | ||
private boolean BT = true; | ||
public boolean sendCommand = false; | ||
|
||
DataOutputStream output; | ||
DataInputStream input; | ||
boolean connected = false; | ||
|
||
public void send(int n) { | ||
if (connected) | ||
try { | ||
output.writeInt(n); | ||
output.flush(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void connect() { | ||
// NXTInfo nxtCtrl = new NXTInfo(); | ||
// NXTConnector conn = new NXTConnector(); | ||
|
||
if (BT) { | ||
try { | ||
NXTComm com = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH); | ||
NXTInfo nxtInfo = new NXTInfo(NXTCommFactory.BLUETOOTH, "NXT", "00:16:53:0e:3a:b4"); | ||
//NXTComm com = new NXTCommBluecove(); | ||
com.open(nxtInfo); | ||
System.out.println("Connection established!"); | ||
output = new DataOutputStream(com.getOutputStream()); | ||
input = new DataInputStream(com.getInputStream()); | ||
connected = true; | ||
} catch (Exception e) { | ||
System.err.println("Could not establish connection."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
connect(); | ||
try { | ||
while(true) { | ||
if(sendCommand) { | ||
output.write(1); | ||
output.flush(); | ||
sendCommand = false; | ||
} | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void stopSending() { | ||
try { | ||
output.close(); | ||
input.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |