从苹果手表(而不是iPhone)获取加速度计和陀螺仪数据?

时间:2015-09-09 18:37:58

标签: ios watchkit core-motion cmmotionmanager

在看到this question后,我尝试编写一个快速程序,将手表加速度计和陀螺仪数据保存到文件中。

@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.");
        }];
    }
}

我已经绘制了这些数据并且为了我的生活,无论我多么努力地摇晃手表,我的所有情节看起来都像这样:

enter image description here

直到8个小时之后,我才开始怀疑我是不是从手表上抓取了加速度数据,而是从手机上取下(坐在我旁边的桌子上)。我进行了一些测试并确认这正是发生的事情。

这引出了我原来的问题。如何从手表而不是从iPhone中提取加速度/陀螺仪/数据?

2 个答案:

答案 0 :(得分:3)

问题是我没有运行watchOS2。我以为我是,但它仍处于测试阶段,我还没有安装它。我得到的数据是来自手机的加速计数据。此外,目前,您只能使用watchOS2而不是陀螺仪数据从手表获取acc数据。

答案 1 :(得分:0)

您可以使用CoreMotion框架获取活动数据。 虽然我只能获得加速数据,但陀螺通常会返回假。