[英]iBeacon: didRangeBeacons is not getting called when running app
I prepared all the necessary stuff to setup a beacon region: 我准备了设置信标区域所需的所有东西:
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
//raspberry pi:E2C56DB5-DFFB-48D2-B060-D0F5A71096E0
//estimote:B9407F30-F5F8-466E-AFF9-25556B57FE6D
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"monaLisaBeacon"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
However, didRangeBeacons is not always invoked. 但是,并不总是调用didRangeBeacons。 It invokes only once but then stops getting invoked.
它只调用一次,但后来停止调用。
Are you sure that didRangeBeacons
was invoked even once? 你确定曾经调用过
didRangeBeacons
吗? It will not be invoked if you just call startMonitoringForRegion
. 如果只调用
startMonitoringForRegion
,则不会调用它。 When you call startMonitoringForRegion
you should get callback in your locationManager's delegate object in the form of didDetermineState
, didEnterRegion
and didExitRegion
. 当你调用
startMonitoringForRegion
你应该以didDetermineState
, didEnterRegion
和didExitRegion
的形式在你的locationManager的委托对象中得到回调。 What you need to do is, in your didEnterRegion
method, call startRangingBeaconsInRegion
. 您需要做的是,在您的
didEnterRegion
方法中,调用startRangingBeaconsInRegion
。 Here is an example: 这是一个例子:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]]) {
CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
//now start ranging
[_locationManager startRangingBeaconsInRegion:beaconRegion];
}
}
Also dont forget to stop ranging: 也别忘了停止测距:
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]]) {
CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
//STOP ranging
[_locationManager stopRangingBeaconsInRegion:beaconRegion];
}
}
Only if you start ranging, your didRangeBeacons
will be invoked. 只有在开始测距时,才会调用
didRangeBeacons
。
Hope this helps. 希望这可以帮助。
Try following code: 请尝试以下代码:
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
//raspberry pi:E2C56DB5-DFFB-48D2-B060-D0F5A71096E0
//estimote:B9407F30-F5F8-466E-AFF9-25556B57FE6D
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"monaLisaBeacon"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startRangingBeaconsInRegion:beaconRegion];
You need to start ranging beacons as well to make didRangebeacons to be called. 你需要开始测距信标,以便调用didRangebeacons。
Are you only seeing this with your Raspberry Pi-based beacon? 你只用基于Raspberry Pi的灯塔看到这个吗? If so, your Pi's advertisements may be getting turned off by another device attempting to connect with it.
如果是这样,您的Pi的广告可能会被试图与之连接的其他设备关闭。 The Knock app for OSX is known to cause this problem , although other apps may do it, too.
已知OSX的Knock应用程序会导致此问题 ,但其他应用程序也可能会这样做。
If this is the problem, there is a fix documented in an edit to the blog post telling you how to build an iBeacon out of a Raspberry Pi. 如果这是问题,那么在博客文章的编辑中会记录一个修复,告诉您如何从Raspberry Pi构建iBeacon。 You need to edit the iBeacon start script on the Pi to change this line:
您需要在Pi上编辑iBeacon启动脚本以更改此行:
sudo hciconfig $BLUETOOTH_DEVICE leadv 0
to: 至:
sudo hciconfig $BLUETOOTH_DEVICE leadv 3
Alternatively, you can try simply shutting off whatever app is running that is trying to establish a Bluetooth LE connection to the Raspberry Pi. 或者,您可以尝试简单地关闭正在尝试建立与Raspberry Pi的蓝牙LE连接的正在运行的应用程序。 If you don't know what app is doing that, though, this may be hard.
但是,如果你不知道应用程序在做什么,这可能很难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.