摇一摇做起来很简单,但是再加上iBEacon,处理起来就没有那么得心应手了
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 扫描iBEacon成为第一响应
[self becomeFirstResponder];
}
- (void)viewDidLoad {
// 摇一摇成为第一响应
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
[super viewDidLoad];
// 摇一摇音效
NSString *path = [[NSBundle mainBundle] pathForResource:@"shake" ofType:@"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
NSString *pathFinish = [[NSBundle mainBundle] pathForResource:@"shake_match" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:pathFinish], &soundFinishID);
self.manager=[[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
}
#pragma mark -摇一摇
摇一摇开始
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"摇一摇");
AudioServicesPlaySystemSound (soundID);
// 扫描iBEacon初始化
[self setLocationInfo];
}
}
-(void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
//摇动取消
DLog(@"摇动取消");
}
-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
//摇动结束
if(event.subtype
== UIEventSubtypeMotionShake) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self.view showLoadingViewWithText:@"正在匹配检查报到信息"];
DLog(@"摇动结束");
}
}
扫描iBEacon需要打开蓝牙
#pragma mark -检测蓝牙是否开启
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
switch (peripheral.state) {
//蓝牙开启且可用
case CBPeripheralManagerStatePoweredOn:
{
NSLog(@"Bluetooth is currently powered on and available to use.");
_isRock = YES;
}
break;
default:
NSLog(@"Peripheral Manager did change state");
_isRock = NO;
break;
}
}
#pragma mark -摇一摇动画
-(void)addAnimations
{
CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"transform"];
translation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
translation.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI_4, 0, 0, 100)];
translation.duration = 0.1;
translation.repeatCount = 8;
translation.autoreverses = YES;
[shake.layer addAnimation:translation forKey:@"translation"];
}
#pragma mark -iBeacon方法
- (void)setLocationInfo{
self.locationManager = [[CLLocationManager alloc] init];
//60B48397-9C07-57EA-7F63-79999272FEF8 -- bsc
if (_ibeaconArr.count != 0) {
_ibeaconModel = _ibeaconArr[0];
}
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:_ibeaconModel.uuId];
//FDA50693-A4E2-4FB1-AFCF-C6EB07647825-- 返回数据
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"hospitalBooking"];
self.locationManager.delegate = self;
DLog(@"------%@",self.myBeaconRegion);
if (iOS8orLater) {
[self.locationManager requestAlwaysAuthorization];//设置location是一直允许
}
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{ //开始RegionBeacons
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
}
//找到iBeacon后扫描它的信息
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
// CheckRegisterDetailViewController *check = [[CheckRegisterDetailViewController alloc] init];
// [self.navigationController pushViewController:check animated:YES];
DLog(@"%@",beacons);
CLBeacon *foundBeacon = [beacons firstObject];
// You can retrieve the beacon data from its properties
_hospitalUUId = foundBeacon.proximityUUID.UUIDString;
_hospitalMajor = [NSString stringWithFormat:@"%@", foundBeacon.major];
_hospitalMinor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
for (int i = 0; i< _ibeaconArr.count; i++) {
_ibeaconModel = _ibeaconArr[i];
if ([_hospitalMinor isEqualToString:_ibeaconModel.minorId] && [_hospitalMajor isEqualToString:_ibeaconModel.majorId]) {
_isSameRow = i;
_isSuccess = YES;
_getDataCount += 1;
}
}
_currentTimestamp = [[NSDate date] timeIntervalSince1970];
double second = _currentTimestamp - _beginTimestamp;
if (second < 15) {
_isLook = YES;
}
NSLog(@"%d%f",_isLook,second);
if (_isSuccess == YES) {
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}
_isShankCount ++;
//查找到相同的iBeacon就调用
if (_isSuccess == YES && _getDataCount == 1 && _isShankCount == 1 && _isLook == YES) {
_ibeaconModel = _ibeaconArr[_isSameRow];
//查询待签到的检查订单
[self getIbeaconCanBookDataWithIbeaconId:_ibeaconModel.id];
}
else{
[self.view dismissLoadingView];
// UIAlertView *noteAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"扫描设备失败,请重新摇一摇" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
// [noteAlertView show];
_isRock = YES;
_isShankCount = 0;
}
}
个人笔记:技术不难,就怕结合,结合起来考虑的问题要多点。记得在plist中加这个:
NSLocationAlwaysUsageDescription 我们需要通过您的地理位置信息获取您周边的相关数据
NSLocationWhenInUseUsageDescription 我们需要通过您的地理位置信息获取您周边的相关数据