|
发表于 2012-6-13 08:27:03
|
显示全部楼层
本帖最后由 yining101 于 2012-6-13 08:29 编辑
import java.io.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;
class Sample14_1
{
public static void main(String args[])
{
Button.ESCAPE.addButtonListener(new ButtonListener() {
public void buttonPressed(Button b) {System.exit(0);}
public void buttonReleased(Button b) {}
});
int STOP = 0, FWD = 1, BACK = 2, LEFT = 3, RIGHT = 4, cmd; //??㏑?絏
LCD.drawString("Waiting...", 0, 0);
NXTConnection connection = Bluetooth.waitForConnection(); //单?硈絬
DataInputStream dataIn = connection.openDataInputStream();//?眔块?﹃瑈
LCD.drawString("Connected", 0, 1);
try{
while(true)
{
cmd = dataIn.readInt(); //钡Μ㏑?
if(cmd==STOP) //停
{
Motor.B.stop();
Motor.C.stop();
}//if
if(cmd==FWD) //前进
{
Motor.B.forward();
Motor.C.forward();
}//if
if(cmd==BACK) //后退 {
Motor.B.backward();
Motor.C.backward();
}//if
if(cmd==LEFT) //左 {
Motor.B.backward();
Motor.C.forward();
}//if
if(cmd==RIGHT) //右
{
Motor.B.forward();
Motor.C.backward();
}//if
}//while
} catch(IOException e) {System.exit(1);}
}//main
}//Sample14_1
先定义cmd int stop=0,fwd=1,back=2,left=3,right=4cmd;
接着等待蓝牙连接
lcd.drawstring("Waiting...",0,0);
NXTConnection connection = Bluetooth.waitForConnection(); //单?硈絬
DataInputStream dataIn = connection.openDataInputStream();//?眔块?﹃瑈
LCD.drawString("Connected", 0,1);
在进行处理命令............上面有完整例子希望有用 |
|