Offline Data Transfer in Android

23 / Aug / 2017 by Shalini Prajesh 2 comments

Offline data transfer in Android is a way to transfer data among devices wirelessly without any network accessibility. There are many ways for connecting devices sans cables such as Bluetooth, NFC, Wifi Direct, SIP, etc. Despite the fact that a large audience is looking towards internet solutions, these ways have their unique use cases and concise solutions to problems. There are many instances where these options have played a magical role in providing brilliant results like offline gaming apps, offline file sharing apps, offline chat application, etc. Wifi Direct is one of the ways that is accepted as a solution to one of our client’s requirements. Wifi Direct have solved problems that occurred in rural areas.

Worldwide, 48% of the population uses the internet. In India, only 26% of the population have access to it. Nowadays, the internet has become an integral part of technology. What if your app has some functionalities that require it to be function-able even when the internet is not available. Based on such use case, we would be covering that how to transfer data between two devices when the internet is not available.

This technology has come to aid to workers who are in rural areas and have very less access to the internet. For a reference we can consider a real scenario, the government has Front Line Workers who manages the integrated Mother and Child Care System. An application is created to maintain this system and turn the written records into a database. These workers need to exchange data among themselves.

Data transfer between two nearby Android devices can be done in few ways like NFC, Bluetooth, Wifi P2P, USB or SIP. In our research, to implement this we have gone through every option available and analyzed their features. We’ll learn the succinct description of the mentioned methods and then see which one is the best fit.

Problem Statement

We have an app that is being used in remote areas in India where internet connectivity is not very good. Our application has three users that functions and have access to each other’s data. For instance, a User A uses data provided by User B and User B uses data generated by User C. These workers create digital medical records to ensure that the loop of care is complete. The application has to store all data locally in a database and then the user has to send this data to server timely, let’s say once in a month or weekly by going to the main office where the internet is available with good bandwidth. But in this process, there are challenges which user has to face that may result in few problems like data loss, irregular updates, inconsistent data, etc. And mainly when the internet is not available other users cannot access the updated data.


For this problem, we have come to a solution wherein we’ll try to update app’s database offline. We have explored methods in Android platform and we found the following:-

  • NFC: Near Field Communication (NFC) is a set of short-range wireless technologies, requiring a distance of 4cm or less to initiate a connection. NFC allows sharing small payloads of data between an NFC tag and an Android-powered device, or between two Android-powered devices. Since this offered small amount of data transfer, we were looking for an option that can be used to transfer a large volume of data. But if NFC is what you’ve been looking for, you can read the full guide here.
  • Bluetooth: By using Bluetooth, we can wirelessly transfer data between two Bluetooth devices. But Bluetooth operations require more battery intensive work and that was a problem for an app which may exist in rural areas. Bluetooth Low Energy (BLE) is designed to provide significantly lower power consumption, but again it is used to transfer small amounts of data.
  • Wifi P2P: Android’s Wifi peer-to-peer framework complies with Wi-Fi Alliance’s Wi-Fi Direct™ certification program. It is much faster than the Bluetooth. We can share a large amount of data between two devices over a speedy connection. This framework met our requirements completely. We will discuss it in more detail.
  • USB: It is used to transfer data between Android devices and USB Hardware. In this case, it cannot be done between two Android devices. To read more about USB, click here.
  • SIP: Session Initiation Protocol adds SIP-based internet telephony features to applications. Using SIP, you can transfer data in the form of voice calls and messages. Android includes a full SIP protocol stack and integrated call management services that easily let applications set up outgoing and incoming voice calls without having to manage sessions, transport-level communication or audio record or playback directly.

Wifi P2P

Prerequisite to implement this feature is the knowledge of Socket programming, the device should have Wifi Direct and your app should support WifiP2p APIs. Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi without an intermediate access point. While working on this implementation, we came across many problems whose solutions were not available on the official site.

There are three major steps which are followed to transfer data between two devices.

  • Discover the nearby devices available.
  • Connect with the device from the list of available devices.
  • After a stable connection is established, you can send and receive data over this connection.

For implementing these steps, we have WifiP2P APIs with the help of these APIs we’ll perform the connection between two devices. For a basic implementation of this feature, follow the official doc which is mentioned here.

We have come across Service discovery using WifiP2P, but this was required in our project. We got an idea about how can these be used in a product. The use of Service discovery is that when you want to discover only those devices which support some particular services. For instance, in the case of offline multiplayer game application, to make only those devices discoverable that has the same game installed.

If you go through the basic implementation given in the official doc, you will easily be able to send a photo/jpg file from one device to another device within a fraction of seconds. But the challenge here for us was to send data stored in our database to another device which has the same database but different data. It is like updating the database of one user from another. One device will act as a client and other will be a server. Following are the points that we faced while completing our solution for data transfer.

  1. After discovering peers connecting to a device, one of them is Group Owner(GO) in a network and other is a client. Using GO’s information client can send files to GO but not happening other way round. We have tried many demos on GitHub which implemented this but what we got is chat architecture where a small amount of data is transferred to and fro. But we want large files to transfer between two devices.
  2. Now we have decided to put one of the devices as GO(receiver) and other as a client(sender). Connection decides itself that who will be the Group Owner. For this, we have an attribute groupOwnerIntent which decides whether a device should be GO or not. After many attempts to make a device group owner, it was discovered that the groupOwnerIntent works for the first time and then that particular device is GO for a series of connections. This happened because when the connection between two devices takes place, a group is created between two devices. These groups can be seen in Android device WifiDirect setting where it shows available devices and remembered groups. When we remove these groups from the list, then the device can be set as GO based on the value of groupOwnerIntent. To eliminate these persistence group from the device, following method can do the required. Private void deletePersistentGroups() {try {Method[] methods = WifiP2pManager.class.getMethods();for (int i = 0; i < methods.length; i++) {if (methods[i].getName().equals(“deletePersistentGroup”)) {// Delete any persistent group               for (int netid = 0; netid < 32; netid++) {

    methods[i].invoke(mManager, mChannel, netid, null);




    } catch (Exception e) {




  3. After obtaining a stable connection between two devices, next problem that can occur is with Socket programming over which data is transferred. For ServerSocket, use background thread and manage your threads, so that transferring files using stream would not cause any problem.


After solving these problems, we came up with a stable solution that helped to meet the requirements of the application. Wifi Direct served the purpose to transfer data offline with good speed and connection.


comments (2)

  1. anu

    keep sharing your information regularly for my future reference,given articles was very excellent and easily observe all provided information. . . .


Leave a Reply

Your email address will not be published. Required fields are marked *