forked from TheFwGuy/bluetooth_Rpi3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLtest.py
executable file
·75 lines (64 loc) · 2.12 KB
/
BLtest.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
# Initialize BLtest board
import sys
import time
import commands
import os
import bluetooth
from socket import error as socket_error
# Initialization
# Display IP address
print "IP WLAN: hostname -I"
# Set up Bluettoth
# Restart Bluetooth and set for slave
os.popen('sudo hciconfig hci0 reset')
os.popen('sudo hciconfig hci0 piscan')
print "Bluetooth initialization - set as slave"
# State machine for Bluetooth
# 0 -> waiting for connection
# 1 -> connected
state = 0
# Monitor push buttons
while True:
# Bluetooth management
if (state == 0):
# Set up Bluetooth socket
server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
server_socket.bind(("",port))
server_socket.listen(1) # Waiting for connection
# NOTE ! The line below waits for a connection blocking the loop !
try:
print "Wait for connection"
client_socket,address = server_socket.accept()
print "Accepted connection from ",address
state = 1
except Exception as ex:
template = "1) An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print message
if (state == 1):
try:
data = client_socket.recv(1024, 0x40)
print "Received: %s" % data
if (data == 'q\n'):
client_socket.close()
server_socket.close()
state = 0
print "Closed Bluettoth - disconnected"
except Exception as ex:
template = "2) An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
if type(ex).__name__ == "BluetoothError":
if ex.args[0] == "(104, 'Connection reset by peer')":
state = 0
client_socket.close()
server_socket.close()
print "Closed Bluettoth - disconnected"
elif ex.args[0] == "(11, 'Resource temporarily unavailable')":
# Ignore
a = ex.args[0]
else:
print message
else:
print message