BlogDemo
BlogDemo copied to clipboard
楼主您好,我把这个扫描的demo集成到IOS原声交互的一个方法里,结果它的回调函数一个都不触发
代码: #import "DiscoverBeacon.h" #import <React/RCTBridge.h> #import "LocalPush.h"
#import <CoreLocation/CoreLocation.h> #import <CoreBluetooth/CoreBluetooth.h>
#define Beacon_Device_UUID @"FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
@interface DiscoverBeacon () < CLLocationManagerDelegate
/** 检查定位权限 / @property (nonatomic, strong) CLLocationManager locationManager; / 需要被监听的beacon */ @property (nonatomic, strong) CLBeaconRegion *beaconRegion; @property (nonatomic, strong) NSMutableDictionary *dataDict; @end
@implementation DiscoverBeacon
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(discoverBeacon) { // 在开始监控之前,我们需要判断改设备是否支持,和区域权限请求 BOOL availableMonitor = [CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]];
if (availableMonitor) {
CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
switch (authorizationStatus) {
case kCLAuthorizationStatusNotDetermined:
[self.locationManager requestAlwaysAuthorization];
break;
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied:
NSLog(@"受限制或者拒绝");
break;
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:{
NSLog(@"开启扫描");
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
}
break;
}
} else {
NSLog(@"该设备不支持 CLBeaconRegion 区域检测");
}
}
-
(CLLocationManager *)locationManager { if (!_locationManager) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; } return _locationManager; }
-
(CLBeaconRegion *)beaconRegion { if (!_beaconRegion) { // 监听所有UUID为Beacon_Device_UUID的Beacon设备 _beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:Beacon_Device_UUID] identifier:@"test"];
// 监听UUID为Beacon_Device_UUID,major为666的所有Beacon设备 // _beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:Beacon_Device_UUID] major:666 identifier:@"test"]; // 监听UUID为Beacon_Device_UUID,major为666,minor为999的唯一一个Beacon设备 // _beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:Beacon_Device_UUID] major:666 minor:999 identifier:@"test"]; _beaconRegion.notifyEntryStateOnDisplay = YES;
} return _beaconRegion; }
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) { [self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; [self.locationManager startMonitoringForRegion:self.beaconRegion]; } }
#pragma mark -- Monitoring
/** 进入区域 */
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { NSLog(@"进入区域"); }
/** 离开区域 */
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
}
/** Monitoring有错误产生时的回调 */
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(nullable CLRegion *)region withError:(NSError *)error { NSLog(@"monitoring开启扫描失败"); }
/** Monitoring 成功回调 */
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region { NSLog(@"monitoring开启扫描成功"); }
#pragma mark -- Ranging
/** 1秒钟执行1次 */
-
(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(nonnull NSArray<CLBeacon *> *)beacons inRegion:(nonnull CLBeaconRegion *)region { for (CLBeacon *beacon in beacons) { NSLog(@" rssi is :%ld",(long)beacon.rssi); NSLog(@" beacon proximity :%ld",(long)beacon.proximity); NSLog(@" accuracy : %f",beacon.accuracy); NSLog(@" proximityUUID : %@",beacon.proximityUUID.UUIDString); NSLog(@" major :%ld",(long)beacon.major.integerValue); NSLog(@" minor :%ld",(long)beacon.minor.integerValue); }
if (!_dataDict) { _dataDict = [NSMutableDictionary dictionary]; } if (beacons.count) { [_dataDict setObject:beacons forKey:region.proximityUUID.UUIDString]; } else { [_dataDict removeObjectForKey:region.proximityUUID.UUIDString]; } }
/** ranging有错误产生时的回调 */
- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error { NSLog(@"ranging开启扫描失败"); }
#pragma mark -- Kill callBack
/** 杀掉进程之后的回调,直接锁屏解锁,会触发 */
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region { { NSLog(@"退出应用"); LocalPush *localNotification = [[LocalPush alloc] init]; NSString *msg = [NSString stringWithFormat:NSLocalizedString(@"你监听的Beacon区域状态:%@,锁屏点亮屏幕会收到此推送",nil),(state==CLRegionStateUnknown)?@"未知":(state=CLRegionStateInside)?@"区域内":@"区域外"]; if ([region isKindOfClass:[CLBeaconRegion class]]) { CLBeaconRegion *bregion = (CLBeaconRegion *)region; NSString *body = [NSString stringWithFormat:@"status = %@,uuid = %@,major = %ld,minor = %ld",((state==CLRegionStateUnknown)?@"未知":(state=CLRegionStateInside)?@"区域内":@"区域外"),bregion.proximityUUID.UUIDString,[bregion.major integerValue],[bregion.minor integerValue]]; localNotification.body = body; localNotification.soundName = nil; // localNotification.delayTimeInterval = 0.0; [localNotification pushLocalNotification]; } } }
@end
只会打印一个“开启扫描”,开启扫描成功或失败的回调函数都没有触发
初始化locationManager和beaconRegion调用了,其他的都没调用