我正在使用Google的“附近”并尝试使用discoveryMediums Audio(仅限)而不使用BLE或Classic BT。原因是因为我希望发现在一个房间里发生而不是在墙壁上流血。目前我有这个代码。如果我在运行应用程序的iPhone上关闭BT,我会告知它需要附近的工作。我必须遗漏一些简陋的东西。
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))
})
}
}`
答案 0 :(得分:2)
您需要将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
})