http://www.cnblogs.com/ShaoYinling/p/4778245.html
http://www.jianshu.com/p/233be81b8ead
一、传感器的使用举例:
1)在地图应用中,根据手机朝向识别方向
2)打电话时,手机靠近耳朵进入锁屏状态,打到省电目的
3)根据环境亮度调节手机亮度, 拍照时,闪光灯会在一定条件下自动启动
二、传感器的类型:
/** 传感器类型 作用
环境光传感器 Ambient Light 感应周边环境光线的强弱(自动调节屏幕的亮度)
内部温度传感器 Internal Temperature 感应设备的温度(提醒用户降温,防止设备损坏)
湿度传感器 Moisture 感应设备是否进水(方便维护人员)
距离传感器 Proximity 感应是否有其他物体靠近设备屏幕(打电话自动锁屏)
磁力计传感器 Mangetometer 感应周边的磁场
陀螺仪 Gyroscope 感应设备的持握方向(赛车类游戏)
加速计 Motion/Acceletometer 感应设备的运动(摇一摇、计步器等)
*/
————————————————————————————
距离传感器实例:
- (void)viewDidLoad {
[super viewDidLoad];
//1.开启距离传感器,default is NO
// [UIApplication sharedApplication].proximitySensingEnabled = YES;
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
//2.当监听到有物体靠近设备时系统会发出通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
- (void)proximityStateDidChange:(NSNotification *)noti
{
if ([UIDevice currentDevice].proximityState) {
//有物体靠近
} else {
//物体离开
}
}
————————————————————————————
加速计实例:
{
//iOS5之前版本, UIAccelerometer
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = 1/30.0; //1秒钟采样30次
}
#pragma mark - UIAccelerometerDelegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
//acceleration 中 x,y,z 三个属性分别代表每个轴上的加速度
}
//2.加速计 CoreMotion #import <CoreMotion/CoreMotion.h>
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
if (!motionManager.isAccelerometerAvailable) { //判断加速计是否可用
NSLog(@"-------- 加速计不可用 ------");
return;
}
motionManager.accelerometerUpdateInterval = 1/30.0; //采集间隔, 1秒30次
//开始采样
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (!error) {
//获取加速计信息
CMAcceleration acceleration = accelerometerData.acceleration;
NSLog(@"x:%f, y:%f, z:%f", acceleration.x, acceleration.y, acceleration.z);
}
}];
//计步器
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { // iOS7
if (![CMStepCounter isStepCountingAvailable]) { //iOS7
NSLog(@"------- iOS7 计步器不可用 ------");
return;
}
CMStepCounter *stepCounter = [[CMStepCounter alloc] init];
//开始计步
[stepCounter startStepCountingUpdatesToQueue:[[NSOperationQueue alloc] init] updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {
if (!error) {
NSString *stepString = [NSString stringWithFormat:@"您一共走了%zd步", numberOfSteps];
dispatch_async(dispatch_get_main_queue(), ^{
//更新UI显示
});
}
}];
} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //iOS8
if (![CMPedometer isStepCountingAvailable]) {
NSLog(@"------- iOS8 计步器不可用 ------");
return;
}
CMPedometer *pedMeter = [[CMPedometer alloc] init];
//更新从某个时间开始的步数变化 (当天0点开始)
NSCalendar *caleddar = [NSCalendar currentCalendar];
NSDateComponents *components = [caleddar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
NSDate *startDate = [caleddar dateFromComponents:components];
[pedMeter startPedometerUpdatesFromDate:startDate withHandler:^(CMPedometerData *pedometerData, NSError *error) {
if (!error) {
if (pedometerData) {
NSLog(@"numberOfSteps = %@, distance = %@", pedometerData.numberOfSteps, pedometerData.distance);
// numberOfSteps步数, distance距离(包括走路和跑步),floorsAscended上楼, floorsDescended下楼
// iOS9: currentPace步伐, currentCadence速率
// iOS10: averageActivePace平均步伐
dispatch_async(dispatch_get_main_queue(), ^{
//更新UI显示
});
}
}
}];
//查询过去某时间段内走过的步数
[pedMeter queryPedometerDataFromDate:startDate toDate:[NSDate date] withHandler:^(CMPedometerData *pedometerData, NSError *error) {
}];
}
//陀螺仪
CMMotionManager *motionManaget = [[CMMotionManager alloc] init];
if (!motionManaget.isGyroAvailable) {
NSLog(@"--- 陀螺仪不可用----");
return;
}
motionManaget.gyroUpdateInterval = 1/30.0; //采样间隔
[motionManaget startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {
if (!error) {
NSLog(@"x: %f, y:%f, z:%f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z);
}
}];
//磁力计
if (!motionManaget.isMagnetometerAvailable) {
NSLog(@"--- 磁力计不可用----");
return;
}
motionManaget.magnetometerUpdateInterval = 1/30.0;
[motionManaget startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
if (!error) {
NSLog(@"x: %f, y:%f, z:%f", magnetometerData.magneticField.x, magnetometerData.magneticField.y, magnetometerData.magneticField.z);
}
}];
————————————————————————————
光感实例:
//获取屏幕亮度 值:0~1.0
[[UIScreen mainScreen] brightness];
//亮度改变通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(brightnessDidChange:) name:UIScreenBrightnessDidChangeNotification object:nil];
————————————————————————————
蓝牙:CoreBluetooth.framework