Yesterday I set a goal for myself: get an arrow on the PC moving in synchronisation with the accelerometer in my iPod. It took a while but I finally got it working!
The most frustrating problem I had, which I wasted about an hour trying to fix, involved read accelerometer output from the sensor. No matter what I did, the callback just would not fire to give me the new readings. It turned out that the accelerometer callbacks just don’t fire in the iPhone Simulator! You have to run the code on a real device to get the readings. I already knew this but the penny was in freefall for a long time before it finally dropped.
As expected, writing for iPhone OS devices is much more enjoyable than writing for Windows. To set up a TCP/IP server on Windows you have to fill in a few structs (I hate how this is so standard in Windows API programming), create a socket, bind the socket, listen on the socket, accept a connection from the socket and then you can read and write. Using Bonjour with the iPhone SDK, I get a list of available services on the network, and when I want to use one, I just do getInputStream:outputStream: on the service (NSNetService) and I can now read and write to my heart’s content. I don’t even know what the hostname, port or transport is, and why should I? I really don’t care… all I want to do is say “I’ll take that device, hook me up”, and then start communicating.
I’m having a bit of trouble with lag, as expected, but I still haven’t set the thing up with multithreading; I’m just checking if there’s data to be read with select() every frame and grabbing it if it is. I expect that my synchronous reads aren’t keeping up with the stream so I’m falling a few frames back in time.
I’ll leave you with a N.E.R.D. song I heard late one night while flicking through the music channels and can’t get out of my head:
