Advertisement
Guest User

Henrik Klaus Martin

a guest
Jan 16th, 2009
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. import lejos.pc.comm.NXTComm;
  8. import lejos.pc.comm.NXTCommException;
  9. import lejos.pc.comm.NXTCommFactory;
  10. import lejos.pc.comm.NXTInfo;
  11.  
  12.  
  13. public class bluetooth {
  14.     NXTComm nxtComm;
  15.     InputStream is;
  16.     OutputStream os;
  17.    
  18.     DataOutputStream dos;
  19.     DataInputStream dis;
  20.    
  21.    
  22.     public bluetooth(){
  23.     }
  24.    
  25.     public void connect(String deviceName, String blueAdr){    
  26.        
  27.         try {
  28.             nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  29.         } catch (NXTCommException e) {
  30.             System.out.println("Failed to load Bluetooth driver");
  31.             //System.exit(1);
  32.         }
  33.        
  34.         is = nxtComm.getInputStream();
  35.         os = nxtComm.getOutputStream();
  36.        
  37.         dos = new DataOutputStream(os);
  38.         dis = new DataInputStream(is);
  39.        
  40.         NXTInfo[] nxtInfo = new NXTInfo[1];
  41.            
  42.         nxtInfo[0] = new NXTInfo(deviceName, blueAdr);
  43.        
  44.         System.out.println("Connecting to " + nxtInfo[0].btResourceString);
  45.  
  46.         boolean opened = false;
  47.        
  48.         try {
  49.             opened = nxtComm.open(nxtInfo[0]);
  50.         } catch (NXTCommException e) {
  51.             System.out.println("Exception from open");
  52.         }
  53.        
  54.         if (!opened) {
  55.             System.out.println("Failed to open " + nxtInfo[0].name);
  56.             //System.exit(1);
  57.         }
  58.        
  59.         System.out.println("Connected to " + nxtInfo[0].btResourceString);
  60.     }
  61.    
  62.     public myMeasurements exchangeData(int turnDeg, int drive) {
  63.         int dx = 0, dy = 0, dt = 0, s1 = 0, s2 = 0, s3 = 0;
  64.        
  65.         try {
  66.             System.out.println("Sending: turn " + turnDeg + " degrees and drive " + drive + " mm.");
  67.             dos.writeInt(turnDeg);
  68.             dos.flush();
  69.             dos.writeInt(drive);
  70.             dos.flush();           
  71.  
  72.         } catch (IOException ioe) {
  73.             System.out.println("IO Exception writing bytes:");
  74.             System.out.println(ioe.getMessage());
  75.             System.exit(1);
  76.         }
  77.  
  78.         try {
  79.             dx = dis.readInt();
  80.             dy = dis.readInt();
  81.             dt = dis.readInt();
  82.             s1 = dis.readInt();
  83.             s2 = dis.readInt();
  84.             s3 = dis.readInt();
  85.             System.out.println("Received: dx " + dx + ", dy " + dy + ", dt " + dt + ", s1 " + s1 + ", s2 " + s2 + ", s3 " + s3);
  86.         } catch (IOException ioe) {
  87.             System.out.println("IO Exception reading bytes:");
  88.             System.out.println(ioe.getMessage());
  89.             System.exit(1);
  90.         }
  91.  
  92.         return new myMeasurements(dx, dy, dt, s1, s2, s3);
  93.     }
  94.    
  95.     public void close()
  96.     {
  97.         try {
  98.             System.out.println("Disconnecting bluetooth");
  99.             dis.close();
  100.             dos.close();
  101.             nxtComm.close();
  102.         } catch (IOException ioe) {
  103.             System.out.println("IOException closing connection:");
  104.             System.out.println(ioe.getMessage());
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement