Streaming using Smartphone as relay via Bluetooth

Introduction:

Bluetooth connectivity in smartphones has a range. It varies from 10 – 12 meters. So, if two smartphones want to connect and the distance is more than the above mentioned figure, the main concept behind the problem statement arises.

Problem Statement:

Two devices (tablet or smart-phone), which cannot communicate directly due to distance will use a 3rd device in between them to communicate. Using the relay, one device should be able to stream music or video from another device.

Relay:

A relay is nothing but a connecter which will act as a middle person and helps in transmitting data between devices which cannot communicate due to distance. It is just like a courier guy who delivers your mail from source to destination.

As shown in the figure above, the problem statement is properly depicted. An android application which connects 3 smart phones acting as sender receiver and relay and the receiver can stream music from the sender’s device.

Abstract:

An android application “Music Share” which will enable 3 smart phones namely A, B and C to stream music from the sender to the receiver via the relay. The relay will first connect to the sender. Thereafter, a connection will be made with the receiver. Once the connection is established, the sender will send a music file to the relay.

The relay will convert the file into bytes, and a byte array is formed. This is stored in a temporary file in the relay and is sent to the receiver which is also connected to the relay. When sufficient amount of the data is sent, and the play button is pressed, the file is played which was originally stored in the sender. This is the overview of how the application works. It is explained in detail in the following sections.

Proposed Solution:

Developing an android application which will have the functionality of sender, receiver and relay. This application will have simple user interface which will guide the user to use the functionalities of sender, receiver and relay. Sender will have the functionality to connect to the relay via all the Bluetooth paired devices.

The relay will be acting as a bridge, which will connect, to the sender as well as the receiver. The receiver will be connected to the relay and when it gets connected to the relay it will connect to sender hence forming a chain. When this chain is established the receiver will get the list songs that are in the storage of the sender device and it will be displayed on the receiver device.

Now, when you select the name of the song that you want to play it will start fetching the song from the sender device. When you hit the play button it will start playing the streamed song on the device.

Architecture:

We have implemented a three-tier architecture which have a UI and that UI is connected to the backend logic which we used to make the application functional. We used a database which was connected to the backend logic.

In this application when we open our application we come across a screen where we have to select whether we want our device to be sender, relay or receiver. If we have entered in the database whether the given device is sender, relay or receiver then the device automatically adapts that role and the respective screen opens.

Now for the sender application we have created a class name “Sender.java”. This class has all the functionalities that are required by the sender class to connect to the relay and send the data from sender to relay. Initially when we open the sender UI, we can see a list view which is populated with the paired Bluetooth device present in the system.

If the Bluetooth device is not paired with each other then we have to pair them manually. Once they are paired they will appear in the list view. When you click on the list view one a connection socket is created and a socket request is send to the relay device using the Bluetooth MAC address.

Once the sender requests a connection the relay accepts the connection request opening Bluetooth socket connection request from the sender device. Now the whole functionality of the relay device is written in two classes ‘Relay.java’ and ‘ThreadToBeConnected.java’. In ‘Relay.java’ we are accepting the connection and opens up a connection with the sender device.

Once it is establish it also accepts a list of names of files of songs from the sender device. The above-mentioned things are done on one thread and the other thread tries establishing the connection with the receiver device.

The connection is established as it was established between sender and relay. Now when the receiver establishes the connection with the relay it will accept the list of files and send the request of back to the receiver.

Once the request is sent to the relay two new threads start. One thread will start fetching the data from the sender via file input output stream it will pass that data to the receiver. This all will be handled by the ‘TheadToBeConnected.java’ class.

Now when the receiver accepts the connection from the relay, it will get a list view from the sender it will populate its list view. Now, when you click on the name of the song it will send a request to relay. When it starts accepting the data from the relay, it will generate a temporary file. This temporary file will be then used to stream the data.

Challenges:

• We were new to android and hence we need to learn how to develop an android applications. Hence we needed to go through a lot of android tutorials and YouTube videos.

• How the Bluetooth can connect between two devices and how to transfer the data via a Bluetooth?
As a result of this we came to know that Bluetooth can be connected between two devices using a socket connection.

Once the connection is established we can send the data over to the next device using input output stream, until the connection stays open.

• How to transfer the file from one device to other device?

We came to know that the file transfer can be done using the byte array. So created a byte array and used to transfer that byte array while transferring the file. This solved the problem of copying the file from one device to another device.

• Streaming a file created problems?

When we used to write a byte array in a temporary file with a chunk of data it created a lot of noise and the playback was not smooth. So we used to send a chunk of data and while writing the data on the temporary file we used to write it byte by byte.

• Creating relay functionality?

The functionality of relay was not difficult to understand but was very difficult to implement. First we used to create a temporary file on the relay device but then the data was not transferred as we wanted it to be. So then we created two different threads and did synchronization on those to thread, which made the streaming smooth.

The chunk of data from sender was received by relay on one thread and the other thread used to take that same data and send that data to the receiver. In this way the relay functionality was implemented.

Drawbacks:

There are a few drawbacks about this application. They can be worked up on in the future making the application more dynamic.

They are listed as follows:

1. Socket connection for Bluetooth versions were not supported in different android versions. This means that this application will not work if one smart phone is lollipop version and another is kit Kat version. This can be included in the future work of the application.

2. The application uses a static Database and the values of the smart phones are inserted manually. This means that the MAC addresses of the smart phones and their names are added manually. So, we want to connect a new device, we have to manually remove one of the phone add the address and the name of the new phone. Therefore, we can’t dynamically add any device directly using the Bluetooth.

3. The file name and the owner name have to be mentioned in the code manually.

4. There should be only one song in the sender phonei.e. only 1 .mp3 file in the sender phone. If there is more than one .mp3 file in the sender file, the application crashes immediately.

Thus these are the drawbacks and can be included in the future work.

How to install the application:

There are two ways you can install the app:

1. You can open the code in eclipse and then ‘RUN AS’ on the devices you want. But before you do so you need to make the devices are in the developer mode.

2. You can also push the musicshare.apk to android phones which is present in the bin folder of the code sent to you before.

How to run the application:

For instance, if you use the second method, i.e. pushing an apk (music share) to all 3 android devices.
Make sure all 3 devices have same android version.

Then, open the apk in all 3 phones.

Select one phone as sender, one phone as relay and other as receiver.

In sender phone, select the Bluetooth MAC address of the relay phone which is to be connected.
Once the sender connection is established with relay, next establish the connection from relay to receiver.

After successful connection, the receiver will get the list of music files present in the sender and it requests for a music file.

After significant bytes are transmitted form sender to receiver through relay, the receiver can play the music file.

Conclusion:

With the help of Bluetooth sockets and all other techniques explained above, we successfully implemented our project “Streaming using Smartphones as relay.”

References:

• http://stackoverflow.com/questions/7273084/how-to-programatically-connect-2-android-devices-with-bluetooth
• http://developer.android.com/guide/topics/connectivity/bluetooth.html
• http://stackoverflow.com/questions/18109479/howt-to-connect-two-devices-via-bluetooth-sending-the-pairng-code-by-parameter
• http://www.glowingpigs.com/index.php/extras

2 Replies to “Streaming using Smartphone as relay via Bluetooth”

Leave a Reply

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