Collecting accelerometer data for analysis

Hi - I want to collect a bunch of data from the iPhone accelerometer for analysis back on my desktop machine.

Is there a simple way for a Unity application to collect and transfer that kind of information back to my machine?

I am thinking that writing to a file would be the easiest way, but cant find any file writing APIs.

File writing can be found in the dot net System.IO classes.

Transferring back to your desktop machine will be the tricky part. You might want to use a web server with HTTP file upload as the middleman. Though if you’re doing this as a one-off, you can probably hard code your desktop’s IP address and open a listening socket on some random high port, and have the Unity app send to that.

I don’t really want to set up a webserver for this

Any other ways of doing it? How do I get a file off the iPhone?

Unless you jailbreak the phone, there’s no easy way to get a file off it.

If you’re doing this as a one-off (i.e. an experiment you are doing, not as part of a game you are distributing) just run a socket server on your desktop PC, and send the data over a socket to it.

You can make a simple socket server using tools you probably have around the house. Perl makes it pretty easily (IO:Socket makes it easy, see here: http://www.perlfect.com/articles/sockets.shtml or here: http://perl.active-venture.com/pod/perlipc-tcpserver.html for a socket server in under 20 lines).

In your Unity app, connect to that socket server using the System.Net.Sockets.Socket. A sample of that is here: System.Net.Sockets.Socket Class

However, if you jailbreak the phone, you can use the “Disk for iPhone” FUSE module to mount your iPhone to the desktop as a USB file device, and browse to your app’s /Documents directory, and copy the file from there.

For those without the pro version of Unity, heres a basic webserver:

            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://*:8080/");

            listener.Start();
            Console.WriteLine("Listening...");

            byte[] ok = System.Text.Encoding.UTF8.GetBytes("OK");

            while (true)
            {
                HttpListenerContext context = listener.GetContext();

                Console.WriteLine(context.Request.Url);

                byte[] buf = new byte[1024];
                MemoryStream ms = new MemoryStream();

                int len = context.Request.InputStream.Read(buf, 0, buf.Length);
                while (len > 0)
                {
                    ms.Write(buf, 0, len);
                    len = context.Request.InputStream.Read(buf, 0, buf.Length);
                }

                buf = ms.ToArray();

                Console.WriteLine(System.Text.Encoding.ASCII.GetString(buf, 0, buf.Length));

                context.Response.StatusCode = 200;
                context.Response.OutputStream.Write(ok, 0, 2);
                context.Response.Close();
            }
            listener.Stop();

Use WWWForm and WWW to submit to the server.

I am running the code above from Visual Studio, but it should also work under Mono