Connecting the Medisana BS440 Bluetooth scale (part 2)

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.

 

Advertisements
This entry was posted in BLE and tagged , , , , , , , . Bookmark the permalink.

6 Responses to Connecting the Medisana BS440 Bluetooth scale (part 2)

  1. Ben Roe says:

    Hello,
    i trie to run you code from github. I solved some problems, but now i am stuck.
    Debian System with bluez installed.
    I run you script python BS440.py and get this error.

    Set Low Energy for hci0 failed with status 0x0b (Rejected)
    ^CTraceback (most recent call last):
    File "BS440.py", line 116, in
    wait_for_device(device_name)
    File "BS440.py", line 53, in wait_for_device
    found = adapter.filtered_scan(devname)
    File "/usr/local/lib/python2.7/dist-packages/pygatt/backends/backend.py", line 52, in filtered_scan

    The comand sudo btmgmt le on worked before i have run your script. After i have run it i get an error. Set Low Energy for hci0 failed with status 0x0b (Rejected)
    Any idea?

    Liked by 1 person

    • keptenkurk says:

      Try disabling hci0 before issuing the btmgmt commands. I recall having done a sequence like:
      $ sudo hciconfig hci0 down
      $ sudo ./btmgmt le on
      $ sudo ./btmgmt bredr off
      hci0 Set BR/EDR complete, settings: connectable bondable le
      $ sudo hciconfig hci0 up
      $ sudo hciconfig hci0 leadv

      But that’s from the commandline. If i understand your question well, that seems to work but the command fails when issued from the script in
      p = subprocess.Popen(“sudo btmgmt le on”, stdout=subprocess.PIPE, shell=True)

      The script only issues this once so you could try doing the init manually and skip it in the script as a workaround.

      Liked by 1 person

      • Ben Roe says:

        Thanks for the quick answer. Tried everything. Also with sudo hciconfig hci0 down it is not working.
        I will try it now on a Raspberry. Maybe Debian is the problem. I let you know if i found the issue.
        Btw. thanks for the pre work.

        Liked by 1 person

  2. gidi2go says:

    Hello,
    I tried to run your code on a Raspberry Pi 3 with the BS440 scales. Unfortunately, it does not seem to work, in particular, the scales (or other BLE devices) are not found by the wait_for_device function, whereas if I do the command “sudo hcitool lescan” in a command window, I can find the scales and other BLE devices.

    Any ideas?
    The log file says:
    “Thu, 27 Oct 2016 17:42:06 DEBUG start gatttool_cmd=gatttool -i hci0 -I
    Thu, 27 Oct 2016 17:42:06 INFO run Running…
    Thu, 27 Oct 2016 17:42:06 INFO scan Starting BLE scan
    Thu, 27 Oct 2016 17:42:06 ERROR scan Unexpected error when scanning”

    Any help will be appreciated!

    Like

    • keptenkurk says:

      Hi, great that you’re trying this! For reference, this is what i see on my Pi2:

      Thu, 27 Oct 2016 19:21:08 INFO BS440 Started
      Thu, 27 Oct 2016 19:21:08 INFO init_ble_mode hci0 Set Low Energy complete, settings: powered bondable ssp br/edr le

      Thu, 27 Oct 2016 19:21:08 DEBUG reset Executing sudo systemctl restart bluetooth.target
      Thu, 27 Oct 2016 19:21:09 DEBUG reset Executing sudo hciconfig hci0 reset
      Thu, 27 Oct 2016 19:21:09 DEBUG start gatttool_cmd=gatttool -i hci0 -I
      Thu, 27 Oct 2016 19:21:09 INFO scan Starting BLE scan
      Thu, 27 Oct 2016 19:21:09 INFO run Running...
      Thu, 27 Oct 2016 19:21:19 INFO scan Found 0 BLE devices
      Thu, 27 Oct 2016 19:21:20 INFO scan Starting BLE scan
      Thu, 27 Oct 2016 19:21:20 ERROR scan Unexpected error when scanning: Set scan parameters failed: Input/output error

      Thu, 27 Oct 2016 19:21:20 DEBUG reset Executing sudo systemctl restart bluetooth.target
      Thu, 27 Oct 2016 19:21:20 DEBUG reset Executing sudo hciconfig hci0 reset
      Thu, 27 Oct 2016 19:21:20 INFO scan Starting BLE scan
      Thu, 27 Oct 2016 19:21:30 INFO scan Found 0 BLE devices
      Thu, 27 Oct 2016 19:21:30 INFO scan Starting BLE scan
      Thu, 27 Oct 2016 19:21:31 ERROR scan Unexpected error when scanning: Set scan parameters failed: Input/output error

      with the last block repeating.
      That all comes from the “wait_for_device(devname)” call. This calls “adapter.filtered_scan(devname)” from pygatt which scans specifically for the address of the BLE device as configured in the ini file. In fact pygatt is nothing more than a python wrapper around the commandline utilities gatttool and hcitool. So if these works you should be good.
      Does your ini file contain the section


      [Scale]
      ble_address: aa:bb:cc:11:22:33
      device_name: 0202B6332211CCBBAA

      with the correct address and name of your scale?

      Like

  3. Gidi van liempd says:

    Okay, thank you for the quick response!
    I noticed that your log file also contains the “ERROR scan Unexpected error when scanning””, so apparently this is not so bad.
    I removed the INI file, but made sure the ble_address and device_name have the correct values. (A partial name should be sufficient as well, looking at the code in the gatttool.py)
    Still, it does not work, (Also with other BLE devices, such as my MIO Fuse watch)

    However,
    If I skip this wait_for_device entirely and directly try to connect with the scales, that works perfectly!
    So now I am looking on how to use the different steps in the script, without crashing the Raspberry Pi (I have noticed a lock-up after leaving the script on for some time).

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s