Humans are social animals.
When we're together, we get more done faster
by talking face-to-face.
But it's still hard to get devices that are
near each other to interact.
When they're together, they should work together.
My name is Andrew Bonner, and Nearby Messages
is a new proximity API to let Android and iOS
devices to communicate across short distances.
Nearby Messages is perfect for setting up
ad hoc groups, collaborative sessions,
or sharing resources to people in a colocated space.
It gives you a publish-subscribe API
to pass messages between phones that are within about 100 feet.
To make this work everywhere, it takes a bit more than magic.
Nearby Messages makes use of Wi-Fi,
Bluetooth, and even an ultrasonic modem that uses
the speakers and microphones.
We'll see how you can use that to restrict the range down
to about five feet.
Let's dive into a fun example, and see how Google Play Games
built the Nearby players feature to make it easier
to set up a multiplayer game.
Sending invites by scrolling through a buddy list,
or typing in user handles, is a pain.
We'll prepopulate the game invitations
by using who's Nearby and in-game.
For creating a gaming session, we
might want to publish an avatar image and maybe a gamer handle.
You can serialize this data however you like.
JSON, protocol buffers, CSV-- it's up to you.
Pass the serialized bytes to the constructor of the message
object, call publish, and we're off.
We also want to see what players are nearby.
The subscribe call takes a message listener.
It's on found method will fire when
the same app on a Nearby device calls publish.
Within the on found handler, we pull out the bytes
from the message with get contents, and modify our UI
to show the players to invite.
It's important to wait for the user
to take an action before calling publish or subscribe.
For example, a stateful button like this
is perfect to let users know Nearby is active.
Waiting for user intent is important,
because your app will need to prompt the user to get
permission to use Nearby.
Nearby introduces a new runtime permission,
consistent with how Android M handles permissions.
You don't need to add anything to your manifest.
And that's important, because your app
will continue to auto update.
Before calling publish or subscribe,
you want to check to see if your app has
permission to use Nearby with get permission status.
Return status object tells if you have a permission,
and gives you a way to ask for it if you don't.
You can see all this in our examples on GitHub.
The best part is it all works on iOS.
You can pod install Nearby messages,
and build a proximity experience across iOS and Android.
On iOS, when you call publish, will automatically
prompt the user with a prepermission dialog to explain
why the app needs microphone.
If your app already has mic access for other reasons,
we'll trim down the prepermission text.
Well, let's say you're publishing something more
sensitive than a gaming avatar.
Maybe an invite token, or a pointer to a document.
Nearby's default range of about 100 feet, or 30 meters,
might be too far for your taste.
Let's restrict Nearby to only using ultrasonic modem,
and get that range down to about five feet.
The publish call optionally takes a strategy object.
Just call set distance type earshot on that
before passing it to publish.
If your use case has natural send and receive rolls,
you can speed up the exchange by telling Nearby
which side should broadcast, and which should scan,
by calling set discovery mode.
So that's how to use the API at the code level.
There are four tips to keep in mind when you're designing
your user flow for Nearby.
The first is battery.
Nearby is doing a lot with the radios and sensors.
An active publish or subscribe will cause your app
to consume battery at two and a half to three and a half times
the normal rate.
So you don't want to have that publish or subscribe
going all the time.
Second, you should require an explicit action
before invoking Nearby-- a button
or switch to activate Nearby, or maybe a special screen.
You can find our standard icons for these
at developers.google.com/nearby.
Third is to avoid surprising your users.
The UI should make clear what data
will be visible to nearby users of your app
before you call publish.
Also, when Nearby is active we suggest
indicating this to your users with an animation.
Finally, exiting the app or leaving
the flow that requires Nearby means
you should stop any active publish or subscribe.
In your activities on pause method,
call unpublish and unsubscribe.
So that's Nearby messages.
A new way to reduce the friction of working or playing together
when your users are together.
Visit developers.google.com/nearby
to get started.
We can't wait to see what you build.
[MUSIC PLAYING]