文/YotrolZ(简书作者)
原文链接:http://www.jianshu.com/p/233be81b8ead
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

iOS中常见的传感器

1240

iOS中常见的传感器

一.距离传感器

  • 监听方式:添加观察者,监听通知

  • 通知名称:UIDeviceProximityStateDidChangeNotification

  • 监听状态:观察者的对应回调方法中,判断[UIDevice currentDevice].proximityState

    • 返回  NO : 有物品靠近了;

    • 返回  YES : 有物品远离了

  • 注意:使用前要打开当前设备距离传感器的开关(默认为:NO):

    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
  • 示例程序:

- (void)viewDidLoad
{
    [super viewDidLoad];    // [UIApplication sharedApplication].proximitySensingEnabled = YES;
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;    // 监听有物品靠近还是离开
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)proximityStateDidChange
{    if ([UIDevice currentDevice].proximityState) {        NSLog(@"有物品靠近");
    } else {        NSLog(@"有物品离开");
    }
}

二.加速计(UIAccelerometer)

  • 概述:
    检测设备在X/Y/Z轴上的受力情况

    1240

    加速计

  • 监听方式:设置代理

  • 使用步骤:(iOS5之前)

    • UIAcceleration参数:

      @interface UIAcceleration : NSObject {@privateNSTimeInterval timestamp;UIAccelerationValue x, y, z;
      }
    • 获取加速计单例对象:

      UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    • 设置加速计代理对象

      accelerometer.delegate = self;
    • 设置采样间隔 : updateInterval

      accelerometer.updateInterval = 0.3;
    • 实现代理相关方法,监听加速计的数据

      - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;
  • 示例程序:

#import "ViewController.h"@interface ViewController () <UIAccelerometerDelegate>@end@implementation ViewController- (void)viewDidLoad {
    [super viewDidLoad];    // 1.获取单例对象
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];    // 2.设置代理
    accelerometer.delegate = self;    // 3.设置采样间隔
    accelerometer.updateInterval = 0.3;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{    NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}@end
  • 备注:UIAccelerationUIAccelerometer在iOS 5.0中已经被弃用。由Core Motion framework取代。

    • 官方提示信息:

      UIAcceleration and UIAccelerometer are deprecated as of iOS 5.0. These classes have been replaced by the Core Motion framework.

三 . Core Motion

  • Core Motion获取数据的两种方式:

    • push : 实时采集所有数据,采集频率高;

    • pull : 在有需要的时候,才去采集数据;

Core Motion的使用步骤---push
  • 1.创建运动管理对象

    CMMotionManager*mgr = [[CMMotionManageralloc]init];
  • 2.判断加速器是否可用(最好判断)

    if(mgr.isAccelerometerAvailable){  //加速计可用}
  • 3.设置采样间隔

    mgr.accelerometerUpdateInterval= 1.0/30.0;// 1秒钟采样30次
  • 4.开始采样(采样到数据就会调用handler,handler会在queue中执行)

    -(void)startAccelerometerUpdatesToQueue:(NSOperationQueue*)queue withHandler:(CMAccelerometerHandler)handler;
  • 示例程序:

#import "ViewController.h"@interface ViewController () <UIAccelerometerDelegate>/** 运动管理者 */@property (nonatomic, strong) CMMotionManager *mgr; // 保证不死@end@implementation ViewController#pragma mark - 懒加载- (CMMotionManager *)mgr
{    if (_mgr == nil) {
        _mgr = [[CMMotionManager alloc] init];
    }    return _mgr;
}
- (void)viewDidLoad {
    [super viewDidLoad];    // 1.判断加速计是否可用
    if (!self.mgr.isAccelerometerAvailable) {        NSLog(@"加速计不可用");        return;
    }    // 2.设置采样间隔
    self.mgr.accelerometerUpdateInterval = 0.3;    // 3.开始采样
    [self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { // 当采样到加速计信息时就会执行
        if (error) return;        // 4.获取加速计信息
        CMAcceleration acceleration = accelerometerData.acceleration;        NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    }];
}@end
Core Motion的使用步骤---pull
  • 说明 : pull是在需要时获取数据,我们此时以点击了屏幕就获取一次数据为例说明;

  • 1.创建运动管理对象

    CMMotionManager*mgr = [[CMMotionManageralloc]init];
  • 2.判断加速器是否可用(最好判断)

    if(mgr.isAccelerometerAvailable){  //加速计可用}
  • 3.开始采样

    -(void)startAccelerometerUpdates;
  • 4.在需要时获取数据

    CMAcceleration acc = mgr.accelerometerData.acceleration;
    NSLog(@"%f,%f, %f", acc.x,acc.y,acc.z);
  • 示例程序:

#import "ViewController.h"@interface ViewController () <UIAccelerometerDelegate>/** 运动管理者 */@property (nonatomic, strong) CMMotionManager *mgr; // 保证不死@end@implementation ViewController#pragma mark - 懒加载- (CMMotionManager *)mgr
{    if (_mgr == nil) {
        _mgr = [[CMMotionManager alloc] init];
    }    return _mgr;
}
- (void)viewDidLoad {
    [super viewDidLoad];    // 1.判断加速计是否可用
    if (!self.mgr.isAccelerometerAvailable) {        NSLog(@"加速计不可用");        return;
    }    // 2.开始采样
    [self.mgr startAccelerometerUpdates];
}@end

    // 3.数据采样(以点击了屏幕为例说明)- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{    // 获取加速计信息
    CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;    NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}
四.磁力计/陀螺仪的使用和上述加速计的使用步骤类似

不同点:

  • 1.判断传感器是否可用:

    • 加速计:

      @property(readonly, nonatomic, getter=isAccelerometerAvailable) BOOL accelerometerAvailable;
    • 陀螺仪:

      @property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable;
    • 磁力计:

      @property(readonly, nonatomic, getter=isMagnetometerAvailable) BOOL magnetometerAvailable
  • 2.设置传感器的采样间隔:

    • 1.加速计:

      @property(assign, nonatomic) NSTimeInterval accelerometerUpdateInterval;
    • 2.陀螺仪:

      @property(assign, nonatomic) NSTimeInterval gyroUpdateInterval;
    • 3.磁力计:

      @property(assign, nonatomic) NSTimeInterval magnetometerUpdateInterval
  • 3.1.开始采样的方法--push:

    • 1.加速计:

      - (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler;
    • 2.陀螺仪:

      - (void)startGyroUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMGyroHandler)handler;
    • 3.磁力计:

      - (void)startMagnetometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMMagnetometerHandler)handler;
  • 3.2.开发采样的方法--pull

    • 1.加速计:

      - (void)startAccelerometerUpdates;
    • 2.陀螺仪:

      - (void)startGyroUpdates;
    • 3.磁力计:

      - (void)startMagnetometerUpdates;
  • 4.1获取采样数据--push

    • 在对应的传感器的开始采样方法中的handler中;

  • 4.2.获取采样数据--pull

    • 在需要获取的数据地方调用下面的方法:

    • 加速计:

      CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    • 陀螺仪:

      CMRotationRate rate = self.mgr.gyroData.rotationRate;NSLog(@"x:%f y:%f z:%f", rate.x, rate.y, rate.z);
五.没事你就,摇一摇
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{    NSLog(@"开始摇一摇");
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{    NSLog(@"摇一摇被取消");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{    NSLog(@"摇一摇停止");
}
六. 没事走两步(计步器)
  • 1.判断计步器是否可用

      if (![CMPedometer isStepCountingAvailable]) {      NSLog(@"计步器不可用");      return;
      }
  • 2.创建计步器对象

    CMPedometer *stepCounter = [[CMPedometer alloc] init];
  • 3.开始记步,并获取采样数据

      [stepCounter startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData *pedometerData, NSError *error) {
          if (error) return;
          // 4.获取采样数据
          NSLog(@"steps = %@", pedometerData.numberOfSteps);
      }];
七.蓝牙
  • 简述:
    iOS中提供了4个框架用于实现蓝牙连接:

    • 可用于第三方蓝牙设备交互,必须要支持蓝牙4.0;

    • 硬件至少是4s,系统至少是iOS6;

    • 蓝牙4.0以低功耗著称,一般也叫BLE(BluetoothLowEnergy)

    • 目前应用比较多的案例:运动手坏、嵌入式设备、智能家居

    • 可用于第三方蓝牙设备交互,但是蓝牙设备必须经过苹果MFi认证(国内较少)

    • 只能用于iOS设备之间的连接,从iOS7开始引入

    • 只能用于iOS设备之间的连接,多用于游戏(比如五子棋对战),可以在游戏中增加对等连接,又称对端连接点对点连接 Peer To Peer,从iOS7开始过期

    • 1.GameKit.framework

    • 2.MultipeerConnectivity.framework

    • 3.ExternalAccessory.framework

    • 4.CoreBluetooth.framework(时下热门)