Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Activate switchbot by python with bluepy on Raspberry Pi
# Activate switchbot by python with bluepy on Raspberry Pi
# Switchbot's API is taken from this
import binascii
from bluepy.btle import Peripheral
# find your switchbot address by
# pi@raspberry:~ $ sudo hcitool lescan
# replace "ff:..:ff"
p = Peripheral("ff:ff:ff:ff:ff:ff", "random")
hand_service = p.getServiceByUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b")
hand = hand_service.getCharacteristics("cba20002-224d-11e6-9fb8-0002a5d5c51b")[0]
Copy link

rasstef commented Jan 18, 2021

it's not working for me, but why?

  • MagicSwitchbot
  • Raspberry 3 Model B -> has Bluetooth Low Energy (BLE) on board
  • Raspbian Buster -> bluez 5.50
# hciconfig hci0 down
# btmgmt le on
# hciconfig hci0 up
# hcitool lescan
  34:14:B5:4A:8E:19 MagicSwitchBot

#./  34:14:B5:4A:8E:19 on
Connecting... 34:14:B5:4A:8E:19
Traceback (most recent call last):
  File "./", line 20, in <module>
    p = Peripheral(mac, 'random')
  File "/usr/local/lib/python2.7/dist-packages/bluepy/", line 391, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python2.7/dist-packages/bluepy/", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral 34:14:B5:4A:8E:19, addr type: random

cannot connect to the device no matter what I do. I also tried gatttool, nothing works...
Have you got an idea? Is an older version of the bluetooth stack needed (stretch?)?

Copy link

vladeha commented Mar 7, 2021

same luck. does anyone have a solution to this problem?

Copy link

remko2000 commented Mar 15, 2021

I follow

Copy link

EngineeredEntropy commented Jul 20, 2021

Try entering your mac address with all lowercase letters, fixed this issue for me.

Copy link

scargill commented Apr 8, 2022

I can't even get that far on my RPI4 - I just entered the code as above but chaning my MAC number - no mistakes - yet...

pi@mainpi:~:11:46[0]> ./
./ line 7: import: command not found
from: too many arguments
./ line 13: syntax error near unexpected token `('
./ line 13: `p = Peripheral("fb:ff:1f:00:4d:aa", "random")'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment