Phone Book Access (PBAP) client for Linux (Raspberry Pi) - linux

i want to access the contacts of my smartphone over bluetooth. This should be made as first step of a project of my with a raspberry pi. I read something about the phone bok access protocol (PBAP).
But all i could was not very helpful for me. The most examples i found where to old.
I already managed to pair and connect my smart phone (OnePlus One). I also can see that is support PBAP. I use bluez in version 5.23-2+rpi1.
I found two examples of an PBAP client, one from obexd (http://code.metager.de/source/xref/linux/bluetooth/obexd/test/pbap-client) and one from bluez (http://code.metager.de/source/xref/linux/bluetooth/bluez/test/pbap-client).
If i execute the obexd example on the pi i get the following error:
Creating Session
Traceback (most recent call last):
File "pbap-client-obexd.py", line 110, in <module>
session_path = client.CreateSession(sys.argv[1], { "Target": "PBAP" })
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.bluez.obex.Error.Failed: No adapter found
If i execute the bluez example i get the following error:
Creating Session
Traceback (most recent call last):
File "pbap-client-bluez.py", line 128, in <module>
session_path = client.CreateSession(sys.argv[1], { "Target": "PBAP" })
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownObject: Method "CreateSession" with signature "sa{ss}" on interface "org.bluez.obex.Client1" doesn't exist
Does anyone of you have an idea to fix it, an actual tutorial or an other example?
Thank you

After searching a few hours later, i found a python library called PyOBEX. It contains some examples, e.g. a PBAP-Client, a example to download files over bluetooth and much more. It also contains all methods for pairing and connecting. Works on first try for me.
Link:
https://bitbucket.org/dboddie/pyobex/overview

PyOBEX is the right choice. you can also have a look into pypbap which has both pbap client and server built on top of PyOBEX. And has nice REPL to playaround with all pbap functionalities.

Related

Method "StopDiscovery" with signature "" on interface "org.bluez.Adapter1" doesn't exist

Running a script here that connects to 2 Bluetooth adapters and collects data to send via mqtt. Its been running relatively smoothly on my RPi Zero 2 W with LM1010 BT USB adapter (internal wifi and BT adapter for RPi are disabled) for about 2 years now. However, yesterday, I ran just your normal system updates 'sudo apt-get update then upgrade', now when I try to run the script, I get these errors.
I don't know even where to start with this one, and if you can provide some suggestions, that would be great.
INFO: Adapter status - Powered: True
INFO: Starting discovery...
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.9/site-packages/gatt/gatt_linux.py", line 156, in stop_discovery
self._adapter.StopDiscovery()
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 141, in __call__
return self._connection.call_blocking(self._named_service,
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 652, in call_blocking
reply_message = self.send_message_with_reply_and_block(
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownObject: Method "StopDiscovery" with signature "" on interface "org.bluez.Adapter1" doesn't exist
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/solar-monitor/solar-monitor.py", line 84, in <module>
device_manager.stop_discovery()
File "/home/pi/.local/lib/python3.9/site-packages/gatt/gatt_linux.py", line 161, in stop_discovery
raise _error_from_dbus_error(e)
gatt.errors.Failed: Method "StopDiscovery" with signature "" on interface "org.bluez.Adapter1" doesn't exist
ran script, but seems to crash at BLE scan part now after system updates

Failed to create a device file using `uinput` module. Python3 on Ubuntu for Windows

I have a python script that runs on python3.4 and uses the package keyboard to allow for keybinds;
keyboard.add_hotkey("enter", self.listener.stop, suppress=True)
keyboard.add_hotkey("shift+enter", self.listener.finish, suppress=True)
When I run this on Windows, it works perfectly listening to both hotkeys, also when run on linux (CentOS) it works.
At work I've gotten a Ubuntu environment on my windows via the windows 10 feature and app store. However this environment has a problem with this keyboard hotkey.
/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixkeyboard.py:110: UserWarning: Failed to create a device file using `uinput` module. Sending of events may be limited or unavailable depending on plugged-in devices.
device = aggregate_devices('kbd')
Traceback (most recent call last):
File "main.py", line 32, in <module>
], 'test')
File "/mnt/.../can_controller.py", line 28, in __init__
self.__initialise_key_handler()
File "/mnt/.../can_controller.py", line 95, in __initialise_key_handler
keyboard.add_hotkey("enter", self.listener.stop, suppress=True)
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/__init__.py", line 637, in add_hotkey
_listener.start_if_necessary()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/__init__.py", line 194, in init
_os_keyboard.init()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixkeyboard.py", line 110, in build_device
device = aggregate_devices('kbd')
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixcommon.py", line 168, in aggregate_devices
assert fake_device
AssertionError
If anybody knows how to fix this or has a good work-around. Please let me know.

pexpect telnet on windows

I've used pexpect on linux successfully to telnet/ssh into Cisco switches for CLI scrapping. I'm trying to convert this code over to Windows and having some issues since it doesn't support the pxpect.spawn() command.
I read some online documentation and it suggested to use the pexpect.popen_spawn.PopenSpawn command. Can someone please point to me what I'm doing wrong here? I removed all my exception handling to simplify the code. Thanks.
import pexpect
from pexpect.popen_spawn import PopenSpawn
child = pexpect.popen_spawn.PopenSpawn('C:/Windows/System32/telnet 192.168.1.1')
child.expect('Username:')
child.sendline('cisco')
child.expect('Password:')
child.sendline('cisco')
child.expect('>')
child.close()
Error:
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 98, in expect_loop
incoming = spawn.read_nonblocking(spawn.maxread, timeout)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\popen_spawn.py", line 68, in read_nonblocking
raise EOF('End Of File (EOF).')
pexpect.exceptions.EOF: End Of File (EOF).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python\windows scripts\telnet\telnet.py", line 37, in <module>
main()
File "C:\Python\windows scripts\telnet\telnet.py", line 20, in main
child.expect('Username:')
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 327, in expect
timeout, searchwindowsize, async_)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 355, in expect_list
return exp.expect_loop(timeout)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 104, in expect_loop
return self.eof(e)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 50, in eof
raise EOF(msg)
pexpect.exceptions.EOF: End Of File (EOF).
<pexpect.popen_spawn.PopenSpawn object at 0x000000000328C550>
searcher: searcher_re:
0: re.compile("b'Username:'")
The solution is to use plink.exe, which is a part of putty installation. You can download it from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
Enable TelnetClient on windows system and put plink.exe in the same folder from where you are running pexpect. Then you can telnet using pexpect as mentioned in the below example.
Also, use timeout flag with PopenSpawn to wait for the connection to establish. The above error is due to a timeout flag is not set.
p = pexpect.popen_spawn.PopenSpawn('plink.exe -telnet 192.168.0.1 -P 23', timeout=1)

BlueZ 5 - Connecting to a Bluetooth LE heart rate watch

I'm pretty much new to using BlueZ 5 and I've been experimenting with a Bluetooth LE heart rate watch. I would really appreciate if someone can point me to the right direction.
When I run the "test-heartrate" test, I see the following error:
Traceback (most recent call last):
File "./test-heartrate", line 88, in <module>
properties = dev_prop.GetAll(HEARTRATE_INTERFACE)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line
651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs:
No such interface 'org.bluez.HeartRate1'
Not sure what I'm doing wrong. Here're some relevant information
BlueZ version installed: 5.24
For installation, I followed the steps listed in: http://www.linuxfromscratch.org/blfs/view/svn/general/bluez.html (with one addition: I added --enable-experimental during configuration)
It seems I'm facing the same issue as described at:
http://comments.gmane.org/gmane.linux.bluez.kernel/47931
I've followed the suggestion provided as well as tried passing -E to
bluetoothd while running it but to no avail.
I can successfully run:
test-discovery : To discover devices, including the heart-rate watch
test-device connect : To connect to this device
But the one that is failing for me is test-heartrate
Even after connecting to the device, when I try to introspect:
dbus-send --system --dest=org.bluez --print-reply [obj_path] org.freedesktop.DBus.Introspectable.Introspect, I don't see the
org.bluez.HeartRate1 interface showing up, with its appropriate methods.
I can post the entire introspection data, along with the device properties that I get, if that helps.
Would really appreciate any helpful pointer in this regard. Thanks!!

urllib.urlopen() gives socket error: Name or service not known on python2.7

I am just starting to use and learn Python, so this may seem vary naive to ask.
On my Linux system if i try to get a webpage using urllib.urlopen() I get an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 207, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 344, in open_http
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 776, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 757, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known
>>>
If I try to do the same in the Python 2.7 installed in my Windows 7 system, it works fine.
Since i am a novice, its difficult for me to diagnose the problem. I tried to research it but still haven't got any answers.
So my questions are:
What is different in the windows system that urlopen() works there but not on Linux.
What needs to be done to ensure that urlopen() works on the Linux system. Its necessary for me that it works since the the program I am developing has some bash command calls and the program depends extensively on the proper working of urllib.

Resources