最近要做穿戴式裝置的App,需要研究Apple Watch如何使用加速度計 (Using Accelerometer)。
首先研究Apple的文件Core Motion Framework。
它這麼介紹:
Core Motion reports motion- and environment-related data from the onboard hardware of iOS devices, including from the accelerometers, gyroscopes, pedometers, magnetometers, and barometers. You use this framework to access hardware-generated data so that you can use it in your app. For example, a game might use accelerometer and gyroscope data to control onscreen game behavior.
Many services of this framework let you access both the raw values recorded by the hardware and a processed version of those values. Processed values do not include forms of bias that might adversely affect how you use that data. For example, a processed accelerometer value reflects only the acceleration caused by the user and not the acceleration caused by gravity.
接著隨意創立一個專案,並加入Watch App的Target。一共需要3個ID,可參考先前文章:Watch App三個ID設定。
接著直接在InterfaceController.m寫code,並連結畫面3個Label:
/** Theme: Watch Accelerometer IDE: Xcode 9 Language: Objective C Date: 107/02/06 Author: HappyMan Blog: https://cg2010studio.com/ */ // // InterfaceController.m // Watch Extension // // Created by HappyBoy on 02/02/2018. // Copyright © 2018 Jason. All rights reserved. // #import "InterfaceController.h" #import <CoreMotion/CoreMotion.h> #import <WatchConnectivity/WatchConnectivity.h> #import <HealthKit/HealthKit.h> @interface InterfaceController() <WCSessionDelegate, HKWorkoutSessionDelegate> @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *xLabel; @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *yLabel; @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *zLabel; @property (nonatomic, strong) CMMotionManager *manager; @property (nonatomic, strong) HKHealthStore *healthStore; @end @implementation InterfaceController - (void)awakeWithContext:(id)context { [super awakeWithContext:context]; HKWorkoutSession *session = [[HKWorkoutSession alloc] initWithActivityType:HKWorkoutActivityTypeRunning locationType:HKWorkoutSessionLocationTypeOutdoor]; session.delegate = self; [self.healthStore startWorkoutSession:session]; CMMotionManager *motionMgr = [[CMMotionManager alloc] init]; if([motionMgr isAccelerometerAvailable]) NSLog(@"Apple Watch: Accelerometer is available"); if([motionMgr isGyroAvailable]) NSLog(@"Apple Watch: Gyroscope is available"); if([motionMgr isMagnetometerAvailable]) NSLog(@"Apple Watch: Magnetometer is available"); if([motionMgr isDeviceMotionAvailable]) NSLog(@"Apple Watch: Device motion identification is available"); } - (void)willActivate { // This method is called when watch view controller is about to be visible to user [super willActivate]; WCSession* session = [WCSession defaultSession]; session.delegate = self; [session activateSession]; _manager = [[CMMotionManager alloc] init]; if (_manager.accelerometerAvailable) { NSLog(@"accelerometer Available"); _manager.accelerometerUpdateInterval = 0.1; [_manager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) { CMAcceleration acc = accelerometerData.acceleration; //NSLog(@"%f,%f,%f",acc.x,acc.y,acc.x); NSString* x = [NSString stringWithFormat:@"x:%lf",acc.x]; NSString* y = [NSString stringWithFormat:@"y:%lf",acc.y]; NSString* z = [NSString stringWithFormat:@"z:%lf",acc.z]; dispatch_async(dispatch_get_main_queue(), ^{ [self.xLabel setText:x]; [self.yLabel setText:y]; [self.zLabel setText:z]; }); }]; } } - (void)didDeactivate { // This method is called when watch view controller is no longer visible [super didDeactivate]; } - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message { } - (void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error { }
我們知道iPhone的感應器有4個都能使用:
- Accelerometer
- Gyroscope
- Magnetometer
- Device motion
然而Watch目前只開放2個:
- Accelerometer
- Device motion
期待未來Apple大發慈悲心,讓我開發者可以盡情發揮Watch的潛力!像是watchOS 4開放存取即時心率,藍芽功能也開放更多功能的權限!
有空在Watch上寫支遊戲App,就使用感應器來操作,想必一定很有趣:D~
參考:A Guide to Develop a Fall Detection Application in Apple Watch、iOS/WatchOS 加速度數據獲取、Core Motion Framework。
隨意留個言吧:)~