Page 1 of 1

Cant connect raspberry with phone through bluetooth

Posted: Mon Oct 07, 2019 10:06 pm
by Steven555
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()








Re: Cant connect raspberry with phone through bluetooth

Posted: Tue Oct 08, 2019 12:35 am
by Douglas6
See if this post helps: https://www.raspberrypi.org/forums/view ... 85#p947185.

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

Re: Cant connect raspberry with phone through bluetooth

Posted: Thu Oct 10, 2019 7:16 am
by Steven555
I am using a samsung