通过树莓派的蓝牙连接不起作用。 server_socket.bind((bind_ip,port))返回错误

时间:2019-03-03 21:19:48

标签: bluetooth raspberry-pi

我正在帮助某人调试树莓派项目的代码,使他们可以从链接到其手机的蓝牙连接中控制脚本。运行时,在行server_socket.bind((bind_ip,port))上出现错误,错误为OSError: An attempt was made to access a socket in a way forbidden by its access permissions.,我正在寻找有关如何解决问题的答案,或出于相同目的的替代方案。完整的代码在这里:

import bluetooth
import RPi.GPIO as GPIO        #calling for header file which helps in using GPIOs of PI
GPIO.setmode(GPIO.BCM)

LED=26
servoPin=8
GPIO.setup(servoPin, GPIO.OUT)
pwm=GPIO.PWM(servoPin, 50)
pwm.start(0)

GPIO.setwarnings(False)
GPIO.setup(LED,GPIO.OUT)  #initialize GPIO21 (LED) as an output Pin
GPIO.output(LED,1)
print ("init GPIO")


server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
print ("init socket")


port = 1
server_socket.bind(("",port))
server_socket.listen(1)

print ("server_socket.bind")

client_socket,address = server_socket.accept()
print "Accepted connection from ",address
while 1:

 data = client_socket.recv(1024)
 print "Received: %s" % data
 if (data == "1"):    #if '0' is sent from the Android App, turn OFF the LED
  print ("GPIO 26 LOW, LED OFF")
  GPIO.output(LED,0)
  QR=1./18.*(5)+2
  pwm.ChangeDutyCycle(QR)
 if (data == "0"):    #if '1' is sent from the Android App, turn OFF the LED
  print ("GPIO 26 HIGH, LED ON")
  GPIO.output(LED,1)
  DC=1./18.*(45)+2
  pwm.ChangeDutyCycle(DC)
 if (data == "5"):
  print ("CLOSE")
  QR=1./18.*(5)+2
  pwm.ChangeDutyCycle(QR)
 if(data=="45"):
  print ("OPEN")
  DC=1./18.*(45)+2
  pwm.ChangeDutyCycle(DC)
# break

client_socket.close()
server_socket.close()

0 个答案:

没有答案