Created
April 23, 2011 03:41
-
-
Save a10y/938235 to your computer and use it in GitHub Desktop.
iOSAccelerometerReceiver.java
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
package com.iaroc.ios; | |
import java.net.DatagramSocket; | |
import java.net.DatagramPacket; | |
/* | |
* iOSAccelerometerReceiver: Class that handles a UDP connection to an | |
* iOS device and parses the data for accelerometer data. | |
* | |
* @author gerberduffy | |
*/ | |
public class iOSAccelerometerReceiver { | |
public static final int PORT = 10552; | |
private static DatagramSocket s; | |
private static double x,y,z; //X, Y, and Z values | |
public static void connect(int port){ | |
try { | |
s = new DatagramSocket(port); //Connect on the specified port | |
} | |
catch(Exception e){System.out.println("Connection Established"); System.exit(1);} | |
while(s.isBound()){ | |
try { //Thread.sleep(1000); | |
byte[] buf = new byte[256]; | |
DatagramPacket p = new DatagramPacket(buf, buf.length); | |
s.receive(p); | |
String[] outputs = new String(p.getData()).split(","); | |
x = Double.parseDouble(outputs[2]); | |
y = Double.parseDouble(outputs[3]); | |
z = Double.parseDouble(outputs[4]); | |
System.out.println("X: " + x); | |
System.out.println("Y: " + y); | |
System.out.println("Z: " + z); | |
} catch(Exception e){ | |
System.out.println("Something failed"); | |
} | |
} | |
} | |
public double getX(){ return x; } | |
public double getY(){ return y; } | |
public double getZ(){ return z; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment