It’s been a while after part one but not because of me abandoning the subject. On the contrary: i must have been stepping on an off that darn scale like a million times trying to debug my way around the BLE mysteries.
Continuing the previous post we now should first install Pygatt. It installs through the pip installer which needs to be installed first.
sudo apt-get install python-pip
after which pygatt and all dependencies may be installed
sudo pip install 'pygatt[GATTTOOL]'
Apart from throwing all kinds of errors and warnings it ends with “Successfully installed…”. To be able to use Pygatt for my purpose i needed to fix another few things.
Fix 1: Support for receiving multibyte indications Receiving data from the scale is performed by subscribing to an indication. The indications contain 40 hex characters and the installed Pygatt (as of march 2016) does not support that. Ilya Sukhanov fixed that in function “_handle_notification_string” but his changes have not been merged (yet). But Ilya made some more improvements so just grab his gatttool.py entirely and replace the one found at /usr/local/lib/python2.7/dist-packages/pygatt/backends/gatttool
EDIT: As of 6/9/16 the above changes have been merged into Peplin’s master
Fix 2: Restarting bluetooth
At adapter.start() Pygatt will first try to restart the bluetooth service by “sudo systemctl restart bluetooth” however on debian Jessie this should be “sudo systemctl restart bluetooth.target“. This is changed manually at the bottom of gatttool.py by changing “subprocess.Popen([“sudo“, “systemctl“, “restart“, “bluetooth“]).wait()” into “subprocess.Popen([“sudo“, “systemctl“, “restart“, “bluetooth.target“]).wait()”
Fix 3: Enable le (low energy) mode
Using the gatttool in interactive mode, like (replace address with yours):
gatttool -i hci0 -b 20:C3:8F:E9:56:F1 -I
would respond with the “[20:C3:8F:E9:56:F1][LE]>” prompt. But the connect command would fail:
[20:C3:8F:E9:56:F1][LE]> connect Attempting to connect to 20:C3:8F:E9:56:F1 Error: connect: Connection refused (111)
In my case this could be solved by
sudo btmgmt le on
So for this to use from Python it is usefull to copy the “btmgmt” command to the /usr/local/bin directory too.
To be frank: Even after fiddling around with the Bluetooth utils it is still not quite clear to me what tool to choose from: hcitool, hciconfig, btmgmt, gatttool…
Anyway: With this installation you now should be able to run the python code i wrote which can be found at my Github pages.
Next post will zoom in the Python code and de BS440 connected scale specifics.