This is very simple and easy to use library, allowing you to discover other devices on a Wifi network that implement the same basic protocol.
This library uses a simple JSON protocol, over UDP (User Datagram Packets). UDP allows you to send packets over a Wifi network, including broadcasts which are sent to all connected devices.
When you send a discovery request, you broadcast a message that asks all devices on the network to respond, making them visible to you.
{
"type": "discover",
"version": 1
}
When you receive a discovery request, you can choose to send a response which makes you visible to the requester. This is sent directly to the requester, not as a broadcast.
{
"type": "response",
"version": 1
}
The Gradle dependency is available via jCenter. jCenter is the default Maven repository used by Android Studio.
Add this to your module's build.gradle
file:
dependencies {
// ... other dependencies
compile 'com.afollestad:udpdiscovery:1.0.0'
}
This library makes managing these messages and broadcasts easy, along with parsing the data.
Discovering entities means finding other devices on that network.
Discovery.instance(this)
.discover(entity -> {
// Do something with the entity
});
This automatically broadcasts a discovery request, and waits for responses indefinitely.
You can manually send out further discovery requests with the refresh method:
Discovery.instance(this).refresh();
Responding to entities means sending responses for discovery requests. This makes you visible to the requester.
Discovery.instance(this)
.respond(entity -> {
// If you return true, a response is sent to the entity, making you visible
return true;
});
As seen in the sample project, you can provide a second callback to the discover
and respond
methods to receive errors asynchronously. If these callbacks are not specified, exceptions are
thrown instead.
Discovery.instance(this)
.discover(entity -> {
// Entity discovered
}, throwable -> {
// Error related to discovery, or packet retrieval in general
})
.respond(entity -> {
// Discovery request received
return true;
}, throwable -> {
// Error related to discovery responses, or sending packets in general
});
Notice that you can also chain the discover
and respond
methods.
When you are done with the library, e.g. when your app closes, you should do cleanup.
Discovery.destroy();
I recommend doing this in onPause()
, and reinitializing in onResume()
when your app comes into
view again. You probably do not want to leak networking while your Activity
is in the background.
Any type of background networking needs to be done from a Service.