-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtserv.py
More file actions
44 lines (31 loc) · 832 Bytes
/
btserv.py
File metadata and controls
44 lines (31 loc) · 832 Bytes
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
#
# bt server
#
#from bluetooth import *
import socket
import signal
import sys # needed for exit
BTAddr = '40:1C:83:C9:9F:81'
#BTAddr = ''
#BTsocket = BluetoothSocket (RFCOMM)
#BTsocket.connect ((BTAddr), 1)
#socket s;
# graceful exit on CNTRL-C
def signal_handler(sig, frame):
# close port and exit
print("\nCaught signal, exiting ....")
s.close() # not shutdown as this is a broadcast stream
sys.exit(0)
# SIGINT
signal.signal(signal.SIGINT, signal_handler)
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((BTAddr, 5))
s.listen(1)
print("Bind and listen, waiting for call")
conn, addr = s.accept() # creates a new socket
print("connection from", addr)
while (True):
data = conn.recv(1024)
if not data: # socket was closed
break
print(data)