swift - Google Nearby for Swift - 如何仅发现音频?

标签 swift audio google-nearby

我正在使用 Google 的 Nearby 并尝试在不使用 BLE 或 Classic BT 的情况下(仅)使用 discoveryMediums Audio。原因是因为我希望发现发生在一个房间里,而不是穿过墙壁流血。目前我有这段代码。如果我在运行该应用程序的 iPhone 上关闭 BT,我会被告知它需要 Nearby 才能工作。我一定是遗漏了一些基本的东西。

func startSharingWithName(name: String) {
    if let messageMgr = self.messageMgr {
        // Show the name in the message view title and set up the Stop button.
        messageViewController.title = name

        // Publish the name to nearby devices.
        let pubMessage: GNSMessage = GNSMessage(content: name.dataUsingEncoding(NSUTF8StringEncoding,
            allowLossyConversion: true))
        publication = messageMgr.publicationWithMessage(pubMessage)            

        // Subscribe to messages from nearby devices and display them in the message view.            

        let params: GNSSubscriptionParams = GNSSubscriptionParams.init(strategy:
            GNSStrategy.init(paramsBlock: { (params: GNSStrategyParams!) -> Void in
                params.discoveryMediums = .Audio
                params.includeBLEBeacons = false
            }))

        subscription = messageMgr.subscriptionWithParams(params,
                                                         messageFoundHandler:{[unowned self] (message: GNSMessage!) -> Void in
                                                            self.messageViewController.addMessage(String(data: message.content, encoding:NSUTF8StringEncoding))
            },
messageLostHandler: {[unowned self](message: GNSMessage!) -> Void in
                                                            self.messageViewController.removeMessage(String(data: message.content, encoding: NSUTF8StringEncoding))
            })  
    }
}`

最佳答案

您需要将 GNSStrategy 对象传递给订阅和发布。正如您对其进行编码一样,该出版物仍在使用 BLE 和音频。

我还建议放弃创建发布/订阅的非弃用方法。试试这个:

  let strategy = GNSStrategy.init(paramsBlock: { (params: GNSStrategyParams!) -> Void in
      params.discoveryMediums = .Audio
  })

  publication = messageMgr.publicationWithMessage(pubMessage, paramsBlock: { (pubParams: GNSPublicationParams!) in
    pubParams.strategy = strategy
  })

  subscription = messageMgr.subscriptionWithMessageFoundHandler({[unowned self] (message: GNSMessage!) -> Void in
    self.messageViewController.addMessage(String(data: message.content, encoding:NSUTF8StringEncoding))
  }, messageLostHandler: {[unowned self](message: GNSMessage!) -> Void in
    self.messageViewController.removeMessage(String(data: message.content, encoding: NSUTF8StringEncoding))
  }, paramsBlock: { (subParams: GNSSubscriptionParams!) -> Void in
      subParams.strategy = strategy
  })

关于swift - Google Nearby for Swift - 如何仅发现音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37598358/

相关文章:

ios - 如何在创建另一个同名 SKSpriteNode 后与 SKSpriteNode 交互?

java - 更改来自类的事件上的按钮

android - Google Nearby 通知 API 不显示新通知

ios - 如何在不让 UIView 移动到角落的情况下使用大小约束?

Swift:如何在子类的init方法中对指令进行排序

ios - 计算tableView中每个单元格的值

java - Android 中音频文件的 FFT

ios - 将图像和标题添加到锁屏媒体控件

安卓:附近的api,没有连接

android - Google 附近连接 Api - 不幸的是 Play 服务已停止