Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List nearby/discoverable bluetooth devices, including already paired, in Python, on Linux

I'm trying to list all nearby/discoverable bluetooth devices, including those already paired, using Python on Linux.

I know how to list services for a device using its address, and can connect successfully:

services = bluetooth.find_service(address='...')

Reading the PyBluez docs I would expect any nearby device to show up if I don't specify any criteria:

"If no criteria are specified, then returns a list of all nearby services detected."

The "only" thing I need right now is to be able to list already paired devices, whether they are on, off, nearby or not. Much like the list I'm getting in All Settings --> Bluetooth in Ubuntu/Unity.

Btw, the following does not list already paired devices on my machine, even if they are on/nearby. Probably because they are not discoverable once paired:

import bluetooth
for d in bluetooth.discover_devices(flush_cache=True):
    print d

Any ideas ...?

Edit: I found and installed "bluez-tools".

bt-device --list

... gives me the information I need, i.e. addresses of added devices.

I've checked the C source, found out that this might not be as easy as I thought it would be.

Still don't know how to do this in Python ...

Edit: I think DBUS might be what I should be reading up on. Seems complicated enough. If anyone has got some code to share I would be really happy. :)

like image 771
Micke Avatar asked Jan 10 '13 16:01

Micke


People also ask

How do I see previously connected devices on Bluetooth?

Swipe down from the top of the screen. Touch and hold Bluetooth . If your accessory is listed under "Available media devices," next to your device's name, tap Settings . If no accessories are listed under "Previously connected devices," tap See all.

How do I find Bluetooth devices on Linux?

Checking Bluetooth Status You can check it with the help of the systemctl command. If the Bluetooth service status is not active you will have to enable it first. Then start the service so it launches automatically whenever you boot your computer.

How do I find Bluetooth devices nearby?

To find an active Bluetooth device, first make sure you have Bluetooth enabled on your smartphone. Next, download Wunderfind for your iPhone or Android device and launch the app. Immediately, you'll see a list of Bluetooth devices that your smartphone has detected using its built-in Bluetooth radio.


2 Answers

I managed to solve the problem myself. The following snippet lists addresses for all paired devices, on my default bluetooth adapter:

import dbus

bus = dbus.SystemBus()

manager = dbus.Interface(bus.get_object('org.bluez', '/'), 'org.bluez.Manager')

adapterPath = manager.DefaultAdapter()

adapter = dbus.Interface(bus.get_object('org.bluez', adapterPath), 'org.bluez.Adapter')

for devicePath in adapter.ListDevices():
    device = dbus.Interface(bus.get_object('org.bluez', devicePath),'org.bluez.Device')
    deviceProperties = device.GetProperties()
    print deviceProperties["Address"]
like image 129
Micke Avatar answered Oct 05 '22 02:10

Micke


Since the adoption of the version 5 of the Bluetooth API most of the functions used in the @Micke solutions were dropped and the interaction with the bus take place throught the ObjectManager.GetManagedObjects [1]

import dbus


def proxyobj(bus, path, interface):
    """ commodity to apply an interface to a proxy object """
    obj = bus.get_object('org.bluez', path)
    return dbus.Interface(obj, interface)


def filter_by_interface(objects, interface_name):
    """ filters the objects based on their support
        for the specified interface """
    result = []
    for path in objects.keys():
        interfaces = objects[path]
        for interface in interfaces.keys():
            if interface == interface_name:
                result.append(path)
    return result


bus = dbus.SystemBus()

# we need a dbus object manager
manager = proxyobj(bus, "/", "org.freedesktop.DBus.ObjectManager")
objects = manager.GetManagedObjects()

# once we get the objects we have to pick the bluetooth devices.
# They support the org.bluez.Device1 interface
devices = filter_by_interface(objects, "org.bluez.Device1")

# now we are ready to get the informations we need
bt_devices = []
for device in devices:
    obj = proxyobj(bus, device, 'org.freedesktop.DBus.Properties')
    bt_devices.append({
        "name": str(obj.Get("org.bluez.Device1", "Name")),
        "addr": str(obj.Get("org.bluez.Device1", "Address"))
    })  

In the bt_device list there are dictionaries with the desired data: ie

for example

[{
    'name': 'BBC micro:bit [zigiz]', 
    'addr': 'E0:7C:62:5A:B1:8C'
 }, {
    'name': 'BBC micro:bit [putup]',
    'addr': 'FC:CC:69:48:5B:32'
}]

Reference: [1] http://www.bluez.org/bluez-5-api-introduction-and-porting-guide/

like image 42
Eineki Avatar answered Oct 05 '22 03:10

Eineki