0

I am using Google's Nearby and trying to use discoveryMediums Audio (only) without using BLE or Classic BT. The reason is because I want discovery to happen in a room and not bleed through walls. Currently I have this code. If I turn off BT on an iPhone running the app I'm informed it's required for Nearby to work. I must be missing something rudimentary.

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))
            })  
    }
}`

1 Answers1

2

You need to pass the GNSStrategy object to both the subscription and the publication. As you've coded it, the publication is still using both BLE and audio.

I would also recommend moving away from the non-deprecated methods that create publications/subscriptions. Try this:

  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
  })
Dan Webb
  • 348
  • 1
  • 7
  • Thank you very much. One thing I did notice was Audio only was not being stopped by walls. I need to keep Wi-Fi on for the devices as they use Google's cloud. Could the Wi-Fi be involved in discovery even though it's set to Audio (only) - do I need to say no BLE, etc? – Radagast the Brown Jun 03 '16 at 02:50
  • On iOS, there are only 2 discovery mediums: audio and BLE. WiFi isn't used. If you've set the GNSStrategy.discoveryMediums to audio, then it's not using BLE. Here's something you can try to make sure it's using just audio: Turn the volume down on the iPhone that's broadcasting audio. Our audio experts are telling me the near-ultrasonic audio can't significantly penetrate the thinnest barrier, even a cardboard box. Are there any gaps in the wall? Open windows, gaps under doors, anything like that? Are there any vents in the wall? – Dan Webb Jun 03 '16 at 18:03
  • I can verify I am now using Audio only. It works for discovery, but when one device goes out of range (behind a wall, 10+ feet away, etc.) the message is cached and the messageLostHandler doesn't fire for many minutes. I think it's the way the API works. It kind of caches - and you don't get accurate out of "earshot" accuracy. – Radagast the Brown Jun 03 '16 at 19:13
  • Thanks for the feedback. I believe you're right about the earshot concept being subverted by the long timeout in the cache. I've started a discussion of this issue with the team. – Dan Webb Jun 04 '16 at 16:29