rapberry pi: bluetooth.btcommon.BluetoothError: нет рекламного устройства в rc.local

ошибка возникает при запуске скрипта Python, включая подключение Bluetooth к другому устройству через rc.local. Ошибка не возникает при запуске через терминал с помощью sudo. Полное сообщение в /var/log/syslog гласит:

      Apr  3 20:25:01 raspberrypi systemd[1]: Starting /etc/rc.local Compatibility...
Apr  3 20:25:01 raspberrypi systemd[1]: Started /etc/rc.local Compatibility.
Apr  3 20:25:02 raspberrypi rc.local[515]: Can't get device info: No such device
Apr  3 20:25:04 raspberrypi rc.local[492]: Exception in thread Thread-2:
Apr  3 20:25:04 raspberrypi rc.local[492]: Traceback (most recent call last):
Apr  3 20:25:04 raspberrypi rc.local[492]:   File "/usr/local/lib/python3.9/dist-packages/bluetooth/bluez.py", line 271, in advertise_service
Apr  3 20:25:04 raspberrypi rc.local[492]:     _bt.sdp_advertise_service (sock._sock, name, service_id, \
Apr  3 20:25:04 raspberrypi rc.local[492]: _bluetooth.error: no advertisable device
Apr  3 20:25:04 raspberrypi rc.local[492]: During handling of the above exception, another exception occurred:
Apr  3 20:25:04 raspberrypi rc.local[492]: Traceback (most recent call last):
Apr  3 20:25:04 raspberrypi rc.local[492]:   File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
Apr  3 20:25:04 raspberrypi rc.local[492]:     self.run()
Apr  3 20:25:04 raspberrypi rc.local[492]:   File "/home/pi/Autosteuerung/AutosteuerungAutostart.py", line 44, in run
Apr  3 20:25:04 raspberrypi rc.local[492]:     advertise_service( server_sock, "SampleServer",
Apr  3 20:25:04 raspberrypi rc.local[492]:   File "/usr/local/lib/python3.9/dist-packages/bluetooth/bluez.py", line 275, in advertise_service
Apr  3 20:25:04 raspberrypi rc.local[492]:     raise BluetoothError (*e.args)
Apr  3 20:25:04 raspberrypi rc.local[492]: bluetooth.btcommon.BluetoothError: no advertisable device

Это скрипт, это контроллер для радиоуправляемой машины:

      import subprocess
import threading
from bluetooth import *
import pigpio
import time

subprocess.call(['pigpiod'])
subprocess.call(['hciconfig', 'hci0', 'piscan'])

pi = pigpio.pi()
pi.set_mode(2, pigpio.OUTPUT)
pi.set_mode(3, pigpio.OUTPUT)
pi.set_mode(23, pigpio.INPUT)
pi.set_mode(13, pigpio.OUTPUT)
pi.set_mode(26, pigpio.OUTPUT)
pi.set_pull_up_down(23, pigpio.PUD_UP)

class CarController(threading.Thread):
    global pi
    def __init__(self):
        threading.Thread.__init__ (self)
        
    def run(self):
        while(True):
            
            pi.write(13, 0)
            pi.write(26, 1)
            time.sleep(1)
            pi.write(13, 1)
            pi.write(26, 0)
            time.sleep(1)
            pi.write(13, 1)
            pi.write(26, 1)
        
            server_sock=BluetoothSocket( RFCOMM )
            server_sock.bind(("",PORT_ANY))
            server_sock.listen(1)

            port = server_sock.getsockname()[1]

            uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"

            advertise_service( server_sock, "SampleServer",
                               service_id = uuid,
                               service_classes = [ uuid, SERIAL_PORT_CLASS ],
                               profiles = [ SERIAL_PORT_PROFILE ], 
            #                   protocols = [ OBEX_UUID ] 
                                )
                           
            print("Waiting for connection on RFCOMM channel %d" % port)

            client_sock, client_info = server_sock.accept()
            print("Accepted connection from ", client_info)

            try:
                data_out = bytes(3)
                while True:
                    data_in = client_sock.recv(3)
                    if len(data_in) > 0:
                        gas = data_in[0] & 127
                        reverse = data_in[0] & 0b10000000
                        steer = data_in[1]
                        other = data_in[2]
                        if gas > 100:
                            pi.write(3, 0)
                            pi.hardware_PWM(18, 2000, 0)
                        else:
                            pi.write(3, 1)
                            pi.hardware_PWM(18, 2000, (gas * 10000))
                        if reverse:
                            pi.write(2, 1)
                        else:
                            pi.write(2, 0)
                        if steer < 7:
                            pi.write(13, 0)
                            pi.write(26, 1)
                        elif steer > 11:
                            pi.write(13, 1)
                            pi.write(26, 0)
                        else:
                            pi.write(13, 1)
                            pi.write(26, 1)
                    client_sock.send(data_out)
            except IOError:
                pi.hardware_PWM(18, 2000, 0)
                pass

            finally:
                print("disconnected")
            
                pi.hardware_PWM(18, 2000, 0)
                running = False
                client_sock.close()
                server_sock.close()
                print("all done")

try:

    shutdown = False
    carController = CarController()
    if(pi.read(23) == 0):
        shutdown = True
        carController.start()
        while(pi.read(23) == 00):
            time.sleep(1)

finally:
    pi.stop()
    if(shutdown):
        print("shutdown")
        subprocess.call(["shutdown", "-h", "now"])
    else:
        print("resume")

Я уже пробовал каждое решение для этого сообщения, которое я нашел, но все они касаются этой проблемы, возникающей из-за запуска терминала, а не rc.local.

0 ответов

Другие вопросы по тегам