IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.58k stars 490 forks source link

bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied) #448

Open Mike4proskills opened 3 years ago

Mike4proskills commented 3 years ago

I utilised and modified the code that is shown below, from https://www.instructables.com/Monitor-and-Record-Temperature-With-Bluetooth-LE-a/

The problem I run into is that the device seems to be blocking me from accessing it's values, although I am unsure of this.

Here is my code:

from bluepy.btle import Scanner, DefaultDelegate
import time
import struct

SENSOR_ADDRESS = ["ec:fe:4e:12:b8:72"]

class DecodeErrorException(Exception):
     def __init__(self, value):
         self.value = value
     def __str__(self):
         return repr(self.value)

class ScanDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if isNewDev:
            print("Discovered device", dev.addr)
        elif isNewData:
            print("Received new data from", dev.addr)

scanner = Scanner().withDelegate(ScanDelegate())

ManuDataHex = []
ReadLoop = True
try:
    while (ReadLoop):
        devices = scanner.scan(2.0)
        ManuData = ""

        for dev in devices:

            entry = 0
            TempData = 0

            for saddr in SENSOR_ADDRESS:

                entry += 1

                if (dev.addr == saddr):

                    CurrentDevAddr = saddr

                    for (adtype, desc, value) in dev.getScanData():

                        if (desc == "Manufacturer"):
                            ManuData = value

                    if (ManuData == ""):
                        print("No data received, end decoding")
                        continue

                    print(ManuData)
                    for i, j in zip(ManuData[::2], ManuData[1::2]):
                        ManuDataHex.append(int(i+j, 16))

                    if ((ManuDataHex[0] == 0x85) and (ManuDataHex[1] == 0x00)):
                        print("Header byte 0x0085 found")
                    else:
                        print("Header byte 0x0085 not found, decoding stop")
                        continue

                    idx = 7

                    print("TotalLen: " + str(len(ManuDataHex)))
                    while idx < len(ManuDataHex):

                          if (ManuDataHex[idx] == 0x43):

                              idx += 1
                              TempData = ManuDataHex[idx]
                              TempData += ManuDataHex[idx+1] * 0x100
                              TempData = TempData * 0.0625
                              idx += 2
                          else:
                              idx += 1

                    print("Device Address: " + CurrentDevAddr)
                    print("Temp Data: " + str(TempData))

                    ReadLoop = False

The exception is as follows: Traceback (most recent call last): File "/home/pi/Desktop/lameteo/print.py", line 31, in devices = scanner.scan(2.0) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 852, in scan self.start(passive=passive) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 790, in start self._mgmtCmd("le on") File "/usr/local/lib/python3.7/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 'le on' (code: 20, error: Permission Denied)

If you have any expertise in this area I would greatly appreciate your help.

brettroyer commented 3 years ago

Try running it with the sudo command.