【发布时间】:2016-03-30 13:48:44
【问题描述】:
在我的应用程序中,我正在寻找信标并想要计算距离,这两天我遇到了一个问题,即 CCLocationManager 委托函数返回空信标数组。我尝试了 CBCentralManagerDelegate 函数,我得到了信标列表。在 didRangeBeacons 方法中给出了空数组帮助我。以下是我尝试过的代码
代码:
@interface ViewController ()<CBCentralManagerDelegate,CLLocationManagerDelegate>
{
NSMutableData *dat;
CLBeaconRegion *clBeconRegion;
CLLocationManager *locManager;
}
@property (strong,nonatomic) CBCentralManager *cbcManager;
- (IBAction)scanBtn:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.cbcManager=[[CBCentralManager alloc] initWithDelegate:self queue:nil];
locManager=[[CLLocationManager alloc] init];
locManager.delegate=self;
[self initRegion];
clBeconRegion.notifyEntryStateOnDisplay=YES;
[locManager requestAlwaysAuthorization];
[self locationManager:locManager didStartMonitoringForRegion:clBeconRegion];
}
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"List of peripheral %@",peripheral);
NSLog(@"List of advertisementData %@ %@",advertisementData,RSSI);
}
-(void)initRegion{
NSUUID *uuid=[[NSUUID alloc]initWithUUIDString:@"XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
clBeconRegion=[[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:@"BluetoothExample"];
clBeconRegion.notifyEntryStateOnDisplay=YES;
clBeconRegion.notifyOnEntry=YES;
clBeconRegion.notifyOnExit=YES;
[locManager startMonitoringForRegion:clBeconRegion];
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"didConnectPeripheral");
[peripheral discoverServices:nil];
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region{
[locManager startRangingBeaconsInRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(@"Entered Region");
[locManager startRangingBeaconsInRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(@"Exit Region");
[locManager stopRangingBeaconsInRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
NSLog(@"beacons %@ last %@",beacons,[region proximityUUID]);
CLBeacon *beacon=[[CLBeacon alloc]init];
beacon=[beacons lastObject];
NSLog(@"min %@ maj %@",beacon.minor,beacon.major);
}
- (IBAction)scanBtn:(id)sender
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.cbcManager scanForPeripheralsWithServices:nil options:options];
NSLog(@"scan clicked");
}
我已经修复了 NSLocationAlwaysUsageDescription info.plist。
我有一个真正的信标和 ipad 迷你设备。
此代码在所有信标查找示例中都给出。帮助我在这里做错了什么。
【问题讨论】:
-
如果它们是“真正的 iBeacons”,您应该无法通过
CBCentralManager看到它们。 -
如果我是你,我会看这个:raywenderlich.com/101891/ibeacons-tutorial-ios-swift。还要考虑更详细地命名变量。在命名和一般不良的命名约定(deviNamArr)上偷工减料确实令人不悦。 deviNamArr = deviceNames 或 deviceNamesArray.
-
我正在使用 CBCentralManager 获取此操作:外围设备列表
2016 -03-30 19:33:22.371 BluetoothExample[21355:3454631] 广告数据列表 { kCBAdvDataIsConnectable = 1; kCBAdvDataLocalName = "XY-1D54-64"; kCBAdvDataServiceUUIDs = ("ABC24206-9BD7-034F-4FC9-ED1023171D54"); } -70 -
好的,谢谢你,我更正了我的变量命名@TheCodingArt
标签: objective-c cllocationmanager ibeacon