我使用Nearby来检测信标。这是我的代码
- (void)viewDidLoad {
[super viewDidLoad];
[self setup];
[self startScanning];
}
-(void)setup {
[GNSMessageManager setDebugLoggingEnabled:YES];
void (^paramsBlock)(GNSMessageManagerParams *) = ^(GNSMessageManagerParams *params) {
params.bluetoothPermissionErrorHandler = ^(BOOL hasError) {
if (hasError) {
NSLog(@"Nearby works better if Bluetooth use is allowed");
}
};
params.bluetoothPowerErrorHandler = ^(BOOL hasError) {
if (hasError) {
NSLog(@"Nearby works better if Bluetooth is turned on");
}
};
};
_messageManager = [[GNSMessageManager alloc] initWithAPIKey:@"API-KEY" paramsBlock: paramsBlock];
}
- (void)startScanning {
// Create a subscription that scans for nearby beacons.
NSString *(^stringFromData)(NSData *) = ^(NSData *content) {
return [[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding];
};
GNSBeaconStrategy *beaconScanStrategy =
[GNSBeaconStrategy strategyWithParamsBlock:^(GNSBeaconStrategyParams *params) {
params.includeIBeacons = NO;
}];
GNSMessageHandler messageFoundHandler = ^(GNSMessage *message) {
};
GNSMessageHandler messageLostHandler = ^(GNSMessage *message) {
};
_subscription = [_messageManager
subscriptionWithMessageFoundHandler:messageFoundHandler
messageLostHandler:messageLostHandler
paramsBlock:^(GNSSubscriptionParams *params) {
params.deviceTypesToDiscover = kGNSDeviceBLEBeacon;
params.beaconStrategy = beaconScanStrategy;
}];
}
Beacon被正确检测到,但是每隔5分钟就会在附近调用messageLostHandler方法并且:
当不再观察到消息(设备超出范围或不再发布消息)时,将调用消息丢失处理程序。
来源:https://developers.google.com/nearby/messages/ios/pub-sub
有人可以告诉我为什么每5分钟调用一次这个方法吗?我不得不说灯塔和设备停留在同一个地方,所以我仍然在灯塔区域。 其余的附近功能工作正常。
这对我来说非常重要,因为当附近检测到信标时,我会连接到他并做出另一个动作。有人有一些想法吗?