This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
forked from hzeller/rpi-rgb-led-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt_server.py
58 lines (45 loc) · 1.72 KB
/
bt_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import bluetooth
import os
import signal
pid_file = "/home/pi/bt_server.pid"
# check if the pid file exists
if os.path.exists(pid_file):
# the pid file exists, so read the process ID from the file
with open(pid_file, 'r') as f:
pid = int(f.read())
# try to kill the process
try:
os.kill(pid, signal.SIGTERM)
except OSError:
pass
server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
server_sock.bind(("",bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "00001101-0000-1000-8000-00805F9B34FB"
bluetooth.advertise_service( server_sock, "My Bluetooth Server",
service_id = uuid,
service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ],
profiles = [ bluetooth.SERIAL_PORT_PROFILE ])
print("[+] Listening for incoming connections on RFCOMM channel %d" % port)
client_sock, client_info = server_sock.accept()
print("[+] Accepted connection from ", client_info)
while True:
data = client_sock.recv(1024)
if len(data) == 0:
print("[-] No data received")
else:
print("[+] Received: %s" % data)
if os.path.exists("/home/pi/src/" + data.decode("utf-8") + ".sh"):
os.popen("sh /home/pi/src/" + data.decode("utf-8") + ".sh")
os.popen("echo " + "sh /home/pi/src/" + data.decode("utf-8") + ".sh" + " >> /home/pi/bt_server.log")
client_sock.send("success")
else:
os.popen("echo couldn't find command for " + data.decode("utf-8") + " >> /home/pi/bt_server.log")
client_sock.send("failure")
if data == "quit":
break
print("[+] Disconnected")
client_sock.close()
server_sock.close()
print("[+] All done")