gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:47:23 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com