Skip to content

Commit 9265250

Browse files
committed
created blueetoh helper and widgets
1 parent eb5a8f3 commit 9265250

File tree

4 files changed

+577
-352
lines changed

4 files changed

+577
-352
lines changed

bluetoothHelper.py

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
# simple inquiry example
2+
import bluetooth
3+
from myutils import *
4+
import time
5+
6+
class BluetoothSocketHelper(TransceiverHelper):
7+
def __init__(self):
8+
super(BluetoothSocketHelper,self).__init__()
9+
self.is_connected = False
10+
self.address = ""
11+
self.port = 1
12+
self.socket = bluetooth.BluetoothSocket()
13+
14+
self.on_msg_received.add(self.printInput)
15+
16+
17+
18+
19+
20+
def sendStr(self,str):
21+
22+
if self.is_connected:
23+
try:
24+
self.socket.send(str.encode())
25+
self.on_msg_sent.broadcast(str)
26+
except:
27+
self.info_broadcaster.broadcast("Send Error")
28+
29+
30+
def read(self):
31+
x = ""
32+
if self.is_connected:
33+
data = self.socket.recv(1)
34+
35+
36+
# x = data.decode('utf-8').strip()
37+
x = data.decode('utf-8').rstrip("\r\n")
38+
39+
if len(x):
40+
return x
41+
# print(f"x is {x} len x is {len(x)}")
42+
# else :
43+
# print("x is empty")
44+
45+
return ""
46+
47+
48+
49+
def readLine(self,doStrip = True):
50+
51+
doIt = True
52+
msg = ""
53+
54+
while doIt == True:
55+
x = self.read()
56+
if x and len(x) > 0:
57+
58+
msg += x
59+
60+
else:
61+
doIt = False
62+
break
63+
64+
65+
# print(f"read msg is {msg}")
66+
return msg
67+
# m = ""
68+
# l = 1
69+
# ok = True
70+
# try:
71+
# while True:
72+
# # try:
73+
# data = ""
74+
# try:
75+
# data = self.socket.recv(1)
76+
# except:
77+
# print("read data error data")
78+
79+
# if len(data) == 0:
80+
# print("breaking")
81+
# break
82+
83+
# else:
84+
# x = ""
85+
# try:
86+
# x = data.decode('utf-8')
87+
# except:
88+
# x = ""
89+
# print(f"x is {x}")
90+
91+
# if len(x) > 0:
92+
# print(f"lenx is {len(x)}")
93+
# m += x
94+
# print(f"msg is {m}")
95+
# else:
96+
97+
# break
98+
# print(f"lenx is 0")
99+
# ok = False
100+
101+
102+
# # except:
103+
# # ok = False
104+
105+
106+
107+
108+
109+
110+
# except:
111+
112+
# self.info_broadcaster.broadcast("Socket read error")
113+
114+
# if len(m) :
115+
116+
# if(doStrip):
117+
# m = m.rstrip("\r\n")
118+
119+
# print(f"message to return{m}")
120+
# return m
121+
122+
123+
124+
125+
def readInputLoop(self):
126+
127+
try:
128+
msg = self.readLine()
129+
if len(msg) > 0 :
130+
# print("readInputLoop -> Broadcasting ")
131+
self.on_msg_received.broadcast(msg)
132+
except:
133+
self.info_broadcaster.broadcast(self.__class__ + " readInputLoop Error")
134+
135+
136+
def waitForRawMessage(self, expected):
137+
pass
138+
# msg = ""
139+
# t = time.time()
140+
# ok = False
141+
# end_time = t+self.time_out
142+
143+
# while (ok == False) and (time.time() <= end_time):
144+
145+
# if self.inWaiting() > 0 :
146+
# x = self.read().decode('utf-8')
147+
# msg += x
148+
149+
# if msg == expected :
150+
# self.on_msg_received.broadcast(msg)
151+
# print("elapsed time is " + str(time.time()- t))
152+
# ok = True
153+
154+
# self.last_msg = msg
155+
156+
# return ok
157+
158+
159+
def getConnectionReadyMessage(self):
160+
pass
161+
# if len(self.connection_succes_msg):
162+
163+
# return self.waitForRawMessage(self.connection_succes_msg)
164+
# return self.is_open
165+
166+
def connect(self):
167+
168+
if(self.is_connected) :
169+
return self.is_connected
170+
171+
self.is_connected = False
172+
173+
try:
174+
self.socket.connect((self.address,self.port))
175+
176+
self.is_connected = True
177+
self.info_broadcaster.broadcast("Connected")
178+
self.input_loop.start()
179+
180+
except:
181+
self.info_broadcaster.broadcast("Connection error")
182+
183+
return self.is_connected
184+
185+
186+
# ok = False
187+
188+
# try:
189+
# self.open()
190+
# try:
191+
# self.info_broadcaster.broadcast("SERIAL CONNECTED, WAITING FOR READY MESSAGE")
192+
# # print("SERIAL CONNECTED, WAITING FOR READY MESSAGE")
193+
# if self.is_open and len(self.connection_succes_msg) > 0:
194+
# if self.getConnectionReadyMessage() :
195+
# self.info_broadcaster.broadcast("GOT WELCOME MESSAGE")
196+
# ok = True
197+
# else:
198+
# self.info_broadcaster.broadcast("CLOSING, WRONG WELCOME MESSAGE RECEIVED :" + self.last_msg)
199+
# if self.is_open:
200+
# self.close()
201+
# elif self.is_open:
202+
# ok = True
203+
204+
# if ok:
205+
# self.info_broadcaster.broadcast("CONNECTED")
206+
# self.input_loop.start()
207+
208+
209+
# except:
210+
# self.info_broadcaster.broadcast("CONNECTION ERROR")
211+
# # print("CONNECTION ERROR")
212+
213+
# except:
214+
# self.info_broadcaster.broadcast("SERIAL NOT AVAILABLE")
215+
# # print ("SERIAL NOT AVAILABLE")
216+
217+
# if ok == False and self.is_open:
218+
# try:
219+
# self.close()
220+
# except:
221+
# pass
222+
# return ok
223+
224+
def disconnect(self):
225+
226+
try :
227+
# self.input_loop.stop()
228+
self.is_connected = False
229+
self.socket.close()
230+
except:
231+
self.info_broadcaster.broadcast("Disconnect Error")
232+
233+
def printDebug(self,msg):
234+
print(msg)

0 commit comments

Comments
 (0)