简体   繁体   中英

Sensor data from iPhone to PC using Java app

i've found an app for iPhone called "SensorLogger" that can log the data of most of iPhone's sensors and then send them via e-mail, but it also can broadcast it over the network using UDP packets, here is the code (Java) i use to receive that data :

import java.io.*;
import java.net.*;

public class Main {
    public static void main(String[] args) throws UnknownHostException, SocketException, IOException {
        DatagramSocket socket = new DatagramSocket(5555, InetAddress.getByName("0.0.0.0"));
        byte[] buf = new byte[64];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        while (true) {
            socket.receive(packet);
            String data = new String(packet.getData());
            System.out.println(java.util.Arrays.toString(data.split(",")));
        }
    }
}

it works perfectly, here is the output with the app on the iPhone running :

[1343331999601, 4, 0.0013, -0.0049, -0.00237]
[1343331999636, 4, -0.0024, 0.0049, -0.00477]
[1343331999670, 4, -0.0073, 0.0073, -0.00377]
[1343331999704, 4, -0.0036, 0.0024, -0.00247]

the 1st number is sort of a timestamp, but i don't know what these numbers mean, it doesn't match with the date/time on the iPhone... the second number means what sensor is used, 1 is GPS position, 2 is compass, 3 is accelerometer, and 4 is gyroscope. The final numbers are the values from the sensor (defined by the 2nd number). Now, i wanted to do a small GUI app which would display all these values in a more user friendly way... but, this is the first time i program something in Java and i have no idea how to do that... can anyone please send a sample code so i can understand how to do it and repeat that for all of the values.

Thanks and have a nice day everyone.

Problem solved, i finally used this code :

import java.io.*;
import java.net.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws UnknownHostException, SocketException, IOException {
        DatagramSocket socket = new DatagramSocket(5555, InetAddress.getByName("0.0.0.0"));
        byte[] buf = new byte[64];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        while (true) {
            socket.receive(packet);
            String data = new String(packet.getData());
            String[] dataParsed = data.split(",");
            float timestamp = Float.parseFloat(dataParsed[0]);
            float sensortype = Float.parseFloat(dataParsed[1]);
            float x = Float.parseFloat(dataParsed[2]);
            float y = Float.parseFloat(dataParsed[3]);
            float z = Float.parseFloat(dataParsed[4]);
            String sensorname = new String();
            if (sensortype == 1) {
                sensorname = "GPS";
            }
            else if (sensortype == 2) {
                sensorname = "Magnetometer";
            }
            else if (sensortype == 3) {
                sensorname = "Accelerometer";
            }
            else if (sensortype == 4) {
                sensorname = "Gyroscope";
            }
            else if (true) {
                sensorname = "Unknown";
            }
            Date date = new Date();String line = ("Timestamp " + timestamp + ", local date " + date.toString() + ", sensor " + sensorname + ", x " + x + ", y " + y + ", z " + z);
            System.out.println(line);

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM