I cannot list up the BLE devices in the neighbourhood of my Raspberry Pi (python, btmgmt)

谁说我不能喝 提交于 2021-01-27 18:52:27

问题


I want to scan the ble devices in the environment of my Raspberry, by using a python script called from a cron script. But when I do this in cron (I mean I added to sudo crontab -e), I allways end up with an empty list.

when I am logged in as pi user - btmgmt works (only) fine with su permissions:

pi@Pluto:~ $ btmgmt find
Unable to start discovery. status 0x14 (Permission Denied)

pi@Pluto:~ $ sudo btmgmt find
Discovery started
hci0 type 7 discovering on
hci0 dev_found: 77:F8:D7:8A:1E:E5 type LE Random rssi -83 flags 0x0000 
...

so in my python script I wrote:

flog.write("P01:\r\n")
out = subprocess.Popen(['sudo', '/usr/bin/btmgmt', 'find'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = out.communicate()
flog.write("stderr: " + str(stderr) + "\r\n")
cDvc = stdout.split('\n')
flog.write("Amount of lines = " + str(len(cDvc)) + "\r\n")
for line in cDvc:
    line = line + '\r\n'
    if debugflag:
        print(line)
        flog.write(line)
..

Running this script from the shell prompt works fine.. in the log-file (flog) I get: ...

P01:
stderr: None
Amount of lines = 40
Discovery started
hci0 type 7 discovering on
hci0 dev_found: 70:D0:FD:74:34:AC type LE Random rssi -59 flags 0x0000 
AD flags 0x1a 
..

Running this same script as a crontab -e line: no devices show up & I cannot find cause:

...
P01:
stderr: None
Amount of lines = 1
P02:
...

Can anyone help me out here?


回答1:


If you use the BlueZ DBus API to get the information then you will not need to use sudo. It also avoids you having to use btmgmt as I am not sure it was intended for it to be scripted in that way

The documentation for the DBus API is available at:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt

The pydbus library is very helpful for accessing the BlueZ DBus API: https://pypi.org/project/pydbus/

Some useful things to know to get you started:

  1. The Dbus service for bluez is called 'org.bluez'
  2. The default Bluetooth adapter normally has '/org/bluez/hci0' as its DBus object path.
  3. BlueZ/DBus has an object manager which stores information about devices

I did the following script to test the idea:

from datetime import datetime
import os
import pydbus
from gi.repository import GLib

discovery_time = 60
log_file = '/home/pi/device.log'

# Create an empty log file
def write_to_log(address, rssi):
    if os.path.exists(log_file):
        open_mode = 'a'
    else:
        open_mode = 'w'

    with open(log_file, open_mode) as dev_log:
        now = datetime.now()
        current_time = now.strftime('%H:%M:%S')
        dev_log.write(f'Device seen[{current_time}]: {address} @ {rssi} dBm\n')

bus = pydbus.SystemBus()
mainloop = GLib.MainLoop()

class DeviceMonitor:
    def __init__(self, path_obj):
        self.device = bus.get('org.bluez', path_obj)
        self.device.onPropertiesChanged = self.prop_changed
        print(f'Device added to monitor {self.device.Address}')

    def prop_changed(self, iface, props_changed, props_removed):
        rssi = props_changed.get('RSSI', None)
        if rssi is not None:
            print(f'\tDevice Seen: {self.device.Address} @ {rssi} dBm')
            write_to_log(self.device.Address, rssi)


def end_discovery():
    """Handler for end of discovery"""
    mainloop.quit()
    adapter.StopDiscovery()

def new_iface(path, iface_props):
    """If a new dbus interfaces is a device, add it to be  monitored"""
    device_addr = iface_props.get('org.bluez.Device1', {}).get('Address')
    if device_addr:
        DeviceMonitor(path)

# BlueZ object manager
mngr = bus.get('org.bluez', '/')
mngr.onInterfacesAdded = new_iface

# Connect to the DBus api for the Bluetooth adapter
adapter = bus.get('org.bluez', '/org/bluez/hci0')
adapter.DuplicateData = False

# Iterate around already known devices and add to monitor
mng_objs = mngr.GetManagedObjects()
for path in mng_objs:
    device = mng_objs[path].get('org.bluez.Device1', {}).get('Address', [])
    if device:
        DeviceMonitor(path)

# Run discovery for discovery_time
adapter.StartDiscovery()
GLib.timeout_add_seconds(discovery_time, end_discovery)
print('Finding nearby devices...')
try:
    mainloop.run()
except KeyboardInterrupt:
    end_discovery()


来源:https://stackoverflow.com/questions/60915339/i-cannot-list-up-the-ble-devices-in-the-neighbourhood-of-my-raspberry-pi-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!