Android – serial communication via Bluetooth using Python
How do you process and receive serial data via Bluetooth and python?
I'm trying to make a simple Python server that accesses data as explained here through Bluetooth
My server.py file sends a random number when sending the text "temp", which is:
#!/usr/bin/env python
import os
import glob
import time
import random
from bluetooth import *
def read_temp():
return random.random()
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, "TestServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
while True:
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
if data == 'temp':
data = str(read_temp())+'!'
client_sock.send(data)
else:
data = 'WTF!'
client_sock.send(data)
print "sending [%s]" % data
except IOError:
pass
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
break
When I first ran it, I got an error:
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
You can fix Google shown by enabling compatibility mode and loading the serial configuration file by editing the file / lib / SYSTEMd / system / bluetooth.service and changing the line
ExecStart=/usr/lib/bluetooth/bluetoothd
To:
ExecStart=/usr/lib/bluetooth/bluetoothd -C
Then run sudo sdptool add sp
Now Python server.py seems to work normally, and I can pair the machine running it with my Android phone. However, it seems that I can't receive any data
I have tried to use Bluetooth terminal emulation applications, such as blueterm, BT simple terminal and Arduino BT, but when I connect to the server, enter the text and press enter, server.py does not respond. It will initially report "accepted connection..." and has received an empty string, but will receive nothing thereafter
Since no obvious errors were reported, I'm not sure how to diagnose the problem. How to determine whether the problem is in my Python code? Or Bluetooth configuration on the server? Or my Android phone?
resolvent:
The correct code is:
#!/usr/bin/env python
"""
A simple test server that returns a random number when sent the text "temp" via Bluetooth serial.
"""
import os
import glob
import time
import random
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, "TestServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
while True:
try:
req = client_sock.recv(1024)
if len(req) == 0:
break
print "received [%s]" % req
data = None
if req in ('temp', '*temp'):
data = str(random.random())+'!'
else:
pass
if data:
print "sending [%s]" % data
client_sock.send(data)
except IOError:
pass
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
break