2

wondering if anyone can help me with this. I'm very new to programming in general and am struggling getting my head around bluetooth. I have been trying to follow the examples given at http://people.csail.mit.edu/albert/bluez-intro/x264.html . My aim is to use a bluetooth camera button to trigger an action in python. I have been able to pair the button to the raspberry pi using bluetoothctl so it seems to work. I tried this python script

import bluetooth
import time
loop = True
while loop:
    result = bluetooth.lookup_name('01:AC:78:E6:EF:7F', timeout=20)
    if (result == None):
        print "not detected"
    else:
    print " Button found"
    break


time.sleep(5)

server_sock=bluetooth.BluetoothSocket( bluetooth.L2CAP )
port = 0x1001
server_sock.bind(("",port))
server_sock.listen(1)
print "listening on port %d" % port
bd_addr = '01:AC:78:E6:EF:7F'
service_matches = bluetooth.find_service( name = None, uuid = None, address = bd_addr)

if len(service_matches) == 0:

    print "cound't find service"
    sys.exit
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
protocol = first_match["protocol"]

print port
print name
print host
print protocol

server_sock.close()

` Running this in a terminal using sudo returns "button found listening on port 4097 17 keyboard 01:AC:78:E6:EF:7F L2CAP" So I know the protocol port etc and that the pi can see it. But I don't really know for to get it to connect. In particular which is the client and which is the server. and how to get the pi to allow connection or make connection with the button.
This is the example that I have tried

import bluetooth

server_sock=bluetooth.BluetoothSocket( bluetooth.L2CAP )

port = 0x1001
server_sock.bind(("",port))
server_sock.listen(1)

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

data = client_sock.recv(1024)
print "received [%s]" % data

client_sock.close()
server_sock.close()'

But to no avail. If anyone had any pointers or know of good examples I would greatly appreciate a few pointers.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.