iOS和Google附近API:如何正确的发布和订阅?

iOS and Google Nearby API: how to publish and subscribe in the right way?

我已经做到了 the guide says

这是消息管理器

[GNSMessageManager setDebugLoggingEnabled:YES];
messageManager = [[GNSMessageManager alloc] initWithAPIKey:API_KEY paramsBlock:^(GNSMessageManagerParams *params) {
        params.bluetoothPowerErrorHandler = ^(BOOL hasError) {
            // Update the UI for Bluetooth power
        };
        params.bluetoothPermissionErrorHandler = ^(BOOL hasError) {
            // Update the UI for Bluetooth permission
        };
    }];

这些是我发布和订阅 Nearby API 的方法。

- (IBAction)onPublish:(id)sender {
    NSLog(@"publish");
    NSString* str = @"hello world";
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    GNSMessage* message = [GNSMessage messageWithContent:data];
    id<GNSPublication> publication = [messageManager publicationWithMessage:message paramsBlock:^(GNSPublicationParams *publicationParams) {
        publicationParams.strategy = [GNSStrategy strategyWithParamsBlock:^(GNSStrategyParams * strategyParams) {
            strategyParams.allowInBackground = YES;
            strategyParams.discoveryMediums = kGNSDiscoveryMediumsBLE;
            strategyParams.discoveryMode = kGNSDiscoveryModeDefault;
        }];;
    }];
}

- (IBAction)onSubscribe:(id)sender {
    NSLog(@"subscribe");
    id<GNSSubscription> subscription = [messageManager subscriptionWithMessageFoundHandler:^(GNSMessage *msg) {
        // Add the name to a list for display
        NSLog(@"message found %@", [msg description]);
    } messageLostHandler:^(GNSMessage *msg) {
        // Add the name to a list for display
        NSLog(@"message lost %@", [msg description]);
    } paramsBlock:^(GNSSubscriptionParams *subscriptionParams) {
        subscriptionParams.strategy = [GNSStrategy strategyWithParamsBlock:^(GNSStrategyParams * strategyParams) {
            strategyParams.allowInBackground = YES;
            strategyParams.discoveryMediums = kGNSDiscoveryMediumsBLE;
            strategyParams.discoveryMode = kGNSDiscoveryModeDefault;
        }];;
    }];
}

同时启用蓝牙中心和外设后台功能,并设置外设的权限字符串。 最后我订阅了一台 iOS 设备并从另一台设备发布但没有任何反应。

务必保留发布和订阅对象。他们在解除分配时停止 publishing/subscribing。通常的方法是将它们作为 properties/ivars 存储在您的 类.

之一中

开发人员文档在这一点上具有误导性,我深表歉意。我们将在下一个版本中改进文档。