-
Notifications
You must be signed in to change notification settings - Fork 1
/
android.py
147 lines (107 loc) · 4.35 KB
/
android.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
from datetime import datetime
from bluetooth import *
import os as os
from config import *
class Android():
def __init__(self):
print("Android starting to initialise")
self.server_sock = None
self.client_sock = None
os.system("sudo hciconfig hci0 piscan")
print("Finished BT initialising")
def connect(self, uuid):
try:
self.server_sock = BluetoothSocket(RFCOMM)
# self.server_sock.allow_reuse_address = True
# self.server_sock = self.server_socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# self.server_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
self.server_sock.bind(("", RFCOMM_PORT))
self.server_sock.listen(RFCOMM_PORT)
port = self.server_sock.getsockname()[1]
advertise_service(self.server_sock, "RPi-RFCOMM",
service_id=uuid,
service_classes=[uuid, SERIAL_PORT_CLASS],
profiles=[SERIAL_PORT_PROFILE], )
print("-----------------------------------------")
print("Waiting connection from RFCOMM channel %d" % port)
self.client_sock, client_info = self.server_sock.accept()
secure = client_info[0]
print("Accepted connection from ", client_info)
print("Connected to Android!")
return 1
except Exception as e:
print("Bluetooth connection exception: %s" % str(e))
try:
print("%s" % str(e))
self.client_sock.close()
self.client_sock = None
except:
print("Error")
return 0
def current_time(self):
return datetime.now().strftime("%d/%m/%Y %H:%M:%S.%f") + '\t'
def disconnect(self):
#disconnect client_socket
try:
if self.client_sock is not None:
self.client_sock.close()
self.client_sock = None
print("Android has been disconnected successfully")
except Exception as e:
print("Bluetooth disconnection exception: %s" % str(e))
def disconnect_all(self):
#disconnect both client_socket and server socket
try:
if self.client_sock is not None:
self.client_sock.close()
self.client_sock = None
if self.server_sock is not None:
self.server_sock.close()
self.server_sock = None
print("Android has been disconnected Successfully")
except Exception as e:
print("Bluetooth disconnection exception: %s" % str(e))
def reconnect(self):
connected = 0
while connected == 0:
print("Attempting reconnection...")
# self.disconnect()
time.sleep(1)
connected = self.connect(UUID)
def write(self, msg):
try:
if msg == "":
print("Socket WRITE: Closed. Skipped write.")
return
elif msg == "REP":
print("Socket WRITE: Repeat Read -- ")
msg = self.read(timeout=None)
self.client_sock.send(msg) #did not encode here
except Exception as e:
print("Bluetooth write exception: %s" % str(e))
self.reconnect()
def read(self):
try:
message = self.client_sock.recv(ANDROID_SOCKET_BUFFER_SIZE).decode('utf-8')
print('From android:')
print(message)
if message is None:
return None
if len(message) > 0:
return message
return None
except Exception as e:
print("Bluetooth read exception: %s" % str(e))
self.reconnect()
if __name__ == '__main__':
android = Android()
android.connect(UUID)
try:
while True:
android.read()
android.write(input("Socket WRITE: Opened.\nSend Message: "))
except KeyboardInterrupt:
print("Terminating the program now...")