1

我想使用我的 swift ios 应用程序使用谷歌附近的 api(不是 iBeacon api)扫描信标

我看到了 Google 开发者文档,并从同一个站点获取了 git 示例。

这是我的代码

我第一次在真正的 ios 设备上安装了该应用程序

但是永远不会调用foundand处理程序。lost

我仔细检查了捆绑 ID,公共 ios API 密钥(信标附件的相同谷歌控制台项目)

但它仍然无法在工作和注册的信标附近工作。

我也有一个成功扫描同一个信标的安卓应用程序。

我还能检查什么?

我的快速代码中缺少"Strategy"这段代码。

我怎样才能添加这个?为什么 github 示例中缺少此内容?

GNSStrategy *beaconStrategy = [GNSStrategy
    strategyWithParamsBlock:^(GNSStrategyParams *params) {
      params.includeBLEBeacons = YES;
    }];
    GNSSubscriptionParams *beaconParams = [GNSSubscriptionParams
        paramsWithMessageNamespace:@"com.mycompany.mybeaconservice"
                              type:@"mybeacontype"
                          strategy:beaconStrategy];
    _beaconSubscription = [_messageManager subscriptionWithParams:beaconParams
                                              messageFoundHandler:myMessageFoundHandler
                                               messageLostHandler:myMessageLostHandler];

在我的代码中:

func startScanning() {
    if let messageMgr = self.messageMgr {

        // Subscribe to messages from nearby devices and display them in the message view.
        subscription = messageMgr.subscriptionWithMessageFoundHandler({[unowned self] (message: GNSMessage!) -> Void in

            self.mainViewController.location_text.text = (String(data: message.content, encoding:NSUTF8StringEncoding))
            self.mainViewController.startRefreshTimer()
            },

            messageLostHandler: {[unowned self](message: GNSMessage!) -> Void in

                if (self.mainViewController.userState.enrollState == EnrollState.confirmPosition)
                {
                    self.mainViewController.stopRefreshTimer()
                    self.mainViewController.enrollButtonManager.setSearchingForBeaconsBtn()
                }
            })
    }
}
4

1 回答 1

1

您需要将 GNSStrategy 添加到您的订阅中,这样您就可以启用信标扫描。尝试这个:

let params: GNSSubscriptionParams = GNSSubscriptionParams.init(strategy:
    GNSStrategy.init(paramsBlock: { (params: GNSStrategyParams!) -> Void in
      params.includeBLEBeacons = true;
    }))
subscription = messageMgr.subscriptionWithParams(params,
    messageFoundHandler:{[unowned self] (message: GNSMessage!) -> Void in
      self.mainViewController.location_text.text = (String(data: message.content, encoding:NSUTF8StringEncoding))
      self.mainViewController.startRefreshTimer()
    },
    messageLostHandler: {[unowned self](message: GNSMessage!) -> Void in
      if (self.mainViewController.userState.enrollState == EnrollState.confirmPosition) {
        self.mainViewController.stopRefreshTimer()
        self.mainViewController.enrollButtonManager.setSearchingForBeaconsBtn()
      }
    })

默认情况下,信标扫描处于关闭状态,因为 iOS 在首次打开信标扫描时会向用户显示位置权限对话框,这对于使用 Nearby 库但不扫描信标的应用程序是不可接受的。

感谢有关 github 示例的反馈,该示例未显示如何扫描信标。我会看看添加它。

于 2016-02-01T01:32:36.803 回答