Reputation: 2371
I'm trying to make a simple bluetooth service on Python using PyBluez and looks like it works, but I can't find that service with neither my phone, nor with PyBluez itself. I'm able to discovery my phone bluetooth when I do search with PyBluez, but not the server created with PyBluez. What's wrong with the code bellow?
import bluetooth
server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 0 # automatically choose port
server_sock.bind(("",port))
server_sock.listen(1)
uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848"
bluetooth.advertise_service( server_sock, "FooBar Service", uuid )
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()
I'm enabling service visibility with hciconfig hci0 piscan
, so my phone is able to find a bluetooth device with name of my PC, but not the service I've created (I mean FooBar Service
).
Also I can't find my service using sdptool browse
command.
Do I misunderstand something? How to connect to the service I've created?
update
Here is the below mentioned code result executed on my Windows 7 PC with D-Link Bluetooth adapter.
update
Finally it worked when I run client from another computer. It doesn't work locally, but why I can't discover this service from any device? Is there anything special I should do to make it behave like a normal Bluetooth service?
Upvotes: 3
Views: 13303
Reputation: 88
I was able to resolve this issue by changing the address
argument of bluetooth.find_service
to localhost
. This is a special case that allows you to search for local SDP, https://people.csail.mit.edu/albert/bluez-intro/x290.html.
Upvotes: 0
Reputation: 7931
First of all your code it's not a service it's a Server that uses uses RFCOMM sockets.
It's very similar to the use of python socket programming (you can read more here)
I'll use a simple example from the PyBluez documentation:
Server part:
from bluetooth import *
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 ])
print("Waiting for connection on RFCOMM channel %d" % port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)
#this part will try to get something form the client
# you are missing this part - please see it's an endlees loop!!
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print("received [%s]" % data)
# raise an exception if there was any error
except IOError:
pass
print("disconnected")
client_sock.close()
server_sock.close()
Client Part (copy paste for the docs):
from bluetooth import *
import sys
if sys.version < '3':
input = raw_input
addr = None
if len(sys.argv) < 2:
print("no device specified. Searching all nearby bluetooth devices for")
print("the SampleServer service")
else:
addr = sys.argv[1]
print("Searching for SampleServer on %s" % addr)
# search for the SampleServer service
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
service_matches = find_service( uuid = uuid, address = addr )
if len(service_matches) == 0:
print("couldn't find the SampleServer service =(")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("connecting to \"%s\" on %s" % (name, host))
# Create the client socket
sock=BluetoothSocket( RFCOMM )
sock.connect((host, port))
print("connected. type stuff")
while True:
data = input()
if len(data) == 0: break
sock.send(data)
sock.close()
Your Problem:
I can't see any client code, you need to create a client and to connect the server.
You need to change it just like above, run the Server and then the Client.
Then you will see at the server the client connection.
Another useful link is here it really helped me.
Edit:
Since my current desktop doesn't have BT it's a bit tricky.
There's a great example for BlueZ server-client here at the bottom.
It is using MAC address:
Server Code: change the hostMACAddress
param - it's your adapter mac address
Client Code: change serverMACAddress
to what you wrote at 'hostMACAddress'
Upvotes: 5