Search code examples
ioswatchkitcore-motioncmmotionmanager

Get accelerometer and gyroscope data from apple watch (and not the iphone)?


After seeing this question, I tried to code up a quick program that would save the watches accelerometer and gyroscope data to a file.

@implementation InterfaceController{
    NSMutableArray *accData;
    bool recording;
}
- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    // Configure interface objects here.
    self.motionManager = [[CMMotionManager alloc] init];
    [self.motionManager setAccelerometerUpdateInterval:.01];
}

- (IBAction)startStopRecording {
    if (!recording){//We are starting to record.
        recording = YES;
        accData = [[NSMutableArray alloc] init];
        [self.startRecording setTitle:@"Stop Recording"];
        [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            [accData addObject:[NSString stringWithFormat:@"%f, %f, %f", accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z]];
        }];
    }else{
        recording = NO;//we are stopping the recording
        [self.motionManager stopAccelerometerUpdates];
        [self.startRecording setTitle:@"Start Recording"];
        [InterfaceController openParentApplication:@{ @"accData": accData } reply:^(NSDictionary *replyInfo, NSError *error) { //this method saves the array to a csv file.
            NSLog(@"Data has been saved.");
        }];
    }
}

I had plotted this data and for the life of me, no matter how hard I shook the watch, all my plots looked like this:

enter image description here

Until 8 hours later, I started to suspect that I wasn't grabbing the acceleration data from the watch, but rather from the phone (sitting still on the table next to me). I ran some tests and confirmed that this is exactly what is happening.

Which leads me to the original question. How do I pull acceleration/gyro/data from the watch and not from the iPhone?


Solution

  • The problem was that I wasn't running watchOS2. I assumed I was but it's still in beta and I hadn't installed it. The data I was getting was accelerometer data from the phone. Also, currently, you can only get acc data from the watch using watchOS2 and not gyro data.