Skip to content

Instantly share code, notes, and snippets.

@prefiks
Created September 3, 2019 21:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save prefiks/e614116fc3983a8e7e5fe326800dc101 to your computer and use it in GitHub Desktop.
Save prefiks/e614116fc3983a8e7e5fe326800dc101 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from bluepy import btle
import sys
class DiscoLH(btle.DefaultDelegate):
def __init__(self):
self.devices = []
btle.DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if not isNewDev:
return
isLH = False
name = ""
if dev.getValue(btle.ScanEntry.MANUFACTURER)[0:4] == b'\x5d\x05\x00\x02':
print('Found LightHouse %s: address = %s' %
(dev.getValue(btle.ScanEntry.COMPLETE_LOCAL_NAME), dev.addr))
self.devices.append(dev.addr)
if __name__ == '__main__':
scanner = btle.Scanner()
delegate = DiscoLH()
scanner.withDelegate(delegate)
scanner.scan(2)
for device in delegate.devices:
lh = btle.Peripheral()
print("Connecting to %s" % (device))
lh.connect(device, addrType = btle.ADDR_TYPE_RANDOM)
if len(sys.argv) > 1 and sys.argv[1] == 'on':
lh.writeCharacteristic(0x12, b'\x01')
else:
lh.writeCharacteristic(0x12, b'\x00')
lh.disconnect()
@michaelnew
Copy link

I kept getting crashes when running this script until making this change:

v = dev.getValue(btle.ScanEntry.MANUFACTURER)
if v is not None and v[0:4] == b'\x5d\x05\x00\x02':

I'm assuming there is some other bluetooth device that scan is finding that doesn't have a manufacturer ID.

@John-Gee
Copy link

John-Gee commented Aug 9, 2020

Hello,

just in case others find themselves with the same issue I had: my setup was working fine for a while and somehow it stopped working with any such script. Turns out the default bluetooth device was not my standard one anymore but the Vive's, and that one won't connect to the bases for whatever reason.

So you would have to replace

lh = btle.Peripheral()

by

lh = btle.Peripheral(iface=)

where is the iface number of the device you want to use, you can see that in hciconfig. You want just the number, so if your device is hci0 you'd type 0, and so on.

Regards!

@waylonflinn
Copy link

waylonflinn commented Aug 29, 2020

Replace the final if block with this code to toggle the lighthouse state, when no argument is specified.
That is, this change will turn the lighthouse off when it's already on, and on when it's already off.

        if len(sys.argv) > 1 and sys.argv[1] == 'on':
            lh.writeCharacteristic(0x12, b'\x01')
        elif len(sys.argv) > 1 and sys.argv[1] == 'off':
            lh.writeCharacteristic(0x12, b'\x00')
        else:
            # toggle
            current = lh.readCharacteristic(0x12)
            if(current) == b'\x00':
                # turn on
                lh.writeCharacteristic(0x12, b'\x01')
            else:
                # turn off
                lh.writeCharacteristic(0x12, b'\x00')

In Ubuntu, I also put the following in a file named lighthouse.desktop on my desktop and click it when I want to turn them on or off. Make sure to edit the line Exec=/path/to/lh.py with the actual path.

[Desktop Entry]
Version=1.0
Name=LightHouse
Comment=Turn Index Lighthouses On/Off
Exec=/path/to/lh.py
Icon=scanner
Terminal=false
Type=Application
Categories=Utility;Application;

This actually works better for me right now than the semi-broken Windows power management.

Also, make sure to set chmod u+x on both the .desktop file and lh.py.

Full modified script can be found in this fork:
https://gist.github.com/waylonflinn/d525e08674ec3abb5c98cd41d1fd2f24

@Firestorm7893
Copy link

Firestorm7893 commented Jan 16, 2021

I know this code is really old, but has anybody had troubles with this error?
Traceback (most recent call last):
File "lh.py", line 35, in <module>
scanner.scan(2)
File "/usr/local/lib/python3.8/dist-packages/bluepy/btle.py", line 852, in scan
self.start(passive=passive)
File "/usr/local/lib/python3.8/dist-packages/bluepy/btle.py", line 797, in start
self._mgmtCmd(self._cmd()+"end")
File "/usr/local/lib/python3.8/dist-packages/bluepy/btle.py", line 312, in _mgmtCmd
raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'scanend' (code: 13, error: Invalid Parameters)

It's a really frustrating issue, and it happens even with a different bluetooth adapter

@Patola
Copy link

Patola commented Jan 16, 2021

Yes, run the command as root, or use setcap on bluepy
sudo setcap cap_net_raw+e /home/waylonflinn/.local/lib/python3.8/site-packages/bluepy/bluepy-helper
sudo setcap cap_net_admin+eip /home/waylonflinn/.local/lib/python3.8/site-packages/bluepy/bluepy-helper

@Firestorm7893
Copy link

Firestorm7893 commented Jan 17, 2021

Ok thanks now it doesn't make that error anymore. But looks like my lightouses don't seem to care 😢
The script exits without any error but the stations do not start

@Firestorm7893
Copy link

Update: just noticed the script is supposed to say when it found a lighthouse. But I'm getting no output whatsoever, so i guess it's not finding them

@prefiks
Copy link
Author

prefiks commented Jan 17, 2021

Hi,

Please take look at https://github.com/DavidRisch/steamvr_utils, it expnads on this script, and maybe it will work for you.

@Firestorm7893
Copy link

Fixed the problem, I forgot my base stations where 1.0 and not 2.0 so it would not be able to find them anyway. https://github.com/risa2000/lhctrl this is the script for 1.0 if anyone is interested
Thanks for the help :)

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