In: Computer Science
How to use Bluetooth to exchange Data between 2 computers. Develop a program with Python to demonstrate this data exchange.
"""
Python script to send messages to a sever over Bluetooth
using PyBluez (with Python 2).
This is how we send data to other person over bluteooth
"""
import bluetooth # importing PyBlueZ
serverMACAddress = '00:1f:e1:dd:08:3d' #Mac Address of the
server
port = 3 # Port number through which the data is transferred
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM) #create a
connection
s.connect((serverMACAddress, port)) #connect to the Mac Address
through port
#looping infinite times until a quit command is received
while 1:
text = raw_input() # Note change to the old (Python 2)
raw_input
if text == "quit":
break
s.send(text)
sock.close() # closing the connection
"""
Python script to receive messages from a client over
Bluetooth using PyBluez (with Python 2).
This is how we recieve messages over bluetooth connection
"""
import bluetooth
hostMACAddress = '00:1f:e1:dd:08:3d' # The MAC address of a
Bluetooth adapter on the server. The server might have multiple
Bluetooth adapters.
port = 3
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
client, clientInfo = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data) # Echo back to client
except:
print("Closing socket")
client.close()
s.close()