In July 2015, Google released the Nearby API as a part of Google Play services. It gives developers a new way to exchange data between devices based on their proximity. The API is divided into two subcomponents: Nearby Messages and Nearby Connections.
Nearby Connections relies solely on Wi-Fi and allows for real-time communication between devices connected to the same local network. First use case that comes to mind is multiplayer gaming, but Google also suggests it could turn a phone into an Android TV’s game controller.
Nearby Messages, on the other hand, uses a more sophisticated and interesting mechanism to determine proximity: it combines Wi-Fi, Bluetooth, speaker and microphone to establish a contextual pairing between all nearby devices. That alone made me want to play with it, since I’ve been recently drafting my own solution for local, P2P-like communication, but totally independent from any server (as opposed to last year’s famous FireChat, which requires an account) and Google Play services.
While Nearby Messages doesn’t require a Google Account, the actual communication is handled by Google’s servers, so each device has to be connected to the Internet, which also implies that there’s a data payload limit of 100 KB, and the recommended optimal size is only 3 KB for a single message (see: Message class). However, after the neighbouring devices have been discovered and the pairing was established, it’s up to developers to decide if they want to stick with Nearby API for further messages exchange, or transparently redirect the app to use their own implementation (whether it’s a server or local BT/Wi-Fi Direct communication).
There’s a lot more of interesting stuff about the API, but here are the 2 facts that you won’t find in the official documentation:
- There’s currently no limit to number of devices communicating over Nearby API.
- While it is claimed that broadcasting/scanning for token with speaker/microphone is based on inaudible ultrasonic sounds, you can actually hear your speaker working quite clearly (at least on a Samsung Galaxy S2 and Nexus 5 that I’ve been testing with) when scanning is active.
Giving it a try
To familiarize myself with the API, I’ve started to work on a simple app called Polly, which – you guessed it – allows to conduct polls and surveys with people around you (at a conference, for example). I’m not sure if it will end up as a full release to Google Play (it would be nice to have an iOS companion app) or just one of many working prototypes, as I call my unfinished projects.
What’s in the box? Mainly ButterKnife, Dagger, Timber and Otto. I’ll probably also refactor
PollRepository and its clients to make use of RxAndroid.