要运行演示应用,请克隆仓库,并首先从 Example 目录运行 `pod install`。
> git clone [email protected]:beaconinside/BEACONinside-SDK-iOS.git
> cd BEACONinside-SDK-iOS/Example
> pod install
演示应用使用 NCIChartView Cocoapod 显示 iBeacon 信号强度的图表。
SDK 需要 iOS 7.x 来实现其功能。
我们支持 iOS 6.0 的部署目标,但是在 iOS 6 上运行时 SDK 不提供功能。
导入 SDK 头文件
#import <BEACONinsideSDK/BEACONinsideSDK.h>
创建一个 BIBeaconManager
实例。当您与 BEACONinside SDK 交互时,应该与这个类交互。您应该持有对 beacon 管理器的强引用(例如,使用属性)。
@property (nonatomic, strong) BIBeaconManager *beaconManager;
...
self.beaconManager = [[BIBeaconManager alloc] init];
接下来,我们告诉 beacon 管理员监视一个特定的 beacon 区域。当设备进入或离开指定的 iBeacon(s) 的范围时,我们会以调用块的形式获得回调。
NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:BIBeaconDefaultProximityUUID];
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID identifier:@"My Beacon Region"];
[self.beaconManager startMonitoringForRegion:self.monitoredRegion
didEnterRegionHandler:^(CLBeaconRegion *region) {
NSLog(@"Entered region: %@", region.identifier);
} didExitRegionHandler:^(CLBeaconRegion *region) {
NSLog(@"Exited region: %@", region.identifier);
} errorHandler:^(CLBeaconRegion *region, NSError *error) {
NSLog(@"Error monitoring region: %@", error);
}];
当您的应用进入后台甚至被终止时,区域监视将继续。如果您从处理块中发布本地通知,您的应用可以在不活跃时通知用户他们已进入或离开了 beacon 区域。
您还可以监视设备如何从一个 iBeacon 移动到另一个在较大 beacon 区域内的 iBeacon。当最近接近设备的 beacon 发生变化时,此代码将通知您(再次通过调用您指定的处理块)
CLBeaconRegion *beaconRegion = ...
[self.beaconManager startMonitoringNearestBeaconInRegion:beaconRegion
updateHandler:^(CLBeaconRegion *region, BIBeacon *nearestBeacon, NSError *error) {
if (error) {
NSLog(@"Error monitoring nearest beacon change: %@", error)
return;
}
NSLog(@"The nearest beacon is now: %@", nearestBeacon);
}];
最后,您可以告诉 beacon 管理员为指定区域内当前在范围内的所有 beacon 提供信号强度的连续更新。在这种情况下,处理块将被大约每秒调用一次,并将传递您在指定区域内看到的所有 beacon 的列表,按信号强度排序。该列表包括 beacon 管理员在指定区域内看到的全部 beacon,即使它们目前不在范围内。
CLBeaconRegion *beaconRegion = ...
[self.beaconManager startContinuousRangingInRegion:beaconRegion
updateHandler:^(CLBeaconRegion *region, NSArray *smoothedBeacons, NSError *error)
{
if (error) {
NSLog(@"Continuous ranging error: %@", error)
return;
}
NSLog(@"List of beacons: %@", smoothedBeacons);
}];
Cornelius Rabsch,BEACONinside GmbH
让我们知道如何通过 BEACONinside 支持您开发出色的移动应用程序。只需发给我们一条消息。
要查看最近版本的更改内容,请参阅变更日志。
BEACONinsideSDK遵循MIT许可协议。更多详情请参阅LICENSE文件。