1

So I am working with trying to transfer data between two phones with Google Nearby Connections.

For example, say I want to transfer a variable from each phone to another and then show a toast containing the text from the other phone.

I have been looking into the Connections API but I can't seem to limit it to when two phones are really close to each other. I have seen that the Nearby Messages API has an Earshot distance option.

But there isn't an option for earshot distance in the Nearby Connections API since the Nearby messages API doesn't seem to contain what I need to make something like the example above work.

Hope you can help me :)

  • Does this answer your question? [Google Nearby Connections set limit on connection distance like Nearby Messages](https://stackoverflow.com/questions/54153355/google-nearby-connections-set-limit-on-connection-distance-like-nearby-messages) – afarre Dec 19 '20 at 16:48

3 Answers3

1

There hadn't been a feature request for it until now, which is why there's no support in Nearby Connections.

I believe Nearby Messages would support your use case, though. It can exchange a few KB of data to other devices nearby. It's not a live bi-directional channel, like Nearby Connections is, but it doesn't sound like you need that.

Xlythe
  • 1,958
  • 1
  • 8
  • 12
  • So to explain my use case precisely. I need something where I can click a button and find another phone that I very close to me and exchange a variable from each phone so each phone will get the variable from the other phone. Then say variable from phone1 is false and variable from phone2 is true it would need to on both phones a check to see if the other variable is == to something is that achievable with Nearby Messages or Nearby Connections with short-range? – danilkp1234 Sep 28 '20 at 18:55
  • Do this have to work in the background or can both phones have the app open at the same time? You could have the phone with the button broadcast first something like... Proto{startingNewSession=true, sessionId=randomNumber, variable=XXX}, broadcast that for a second or two, and then listen for the response Proto{startingNewSession=false, sessionId=hostNumber, variable=YYY}. It's not the most efficient, but if it's just one message back and forth then it should be fine. – Xlythe Sep 29 '20 at 15:47
  • It would be fine to it to just work when people click a button search button in the app. Do you have an example with this or something similar I can look at :) – danilkp1234 Sep 29 '20 at 18:03
  • I just quickly wrote this in notepad (don't mind syntax errors) if this gives you a better idea of what I am trying to do https://pastebin.com/5VuA7JfM – danilkp1234 Sep 30 '20 at 13:20
  • You should be able to do that with Pub/Sub: https://developers.google.com/nearby/messages/android/pub-sub – Xlythe Sep 30 '20 at 16:06
  • If both sides need the app open regardless, though, distance may not be a large problem. It'll be rare for 2 people to be on standby w/ the app in the foreground. – Xlythe Sep 30 '20 at 16:07
  • I want to limit the distance so I can have for example 5 apps running the app and you only connect to the phone closest to you. How would I make it so all 5 phones can find each other and when two phones make a connection then make a toast on each device telling the value from the other device – danilkp1234 Sep 30 '20 at 16:26
  • How don't really see how I can make each phone show a toast containing the text from the other phone? and how could it be done since Nearby Messages doesn't support bi-directional communication – danilkp1234 Sep 30 '20 at 16:29
  • I have been looking at the documentation for both Nearby Connections and Nearby Messages. And I can't really see how to do it as I explained above where it only finds the closest device with more devices running the app. Do a check and display something different for each device like in the Pastebin above – danilkp1234 Oct 03 '20 at 13:27
  • Do you know how I can do this? :) – danilkp1234 Oct 05 '20 at 09:33
  • Sorry, I don't have the time to dive deeper into a solution :( – Xlythe Oct 05 '20 at 17:56
  • That's fine but can you take into consideration to implement distance earshot into Nearby Connections. Nearby Connections is also missing the feedback button on the documentation pages https://developers.google.com/nearby/connections/overview Nearby Messages has a feedback button. So I think Nearby Connections should also have one right? – danilkp1234 Oct 06 '20 at 11:01
  • Where do you see the feedback button on the Nearby Messages doc? I couldn't find it when I looked. I can file a feature request for adding it to Connections, but I want to attach a screenshot so the developer knows what they're supposed to be adding. – Xlythe Oct 12 '20 at 16:19
  • On this page is the feedback button: https://developers.google.com/nearby/messages/android/get-started Here you have a screenshot https://prnt.sc/uxx49w btw hope you can look into adding a feature for limiting distance :) – danilkp1234 Oct 12 '20 at 17:29
0

Nearby connections uses bluetooth, BLE (Bluetooth Low Emissions) and WiFi. This signals can't be adjusted to reach a certain distance.

Nearby messages, on the other hand, also uses ultrasounds which can limit the phone's reach to a much shorter distance. See this video. It's also stated in the documentation of messages.

So your options are either fully offline connections without distance setting, or online messages with distance setting. Note that messages is not fully online, it only publishes the message to be shared in a google server. The key necessary to retrieve that message is sent offline between users.

afarre
  • 512
  • 6
  • 18
  • Yes, but I still can't see how I would use this for my use case even with nearby messages. Since I would need bi-directional communication for each device to send a userID and each device checks if device 1 userID is higher than device 2 then show to device 2 "you lost" and show to device 1 "you won" if that makes sense :) – danilkp1234 Dec 21 '20 at 20:35
  • As I said, with messages the devices only exchange a key needed to retreive a message in google's servers. So if you have two devices, both must publish their userID in the google server. Then both share their keys with the other. With the key, each one reads the message. Now that you have the userID, check if it's higher or lower, publish the results in the google server, and again, share the key so that both devices get the message. With messages there is no direct communication other than sharing the key necessary to read the correct message. – afarre Dec 22 '20 at 10:16
  • Yeah, I got that. But I am still wondering how to code it – danilkp1234 Dec 23 '20 at 12:32
  • Depends on the framework. In my case, I developed a nearby connections app in flutter. I used a package that gave you the basic calls such as Nearby.startAdvertising() or Nearby.sharePayload(), which made it quite easy. If you are using android, check for examples which I bet there are plentiful, or even the Nearby official docs has some android code too. – afarre Dec 23 '20 at 18:28
  • Yeah, I have looked a lot at the docs and GitHub repos for examples but I can't seem to find any example of someone trying to achieve something like what I am trying. I have already looked a the Nearby libraries for Android Studio – danilkp1234 Dec 24 '20 at 15:51
  • Good luck searching. In any case, how to code it is not part of this stack question. If you want more help in that regard, I recommend you making a new, more specific, question for this issue. – afarre Dec 24 '20 at 18:22
0

I believe that your precise requirement for distance is what is being addressed in this Bluetooth Core Specification 5.1. Therefore, the solution is pending on:

  • Bluetooth hardware chips 5.1 will happen
  • Smartphone manufacturer will use these chips
  • Android Bluetooth library is updated to exploit the new feature of this chip
daparic
  • 3,794
  • 2
  • 36
  • 38