OpenWonderLabs / python-host

The python code running on Raspberry Pi or other Linux based boards to control SwitchBot.
Apache License 2.0
376 stars 84 forks source link

Invalid handle on all commands #43

Open dcarrion87 opened 1 year ago

dcarrion87 commented 1 year ago

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 commented 1 year ago

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
BroZer8360 commented 1 year ago

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

dcarrion87 commented 1 year ago

@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 commented 1 year ago

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.

allegewhd commented 1 year ago

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
peterneutron commented 1 year ago

@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

zedrdave commented 1 year ago

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

mesigned commented 9 months ago

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