Just My Life & My Work

最近要做穿戴式裝置的App,需要研究Apple Watch如何使用加速度計 (Using Accelerometer)

[watchOS] 使用加速度計 (Using Accelerometer).jpg

首先研究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設定


 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;

@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;
            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 {



  • Accelerometer
  • Gyroscope
  • Magnetometer
  • Device motion


  • Accelerometer
  • Device motion

期待未來Apple大發慈悲心,讓我開發者可以盡情發揮Watch的潛力!像是watchOS 4開放存取即時心率,藍芽功能也開放更多功能的權限!


參考:A Guide to Develop a Fall Detection Application in Apple WatchiOS/WatchOS 加速度數據獲取Core Motion Framework



WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


%d 位部落客按了讚: