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 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

Now, issue the following command to install the downloaded library.

sudo python install

If everything goes fine, we can start programming. Save the following program as 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  
4:  # we should know  
5:  target_name = "SHV-E210K"  
6:  file_to_send = "/home/asanka/Downloads/20130621_151742.jpg"  
8:  # we don't know yet  
9:  obex_port = None                 
10:  target_address = None  
12:  print "searching for nearby devices..."  
13:  nearby_devices = bluetooth.discover_devices()  
15:  for bdaddr in nearby_devices:  
16:    print bluetooth.lookup_name( bdaddr )  
17:    if target_name == bluetooth.lookup_name( bdaddr ):  
18:       print "found the target device!"  
19:      target_address = bdaddr  
20:      break  
22:  print "searching for the object push service..."  
23:  services = lightblue.findservices(target_address)  
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  
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.


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.!msg/pybluez/XrPgYLeDej4/2Uf8za3oM4cJ

I didn't move into try file receiving functionality, however I hope it is also working properly. So far, this is all I know about file transferring via bluetooth using python.


  1. hai...

    is there any chance to receive any file as .txt,.pdf,.jpg file any time or sequentially

    instead of receiving a particular file

  2. This comment has been removed by the author.

  3. This program shows me an error while sending file. CAN YOU PLEASE HELP ME OUT

    1. the same error,
      all service from window
      ('00:1A:7D:DA:71:14', 1, 'Service Discovery'),
      ('00:1A:7D:DA:71:14', 1, 'Device ID Service Record'),
      ('00:1A:7D:DA:71:14', 15, 'Personal Ad Hoc User Service'),
      ('00:1A:7D:DA:71:14', 23, None),
      ('00:1A:7D:DA:71:14', 25, 'Microsoft Windows Audio Source'),
      ('00:1A:7D:DA:71:14', 1, 'OBEX Object Push')]

  4. I am getting error after printing "sending a file" E: server denied the Put request. Please tell the solution for it.