Reputation: 41
I have a problem with bluetootctl command and awk.
If I run bluetoothctl scan on | awk '{print $0}'
it not print anythink. But only bluetoothctl scan on
works fine.
How can I take the scan on output line by line for parsing ?
Upvotes: 1
Views: 1096
Reputation: 7994
My understanding is that bluetoothctl
was not intended to be used in this way. It is expected that the D-Bus API is used for such scripting.
The API is documented at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc
The repository has an example script of how to do discovery: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/test-discovery
I have also included an alternative below that deletes the device details from BlueZ after printing. This is useful if you are trying to get Manufacturer or Service data from beacons as by default BlueZ does some filtering of duplicate device data.
from gi.repository import GLib
from pydbus import SystemBus
from pprint import pprint
SCAN_TIME = 15
DEVICE_INTERFACE = 'org.bluez.Device1'
remove_list = set()
def stop_scan():
adapter.StopDiscovery()
mainloop.quit()
def clean_device(rm_dev):
try:
adapter.RemoveDevice(rm_dev)
except GLib.Error as err:
pass
def on_iface_added(path, interfaces):
if DEVICE_INTERFACE in interfaces:
on_device_found(path, interfaces[DEVICE_INTERFACE])
def on_device_found(device_path, device_props):
address = device_props.get('Address')
manufacturer_data = device_props.get('ManufacturerData')
print(f'{address} found! Manufacturer Data: [{manufacturer_data}]')
clean_device(device_path)
bus = SystemBus()
adapter = bus.get('org.bluez', '/org/bluez/hci0')
mngr = bus.get('org.bluez', '/')
mngr.onInterfacesAdded = on_iface_added
mainloop = GLib.MainLoop()
GLib.timeout_add_seconds(SCAN_TIME, stop_scan)
adapter.SetDiscoveryFilter({'DuplicateData': GLib.Variant.new_boolean(True)})
adapter.StartDiscovery()
mainloop.run()
Upvotes: 2