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?
Best Answer
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:
Client Part (copy paste for the docs):
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 addressClient Code: change
serverMACAddress
to what you wrote at 'hostMACAddress'