We have seen that Bluetooth devices can locate other nearby devices, those that are in discovery mode. Rather than connecting by the address and port of a device and application, devices can discover the address and port by the service names.
The following version performs the same function but using SDP. On the server-side, the primary use of SDP to advertise the helloService as a serial port or RFCOMM protocols.
The client first finds a list of devices that provide the helloService (in reality, more than just helloService is returned). The list is then searched for the helloService, when found, the associated host address and port are used to connect to the service.
from bluetooth import *
for i in range(len(services)):
print name, port, host
client_socket=BluetoothSocket( RFCOMM )
server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
client_sock, address = server_sock.accept()
print "Accepted connection from ",address
data = client_sock.recv(1024)
print "received [%s]" % data