BlueRange SDK 是一个基于 Android 和 iOS 的库,使应用程序能够与 Relution SmartBeacons 交云。SmartBeacons 可以通过构建基于 FruityMesh 信标固件的 BLE 网络(mesh) 遥距管理、监控、更新和配置,从而始终连接到云端。
目前,BlueRange SDK 支持 iOS 8.0 及以上版本的设备和 API 级别 18 或更高版本的 Android 设备。然而,基于 BLE 广告的特性在 Android 设备上(API 级别 21)需要蓝牙 LE 外设模式。
BlueRange SDK 由核心层和服务层组成。
核心层 包含一系列组件,可以简化信标消息流处理(例如 iBeacon 或 Eddystone 消息)。这些消息处理组件可以组合起来,形成一个灵活的事件驱动架构。
服务层 位于核心层之上,将您的应用程序与 Relution 连接起来。
具体支持以下功能:
docs
文件夹中的API参考。将构建命令(如下所述)生成的BlueRangeSDK.framework
包含在您的目标"嵌入式二进制文件"中。要做到这一点,请选择您的Xcode项目目标,并按"通用"标签中的"嵌入式二进制文件"部分中的"+"按钮。然后,单击"添加其他"并添加BlueRangeSDK.framework
文件。
可以使用以下导入方案导入BlueRange SDK的头文件:
#import <BlueRangeSDK/<HeaderFile>.h>
为了构建包含ARM和Intel架构符号的通用(胖)二进制文件
./Build.sh
SDK二进制文件可以在BlueRangeSDK-<VERSION>/ios/BlueRangeSDK.framework
中找到。
仅构建ARM架构
./Build_ARM_only.sh
SDK二进制文件可以在build/Debug-iphoneos/BlueRangeSDK.framework
中找到。
./GenerateDocs.sh
以下部分显示了可能有助于您将库集成到应用程序中的代码示例。
如果您的应用程序应连接到Relution,请使用RelutionIoTService
。如下所示,您必须在启动服务之前配置服务。
// .h
#import <BlueRangeSDK/BRRelutionIoTService.h>
@interface <YourClass> : NSObject<BRLoginObserver>
@property BRRelutionIoTService* relutionIoTService;
@end
// .m
#import <BlueRangeSDK/BRRelutionIoTService.h>
- (void) startRelutionIoTService {
NSString* baseUrl = @"http://iot.relution.io";
NSString* username = @"your_relution_username";
NSString* password = @"your_relution_password";
self->_relutionIoTService = [[BRRelutionIoTService alloc] init];
[self->_relutionIoTService setLoginData:baseUrl
andUsername:username andPassword:password andLoginObserver:self];
[self->_relutionIoTService setLoggingEnabled:true]; // Logging
[self->_relutionIoTService setCampaignActionTriggerEnabled:true]; // Campaigns
[self->_relutionIoTService setHeatmapGenerationEnabled:true]; // Heatmap
[self->_relutionIoTService setHeatmapReportingEnabled:true]; // Analytics
[self->_relutionIoTService start];
}
- (void) onLoginSucceeded {
}
- (void) onLoginFailed {
}
- (void) onRelutionError {
}
要校准iBeacon消息的RSSI值,只需将设备放置在信标1米远的位置
并将基于约10秒测量的平均RSSI值发送到Relution,方法如下所示调用calibrateIBeacon
:
// .h
#import <BlueRangeSDK/BRRelutionIoTService.h>
@interface <YourClass> : NSObject<BRBeaconMessageObserver>
@property BRRelutionIoTService* relutionIoTService;
@end
// .m
#import <BlueRangeSDK/BRRelutionIoTService.h>
#import <BlueRangeSDK/BRIBeaconMessage.h>
- (void) registerBeaconMessageObserver {
[BRRelutionIoTService addBeaconMessageObserver:self];
}
- (void) onMessageReceived: (BRBeaconMessage*) message {
// Do something with the message.
if ([message isKindOfClass:BRIBeaconMessage]) {
// Get the iBeacon message
BRIBeaconMessage* iBeaconMessage = (BRIBeaconMessage*)message;
// User moves to a place 1 meter away from the beacon that sends the iBeacon message...
// Calibrate the iBeacon message.
[BRRelutionIoTService calibrateIBeacon:iBeaconMessage.iBeacon withTxPower:iBeaconMessage.rssi];
}
}
为在Relution"活动"部分中定义的操作注册事件监听器。
// .h
#import <BlueRangeSDK/BRRelutionIoTService.h>
@interface <YourClass> : NSObject<
BRBeaconNotificationActionObserver,
BRBeaconContentActionObserver,
BRBeaconTagActionObserver>
@property BRRelutionIoTService* relutionIoTService;
@end
// .m
#import <BlueRangeSDK/BRRelutionIoTService.h>
- (void) registerCampaignActionObservers {
// Get informed about to campaign actions.
[BRRelutionIoTService addBeaconNotificationActionObserver:self];
[BRRelutionIoTService addBeaconContentActionObserver:self];
[BRRelutionIoTService addBeaconTagActionObserver:self];
}
- (void) onNotificationActionExecuted: (BRBeaconNotificationAction*) notificationAction {
}
- (void) onContentActionExecuted: (BRBeaconContentAction*) contentAction {
}
- (void) onTagActionExecuted: (BRBeaconTagAction*) tagAction {
}
要启用热力图报告,只需使用BeaconAdvertiser
类开始广播发现消息。
// .m
#import <BlueRangeSDK/BRBeaconAdvertiser.h>
// ...
[[[BRBeaconAdvertiser alloc] init] startAdvertisingDiscoveryMessage];
// ...
如果您使用Relution标签进行邻近消息,请注册一个RelutionTagObserver
以获取所有接收到的Relution标签的通知。如果您需要访问Relution标签的名称或描述,只需调用getTagInfoForTag
// .h
#import <BlueRangeSDK/BRRelutionIoTService.h>
@interface <YourClass> : NSObject<
BRRelutionTagObserver>
@property BRRelutionIoTService* relutionIoTService;
@end
// .m
#import <BlueRangeSDK/BRRelutionIoTService.h>
#import <BlueRangeSDK/BRRelutionTagInfoRegistry.h>
#import <BlueRangeSDK/BRRelutionTagMessage.h>
- (void) registerRelutionTagObserver {
[BRRelutionIoTService addRelutionTagObserver:self];
}
- (void) onTagReceived: (long) tag message: (BRRelutionTagMessage*) message {
@try {
[BRRelutionIoTService getTagInfoForTag:tag];
} @catch(BRRelutionTagInfoRegistryNoInfoFound* exception) {
// ...
}
}
如果您想构建独立于Relution的Beacon感知应用,请使用核心层。
启动扫描仪,如下所示。即使扫描仪正在运行,您也可以更改扫描仪的配置。
// .h
#import <BlueRangeSDK/BRBeaconMessageScanner.h>
#import <BlueRangeSDK/BRBeaconMessageScannerConfig.h>
@interface SystemTestsApplication : NSObject<BRBeaconMessageStreamNodeReceiver>
@property (strong) BRIBeaconMessageScanner* scanner;
@end
// .m
#import <BlueRangeSDK/BRBeaconMessageScanner.h>
#import <BlueRangeSDK/BRBeaconMessageScannerConfig.h>
- (void) startScanning {
self->_scanner = [[BRBeaconMessageScanner alloc] initWithTracer:[BRTracer getInstance]];
BRBeaconMessageScannerConfig *config = [self->_scanner config];
[config scanIBeacon:@"b9407f30-f5f8-466e-aff9-25556b57fe6d" major:45 minor:1];
[config scanIBeacon:@"c9407f30-f5f8-466e-aff9-25556b57fe6d" major:46 minor:2];
[config scanRelutionTagsV1:[[NSArray alloc] initWithObjects:
[NSNumber numberWithLong:13], [NSNumber numberWithLong:2], nil]];
[config scanJoinMeMessages];
[self->_scanner addReceiver:self];
[self->_scanner startScanning];
}
- (void) onMeshActive: (BRBeaconMessageStreamNode *) senderNode {
// Do something
}
- (void) onReceivedMessage: (BRBeaconMessageStreamNode *) senderNode withMessage: (BRBeaconMessage*) message {
// Do something
}
- (void) onMeshInactive: (BRBeaconMessageStreamNode *) senderNode {
// Do something
}
如果您想在稍后的时间处理Beacon消息,可能需要在设备上持久化地保存它们并在稍后读取。为此,您可以使用提供此目的便捷且线程安全的接口的BeaconMessageLogger
。在大多数情况下,您会将扫描仪传递给日志记录器的构造函数。但是,如果您的消息处理流程更复杂,您可以传递任何实现BeaconMessageStreamNode
接口的消息处理组件。接收到的消息将被传递给所有附加到日志记录器的接收者。因此,您可以将日志记录器用作静默消息持久化器
// .h
#import <BlueRangeSDK/BRBeaconMessageScanner.h>
#import <BlueRangeSDK/BRBeaconMessageScannerConfig.h>
#import <BlueRangeSDK/BRBeaconMessageLogger.h>
@interface SystemTestsApplication : NSObject<BRBeaconMessageStreamNodeReceiver>
@property (strong) BRIBeaconMessageScanner* scanner;
@property (strong) BRBeaconMessageLogger* logger;
@end
// .m
#import <BlueRangeSDK/BRBeaconMessageScanner.h>
#import <BlueRangeSDK/BRBeaconMessageScannerConfig.h>
#import <BlueRangeSDK/BRBeaconMessageLogger.h>
- (void) startLogging {
// Configure Beacon scanner
self->_scanner = [[BRBeaconMessageScanner alloc] initWithTracer:[BRTracer getInstance]];
BRBeaconMessageScannerConfig *config = [self->_scanner config];
[config scanIBeacon:@"b9407f30-f5f8-466e-aff9-25556b57fe6d" major:45 minor:1];
[config scanIBeacon:@"c9407f30-f5f8-466e-aff9-25556b57fe6d" major:46 minor:2];
[config scanRelutionTagsV1:[[NSArray alloc] initWithObjects:
[NSNumber numberWithLong:13], [NSNumber numberWithLong:2], nil]];
[config scanJoinMeMessages];
// Configure BeaconMessageLogger
self->_logger = [[BRBeaconMessageLogger alloc] initWithSender:self->_scanner];
[self->_logger addReceiver:self];
[self->_scanner startScanning];
}
- (void) onMeshActive: (BRBeaconMessageStreamNode *) senderNode {
// Do something
}
- (void) onReceivedMessage: (BRBeaconMessageStreamNode *) senderNode withMessage: (BRBeaconMessage*) message {
// Do something
}
- (void) onMeshInactive: (BRBeaconMessageStreamNode *) senderNode {
// Do something
}
如果您想一步内消耗日志中保存的所有消息,您可以使用readLog
方法。但是,如果您的日志包含大量消息,最好使用日志迭代器或for each循环来减少内存消耗。迭代器将按它们被保存的顺序加载消息。正在针对线程安全和性能进行优化。
id<BRLogIterator>* logIterator = [self->_logger getLogIterator];
while ([logIterator hasNext]) {
BRBeaconMessage* message = [logIterator next];
// Do something
}
如果您想减少整体的消息吞吐量或平均接收到的信号强度(RSSI),则消息聚合可能很有用。BeaconMessageAggregator
目前支持两种模式:数据包模式和滑动窗口模式。数据包模式结合接收了一个特定时间内的等效消息流,而滑动窗口模式则保持流中相同数量的消息同时使用移动平均滤波器平均RSSI
// .h
#import <BlueRangeSDK/BRBeaconMessageScanner.h>
#import <BlueRangeSDK/BRBeaconMessageScannerConfig.h>
#import <BlueRangeSDK/BRBeaconMessageAggregator.h>
#import <BlueRangeSDK/BRLinearWeightedMovingAverageFilter.h>
- (void) startAggregating {
// Configure scanner
self->_scanner = [[BRBeaconMessageScanner alloc] initWithTracer:[BRTracer getInstance]];
BRBeaconMessageScannerConfig *config = [self->_scanner config];
[config scanIBeacon:@"b9407f30-f5f8-466e-aff9-25556b57fe6d" major:45 minor:1];
[config scanIBeacon:@"c9407f30-f5f8-466e-aff9-25556b57fe6d" major:46 minor:2];
[config scanRelutionTagsV1:[[NSArray alloc] initWithObjects:
[NSNumber numberWithLong:13], [NSNumber numberWithLong:2], nil]];
[config scanJoinMeMessages];
// Configure aggregator
BRBeaconMessageAggregator* aggregator = [[BRBeaconMessageAggregator alloc]
initWithTracer:[BRTracer getInstance] andSender:self->_scanner];
[aggregator setAggregationMode:AGGREGATION_MODE_SLIDING_WINDOW];
[aggregator setAggregateDurationInMs:5*1000];
[aggregator setAverageFilter:[[BRLinearWeightedMovingAverageFilter alloc] initWithC:0.3]];
}
要定期发送广告消息,只需调用BeaconAdvertiser
类的任一start
方法。
// .m
#import <BlueRangeSDK/BRBeaconAdvertiser.h>
- (void) startAdvertising {
self.beaconAdvertiser = [[BRBeaconAdvertiser alloc] init];
[self.beaconAdvertiser startAdvertisingDiscoveryMessage];
}
* Copyright (c) 2017, M-Way Solutions GmbH
* All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://apache.ac.cn/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.