Connecting to a Bluetooth smart/LE weight scale with bluez/bluetoothctl/gatttool

大城市里の小女人 提交于 2019-11-29 05:10:06

The scale appears to be connectable just for a short period of time after the weight etc. has been measured I used the non-interactive mode of gatttool in a bash script like this:

gatttool -t random -b F1:37:57:XX:XX:XX --char-write --handle 0x001f -n 0200
gatttool -t random -b F1:37:57:XX:XX:XX --char-read --handle 0x0021
gatttool -t random -b F1:37:57:XX:XX:XX --char-write --handle 0x001c -n 0200
gatttool -t random -b F1:37:57:XX:XX:XX --char-write --handle 0x0026 -n 0200
gatttool -t random -b F1:37:57:XX:XX:XX --char-write-req --handle 0x0023 -n 0000000000 --listen

Info was taken from Pratik Sinha here. To get the response displayed the --listen explicitly needed to be given after which some 25 lines of data are received (alternating 1b and 1e responses). Thanks for your info, saved me days of work!

Happy New Year!

EDIT:

Using Python and the pygatt module this boils down to:

import pygatt.backends
from binascii import hexlify

def printIndication(handle, value):
    print('Indication received {} : {}'.format(hex(handle), hexlify(str(value))))

adapter = pygatt.backends.GATTToolBackend()
adapter.start()
# wait for someone to step on the scale
while True:  
    try:
        device = adapter.connect('f1:37:57:xx:xx:xx', 5, 'random')
        break
    except pygatt.exceptions.NotConnectedError:
        print('Waiting...')
device.subscribe('00008a22-0000-1000-8000-00805f9b34fb', callback = printIndication, indication = True)
device.subscribe('00008a21-0000-1000-8000-00805f9b34fb', callback = printIndication, indication = True)
device.subscribe('00008a82-0000-1000-8000-00805f9b34fb', callback = printIndication, indication = True)
try:
    device.char_write_handle(0x23, [02,00,00,00,00], True)
except pygatt.exceptions.NotificationTimeout:
    pass
device.disconnect()
adapter.stop()

NOTES:

  • Reading handle 0x21 serves no purpose in my experience
  • Writing handle 0x23 with 02 followed by the unix timestamp syncs the RTC of the scale
  • Use the multibyte modified pygatt (pull 34) to be able to receive multiple bytes in an Indication
  • Although writing to 0x23 will not produce any Notification it is required to wait for a Notification to be able to receive the 0x1b and 0x1e Indications. When the last Indication was received the NotificationTimeout exception is received.
Edmundo Del Gusto

Okay.. I solved this by my self.. all I needed to do was change ,,char-write-cmd" to ,,char-write-req".. In the log-file by my Android-Hci-Snoop it was always a write requst. Don't know why I didn't recognize it all the time...

If someone has the same problem with bluez/bluetoothd/bluetoothctl & dbus like me (to use the commands "list-attributes", "select" "write" in bluetoothctl you have to execute bluetoothd in Experimental-Mode like "bluetoothd -n -E", but everytime I did this I got some error-messages like "D-Bus: name already in use" or something like "d'bus setup failed: connection is not allowed to own the service due to security..." (don't remember the exact errormessage) )

What I did:

  • installing bluez with ./configure [...] --enable-experimental (read ReadMe-file)

  • sudo nano /etc/dbus-1/system.d/bluetooth.conf

    --> copied the whole block that starts with >policy user="root">, and paste it below this block. Then I changed "root" to "pi" in one of the two blocks

If someone want to know how this scale provides the measurement results check out this link: A little Game/Quiz: Do you see my values? (Interpreting Hex-Values)

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