Internet of Things (IoT) is no longer something reserved for the future or limited to the realm of ideas. It is real and it’s begun to find its way into our homes turning lamps, locks, security cameras, and several other home appliances into smart devices controlled by your smartphone.

The crucial point, as a developer, is to know how to find and access these devices, allowing people to use this technology to its best extent. To help you in this process, this article lists some of the most popular approaches to finding and connecting to any device in your network when developing IoT apps, and presents some code snippets and examples to get you up and running in no time.

1. DIY – Network scanning

Let’s start with the most basic way, because not all devices implement fancy service discovery protocols. In this example, we need to do all the discovery and connection ourselves. So we roll up our programming sleeves, get our local network netmask and calculate the range of possible IPs.

This gets you the base network address and the number of possible devices, which will be at least 255 for local networks, and therefore determines the length of our for loop. Inside this loop, we’ll ping every one of these addresses and log the ones that ACKed us for later connection.

Unfortunately, this only gets you a list of connected IPs, so there’s still an extra step in identifying whether a particular IP corresponds to your smart device or is the smartTV hanging on your wall. Since we’re close to the metal here, the obvious course of action is opening a socket and connecting to a pre-defined port.

If you get a valid connection that passes through your validation process, you just found your device! Otherwise, rinse and repeat until you find it. Needless to say that the above code, as every network operation in Android, must not run in the UI.

2. Automating the discovery – zeroconf/Bonjour

Let’s be honest: scanning the network is pretty similar to starting a fire by rubbing sticks – it takes time and doesn’t seem very reliable. This is especially true if your network has the bad habit of losing some packages or has DHCP enabled, which may force you to execute the discovery task frequently.

To overcome this, there’s zeroconf (or Bonjour for Apple products), a blazing fast network discovery tool, with the downside of needing to be configured in your Android app and the IoT device.

The general idea is that one device broadcasts its services to the network and another listens to these broadcasts until it finds something of interest. The latter then resolves the IP of the advertiser of that service and can start communicating.

In Android, this goes by another name: Network Service Discovery ( NSD ). All you need to make it work is the NsdManager class and to know the name of the service you’re looking for.

You will still need to do something with this IP, i.e. open a connection by yourself, maybe using the socket from the first section to send/receive messages. The next section will discuss how to avoid doing even that.

3. Transparent network – AllJoyn®

Designed by the AllSeen Alliance , since 2013, a cross-company consortium composed by some big companies such as Microsoft, Philips, and AT&T, AllJoyn® defines a common protocol for device discovery and communication and has SDKs for all major platforms. In their words:

AllJoyn® is an open source software framework that makes it easy for devices and apps to discover and communicate with each other. Developers can write applications for interoperability regardless of transport layer, manufacturer, and without the need for Internet access.

The framework works with the concept of Sessions that are created by at least 2 devices connected through a virtual Bus. In Java, a certain device can emit and receive signals, which are messages broadcasted to any device belonging to the same Session.

So first we need to create a BusObject , which will be used to send our messages. However, to do so we need to instantiate a BusAttachment object which is our main interface with the framework API. The BusAttachment is the entity that allows us to connect to a session and find other devices.

You may be wondering what the BusListener object is. This is the interface used to notify when a new service is discovered or when a found one is lost. It will only work after we start our network discovery.

After you find the service with the name you were looking for, you should join it before being able to receive and send messages. Joining requires you to set the connection port, that was probably pre-defined, and other options related to the session you’re about to connect, which can be left empty if you’re not clear on them.

Wrapping up

Although not a comprehensive guide on all approaches to discovering and connection to devices in a network, this article sheds some light on how it can be achieved, be it by writing the whole protocol yourself or with the help of some frameworks.

Whichever approach you choose, it is a great time to invest in developing IoT apps and creating solutions for a connected home, car or work place, and if you need some help with that,Cheesecake Labs is a great match for you.