Steven555
Posts: 3
Joined: Mon Oct 07, 2019 10:03 pm

Cant connect raspberry with phone through bluetooth

Mon Oct 07, 2019 10:06 pm

I have being trying to connect my code to my phone but i have not being able to. In the bluetooth terminal on my phone, i keep getting the rror, connection failed: read failed, scoket might closed or timerout, read ret:-1, please can someone help me

Code: Select all

import bluetooth
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

Motor1a = 20
Motor1b = 21
Motor2a = 2
Motor2b = 3

GPIO.setup(Motor1a,GPIO.OUT)
p1=GPIO.PWM(Motor1a,50)
p1.start(0)

GPIO.setup(Motor1b,GPIO.OUT)
p2=GPIO.PWM(Motor1b,50)
p2.start(0)

GPIO.setup(Motor2a,GPIO.OUT)
p3=GPIO.PWM(Motor2a,50)
p3.start(0)

GPIO.setup(Motor2b,GPIO.OUT)
p4=GPIO.PWM(Motor2b,50)
p4.start(0)




server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port =1
server_socket.bind(("",port))
server_socket.listen(1)
client_socket,address = server_socket.accept()

print("Accepted connection from "+str(address))

def stop_car():
    p1.start(0)
    p2.start(0)
    p3.start(0)
    p4.start(0)

while True:
    data = client_socket.recv(1024)
    if (data == "B" or data == "b"):
        p1.start(50)
        p2.start(0)
        p3.start(50)
        p4.start(0)
        time.sleep(1)
        stop_car()


    if (data == "F" or data == "f"):
        p1.start(0)
        p2.start(50)
        p3.start(0)
        p4.start(50)
        time.sleep(1)
        stop_car()


    if (data == "R" or data == "R"):
        p1.start(0)
        p2.start(50)
        p3.start(50)
        p4.start(0)
        time.sleep(1)
        stop_car()



    if (data == "R" or data == "R"):
        p1.start(50)
        p2.start(0)
        p3.start(0)
        p4.start(50)
        time.sleep(1)
        stop_car()




    if (data == "Q" or data == "q"):
        stop_car()
    if (data == "Z" or data == "z"):
        client_socket.close()
        server_socket.close()








User avatar
Douglas6
Posts: 4779
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Cant connect raspberry with phone through bluetooth

Tue Oct 08, 2019 12:35 am

See if this post helps: https://www.raspberrypi.org/forums/view ... 85#p947185.

It will work for an Android phone, not an iPhone.

Steven555
Posts: 3
Joined: Mon Oct 07, 2019 10:03 pm

Re: Cant connect raspberry with phone through bluetooth

Thu Oct 10, 2019 7:16 am

I am using a samsung

Return to “Beginners”