Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import lejos.pc.comm.NXTComm;
- import lejos.pc.comm.NXTCommException;
- import lejos.pc.comm.NXTCommFactory;
- import lejos.pc.comm.NXTInfo;
- public class bluetooth {
- NXTComm nxtComm;
- InputStream is;
- OutputStream os;
- DataOutputStream dos;
- DataInputStream dis;
- public bluetooth(){
- }
- public void connect(String deviceName, String blueAdr){
- try {
- nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
- } catch (NXTCommException e) {
- System.out.println("Failed to load Bluetooth driver");
- //System.exit(1);
- }
- is = nxtComm.getInputStream();
- os = nxtComm.getOutputStream();
- dos = new DataOutputStream(os);
- dis = new DataInputStream(is);
- NXTInfo[] nxtInfo = new NXTInfo[1];
- nxtInfo[0] = new NXTInfo(deviceName, blueAdr);
- System.out.println("Connecting to " + nxtInfo[0].btResourceString);
- boolean opened = false;
- try {
- opened = nxtComm.open(nxtInfo[0]);
- } catch (NXTCommException e) {
- System.out.println("Exception from open");
- }
- if (!opened) {
- System.out.println("Failed to open " + nxtInfo[0].name);
- //System.exit(1);
- }
- System.out.println("Connected to " + nxtInfo[0].btResourceString);
- }
- public myMeasurements exchangeData(int turnDeg, int drive) {
- int dx = 0, dy = 0, dt = 0, s1 = 0, s2 = 0, s3 = 0;
- try {
- System.out.println("Sending: turn " + turnDeg + " degrees and drive " + drive + " mm.");
- dos.writeInt(turnDeg);
- dos.flush();
- dos.writeInt(drive);
- dos.flush();
- } catch (IOException ioe) {
- System.out.println("IO Exception writing bytes:");
- System.out.println(ioe.getMessage());
- System.exit(1);
- }
- try {
- dx = dis.readInt();
- dy = dis.readInt();
- dt = dis.readInt();
- s1 = dis.readInt();
- s2 = dis.readInt();
- s3 = dis.readInt();
- System.out.println("Received: dx " + dx + ", dy " + dy + ", dt " + dt + ", s1 " + s1 + ", s2 " + s2 + ", s3 " + s3);
- } catch (IOException ioe) {
- System.out.println("IO Exception reading bytes:");
- System.out.println(ioe.getMessage());
- System.exit(1);
- }
- return new myMeasurements(dx, dy, dt, s1, s2, s3);
- }
- public void close()
- {
- try {
- System.out.println("Disconnecting bluetooth");
- dis.close();
- dos.close();
- nxtComm.close();
- } catch (IOException ioe) {
- System.out.println("IOException closing connection:");
- System.out.println(ioe.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement