This tutorial shows how to use PyBluez, which is a Bluetooth library, in Python program that search all the nearby devices.
How to install bluez and PyBluez in ubuntu14.04
Bluez
# By default, ubuntu 14.04 use Bluez already… # If you don’t have one, you can paste the command in your terminal $ sudo apt-get install bluez $ sudo apt-get install bluez python-gobject python-dbus # Install the utilities of bluez $ sudo apt-get install bluez-utils # Development files for using the BlueZ Linux Bluetooth library # You can use it by #include $ sudo apt-get install libbluetooth-dev
PyBluez
$ pip install pybluez $ sudo apt-get install python-bluetooth # Another way to install is to download the latest version of pyBluez from https://code.google.com/p/pybluez/downloads/list and setup it. # For example: $ wget https://pybluez.googlecode.com/files/PyBluez-0.20.zip $ unzip PyBluez-0.20.zip $ cd PyBluez-0.20 # Setup your PyBluez $ sudo python setup.py install
Check the version of Bluetooth
$ dpkg --status bluez | grep '^Version:' Version: 4.101-0ubuntu13 $ bluetoothd -v 4.101 $ hciconfig -a hci0: Type: BR/EDR Bus: USB BD Address: C8:F7:33:E3:9B:ED ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING PSCAN ISCAN RX bytes:937 acl:0 sco:0 events:48 errors:0 TX bytes:1500 acl:0 sco:0 commands:47 errors:0 Features: 0xff 0xff 0x8f 0xfe 0xdb 0xff 0x5b 0x87 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: RSWITCH HOLD SNIFF PARK Link mode: SLAVE ACCEPT Name: 'ubuntu-0' Class: 0x640100 Service Classes: Rendering, Audio, Telephony Device Class: Computer, Uncategorized HCI Version: 4.0 (0x6) Revision: 0x1ebd LMP Version: 4.0 (0x6) Subversion: 0xfc00 Manufacturer: Intel Corp. (2)
Playing PyBluez
First, you need to enable bluetooth of your devices. Then, you can write a python program to communicate with them. I have a flame now, so I take it as an example.
$ vim bluetooth.py
Click here to view code on gist
import bluetooth print "Start discovering...." nearby_devices = bluetooth.discover_devices() print "Found devices:" for bdaddr in nearby_devices: print bluetooth.lookup_name( bdaddr ), "with address:", bdaddr
Now, you can enable bluetooth of your devices and use the program to discover them.
$ python bluetooth.py # output: Start discovering.... Found devices: flame with address: D8:E5:6D:1B:FF:6B
Actually, you can write your code more formally like this:
$ vim bluetooth_discover.py
import bluetooth def search(searchTime): devices = bluetooth.discover_devices(duration=searchTime, lookup_names = True) return devices if __name__=="__main__": print "Start discovering...." results = search(5); if(results!=None): for addr, name in results: print "{0} - {1}".format(addr, name) #endfor else: print "nothing found!" #endif
$ python bluetooth_discover.py # output: Start discovering.... D8:E5:6D:1B:FF:6B - flame
Find the bluetooth services on your devices
$ vim bluetooth_find_services.py
import bluetooth from pprint import pprint def search(searchTime): devices = bluetooth.discover_devices(duration=searchTime, lookup_names = True) return devices if __name__=="__main__": print "Start discovering...." results = search(10); if(results!=None): for addr, name in results: print "{0} - {1}".format(addr, name) service = bluetooth.find_service(address=addr) pprint(service) #endfor else: print "nothing found!" #endif
$ python bluetooth_find_services.py # output: Start discovering.... D8:E5:6D:1B:FF:6B - flame [{'description': None, 'host': 'D8:E5:6D:1B:FF:6B', 'name': None, 'port': None, 'profiles': [('1200', 258)], 'protocol': None, 'provider': None, 'service-classes': ['1200'], 'service-id': None}, {'description': None, 'host': 'D8:E5:6D:1B:FF:6B', 'name': 'Audio Source', 'port': 25, 'profiles': [('110D', 258)], 'protocol': 'L2CAP', 'provider': None, 'service-classes': ['110A'], 'service-id': None}, {'description': None, 'host': 'D8:E5:6D:1B:FF:6B', 'name': 'AVRCP TG', 'port': 23, 'profiles': [('110E', 259)], 'protocol': 'L2CAP', 'provider': None, 'service-classes': ['110C'], 'service-id': None}, {'description': None, 'host': 'D8:E5:6D:1B:FF:6B', 'name': 'Voice Gateway', 'port': 10, 'profiles': [('111E', 261)], 'protocol': 'RFCOMM', 'provider': None, 'service-classes': ['111F', '1203'], 'service-id': None}, {'description': None, 'host': 'D8:E5:6D:1B:FF:6B', 'name': 'Voice Gateway', 'port': 11, 'profiles': [('1108', 258)], 'protocol': 'RFCOMM', 'provider': None, 'service-classes': ['1112', '1203'], 'service-id': None}, {'description': None, 'host': 'D8:E5:6D:1B:FF:6B', 'name': 'OBEX Object Push', 'port': 12, 'profiles': [('1105', 258)], 'protocol': 'RFCOMM', 'provider': None, 'service-classes': ['1105'], 'service-id': None}]