## Friday, July 19, 2013

### Algorithms in IEEE latex paper manuscripts

While preparing a manuscript on a IEEE latex paper template, I wanted to add a pseudo code of an algorithm to it. I write it down here to avoid forgetting it and also for the benefit of somebody somewhere in this world.

First of all I should have installed the full package of latex on my Ubuntu 12.04 machine to avoid the problems of different missing packages. I do it by the following command.

sudo apt-get install texlive-full

For preparing the manuscript I used the bare_conf.tex file coming with the latex templates which can be downloaded from IEEE website here. So, following commands should be added to that file in the appropriate places to make an example algorithm.

1:  \usepackage{algpseudocode}
2:
3:  \begin{figure}
4:  \begin{algorithmic}[1]
5:  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
6:  \State $r\gets a\bmod b$
7:  \While{$r\not=0$}\Comment{We have the answer if r is 0}
8:  \State $a\gets b$
9:  \State $b\gets r$
10:  \State $r\gets a\bmod b$
11:  \EndWhile\label{euclidendwhile}
12:  \State \textbf{return} $b$\Comment{The gcd is b}
13:  \EndProcedure
14:  \end{algorithmic}
15:  \caption{Euclid's algorithm}
16:  \label{euclid}
17:  \end{figure}

After this if we generate the PDF file, our algorithm should appear in the paper like the following.

So, have a nice time with preparing research papers!

## Monday, July 8, 2013

### Transferring files via bluetooth using python scripts

In a previous post, I wrote about bluetooth programming using python. A library called BlueZ is used for that. We can do various things including client-server programs using that library. However, today I had a requirement to send or receive a file from a python script via bluetooth. As usual I started to search through the web until I find some way. Finally I came through a solution. A python library called lightblue can be used for this task. So, I'm writing down the basics I learned today about file transferring via bluetooth.

First of all, we need to download the lightblue library from the website http://lightblue.sourceforge.net/. It's scary to see the notice "This project is no longer maintained" in the website but thankfully it worked for me. I'm running Ubuntu 12.04 in my machine. Download the file "lightblue-0.4.tar.gz" and uncompress it. Now move into it from the terminal. We need to install some packages before installing the library. Issue the following commands for that purpose.

sudo apt-get install libopenobex1-dev
sudo apt-get install bluez
sudo apt-get install python-bluez libbluetooth-dev python-dev

sudo python setup.py install

If everything goes fine, we can start programming. Save the following program as lightblue_test.py in your machine. The variable "target_name" should contain the name of the bluetooth device we are going to connect to. "file_to_send" variable should contain the path to the file which we are going to send.

1:  import bluetooth
2:  import lightblue
3:
4:  # we should know
5:  target_name = "SHV-E210K"
7:
8:  # we don't know yet
9:  obex_port = None
11:
12:  print "searching for nearby devices..."
13:  nearby_devices = bluetooth.discover_devices()
14:
17:    if target_name == bluetooth.lookup_name( bdaddr ):
18:       print "found the target device!"
20:      break
21:
22:  print "searching for the object push service..."
24:  for service in services:
25:       if service[2] == "OBEX Object Push":
26:            obex_port = service[1]
27:            print "OK, service '", service[2], "' is in port", service[1], "!"
28:            break
29:
30:  print "sending a file..."
31:  try:
32:       lightblue.obex.sendfile( target_address, service[1], file_to_send )
33:       print "completed!\n"
34:  except:
35:       print "an error occurred while sending file\n"


Before running this program, we need to pair the two devices. For example since I'm going to send a file from my linux PC to an Android smart-phone, I paired the PC and Android phone first. Then issue the following command to run the script.

python lightblue_test.py

Some of the basic examples are available in the lightblue library website. I referred some other websites which are listed below to solve some issues I faced during this work.