python-host icon indicating copy to clipboard operation
python-host copied to clipboard

Invalid handle on all commands

Open dcarrion87 opened this issue 2 years ago • 8 comments

Running on a Raspberry Pi 4 using the inbuilt BT adapter

I've attempted to use the switchbot_py3.py as well as doing this myself and get the following when sending the handles which it should be sending:

gattlib.GATTException: Characteristic value/descriptor operation failed: Invalid handle

E.g.

python3 switchbot_py3.py -d XX:XX:XX:XX:XX:XX -c on
Connected!
Traceback (most recent call last):
  File "/root/python-host/switchbot_py3.py", line 180, in <module>
    main()
  File "/root/python-host/switchbot_py3.py", line 175, in main
    driver.run_command(opts.command)
  File "/root/python-host/switchbot_py3.py", line 124, in run_command
    return req.write_by_handle(self.handles[command], self.commands[command])
gattlib.GATTException: Characteristic value/descriptor operation failed: Invalid handle

Is there a change to the commands/handles with Switchbot Bot version 5?

dcarrion87 avatar Jan 20 '23 12:01 dcarrion87

Not sure if this helps at all with troubleshooting:

# gatttool -t random -b XX:XX:XX:XX:XX:XX --characteristics
handle = 0x0002, char properties = 0x20, char value handle = 0x0003, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x0006, char properties = 0x02, char value handle = 0x0007, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0008, char properties = 0x02, char value handle = 0x0009, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x000a, char properties = 0x02, char value handle = 0x000b, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000c, char properties = 0x02, char value handle = 0x000d, uuid = 00002aa6-0000-1000-8000-00805f9b34fb
handle = 0x000f, char properties = 0x0c, char value handle = 0x0010, uuid = cba20002-224d-11e6-9fb8-0002a5d5c51b
handle = 0x0011, char properties = 0x10, char value handle = 0x0012, uuid = cba20003-224d-11e6-9fb8-0002a5d5c51b
# gatttool -t random -b XX:XX:XX:XX:XX:XX --char-write-req -a 0x0016 -n 570101
Characteristic Write Request failed: Invalid handle

# gatttool -t random -b XX:XX:XX:XX:XX:XX --char-write-req -a 0x0016 -n 570102
Characteristic Write Request failed: Invalid handle

dcarrion87 avatar Jan 20 '23 12:01 dcarrion87

Let's write in here @dcarrion87 Did you tried sudo python2 switchbot.py <mac_adress> Bot Turn On And what's the error message ?

Edit : <Mac_adress> is to be replaced by the mac adresse of your device, it's shown in the mobile app and is like this format : XX:XX:XX:XX:XX:XX with X from 0 to 9 and from A to F

BroZer8360 avatar Jan 20 '23 15:01 BroZer8360

@BroZer8360 I don't have python2 on this system no error message for the switchbot_py2topy3.py converted one but doubt it would show an error as it's just firing the command and not listening for a response. Nothing happening on the bot.

# sudo switchbot_py2topy3.py XX:XX:XX:XX:XX:XX Bot Turn On
Preparing to connect.
Connection successful.
Complete

Not showing mac address for identification reasons.

dcarrion87 avatar Jan 20 '23 20:01 dcarrion87

I was able to get this working with this project https://github.com/Danielhiversen/pySwitchbot

E.g.:

from switchbot import Switchbot
from bleak import BleakScanner
import asyncio

async def main():
  ble_device = await BleakScanner.find_device_by_address("MAC ADDRESS", timeout=20)
  device = Switchbot(ble_device)
  await device.turn_off()

if __name__ == "__main__":
  asyncio.run(main())

Looks like there's differences in how it handles.

dcarrion87 avatar Jan 20 '23 21:01 dcarrion87

same problem with fireware 6.3. OS is ubuntu 20.04

> gatttool -t random -b switch_bot_with_fireware_6.3_mac  --char-write-req -a 0x0016 -n 570101
Characteristic Write Request failed: Invalid handle

 > gatttool -t random -b switch_bot_with_fireware_4.9_mac  --char-write-req -a 0x0016 -n 570101
Characteristic value was written successfully

allegewhd avatar Mar 29 '23 04:03 allegewhd

@dcarrion87 This is all very strange and almost looks like either the BLE API is abandoned in favour of the Hub or the documentation is totally out of date regarding newer Bots. As it seems newer Bots can't be controlled anymore using just BLE and instead require the App or a Hub. See my issue here https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/issues/25#issue-1717859940

peterneutron avatar May 23 '23 18:05 peterneutron

Based on replies on that other thread, does this seem to address the problem above?

zedrdave avatar Jul 03 '23 15:07 zedrdave

I ran into the same issue using the switchbot_py3.py file. Interestingly using the switchbot_py2topy3.py file worked for me. What I noticed was that the command used to e.g. turn on the switchbot looked like this:

> gatttool -t random -b XX:XX:XX:XX:XX:XX --char-write-req -a 0x0013 -n 570101
Characteristic value was written successfully

I spent quite some hours searching for a package that works, hopefully this information is of use to someone. Switchbot running on firmware version 6.5

mesigned avatar Dec 17 '23 20:12 mesigned