@@ -28,7 +28,7 @@ public class ControlActivity extends AppCompatActivity implements View.OnClickLi
2828 private InputStream inputStream ;
2929 private final UUID PORT_UUID = UUID .fromString ("00001101-0000-1000-8000-00805f9b34fb" );
3030 //Adress of bluetooth module
31- public static String DEVICE_ADDRESS = "" ;
31+ public static String DEVICE_ADDRESS ;
3232 String message ;
3333 byte buffer [];
3434 boolean stopThread ;
@@ -67,114 +67,95 @@ protected void onCreate(Bundle savedInstanceState) {
6767 ibDown .setOnClickListener (this );
6868 }
6969
70+ @ Override
71+ protected void onResume () {
72+ super .onResume ();
73+ connectDisconnect ();
74+ }
75+
7076 @ Override
7177 public void onClick (View view ) {
72- switch (view .getId ()) {
73- case R .id .ibConnect :
74- if (DEVICE_ADDRESS !="" ){
75- if (deviceConnected ) {
76- stopThread = true ;
77- try {
78- outputStream .close ();
79- } catch (IOException e ) {
80- e .printStackTrace ();
81- }
82- try {
83- inputStream .close ();
84- } catch (IOException e ) {
85- e .printStackTrace ();
86- }
87- try {
88- socket .close ();
89- } catch (IOException e ) {
90- e .printStackTrace ();
91- }
92- deviceConnected = false ;
93- ibConnect .setImageResource (R .drawable .connect );
94- ibConnect .setBackgroundResource (R .color .colorAccent );
95- }else {
96- if (BTinit ()){
97- if (BTconnect ()) {
98- deviceConnected = true ;
99- beginListenForData ();
100- ibConnect .setImageResource (R .drawable .disconnect );
101- ibConnect .setBackgroundColor (Color .RED );
102- }
103- }
104- }
105- }else {
106- Toast .makeText (getApplicationContext (), "Select a device" , Toast .LENGTH_SHORT ).show ();
107- Intent intent =new Intent (this , PairedDevices .class );
108- startActivity (intent );
109- }
78+ if (view .getId ()==R .id .ibConnect ){
79+ if (DEVICE_ADDRESS != "" && DEVICE_ADDRESS !=null ) {
80+ connectDisconnect ();
81+ } else {
82+ Toast .makeText (getApplicationContext (), "Select a device" , Toast .LENGTH_SHORT ).show ();
83+ Intent intent = new Intent (this , PairedDevices .class );
84+ startActivity (intent );
85+ }
11086
111- break ;
112- case R .id .ibUp :
113- message ="a" ;
114- try {
115- outputStream .write (message .getBytes ());
116- } catch (IOException e ) {
117- e .printStackTrace ();
118- }
119- break ;
120- case R .id .ibRight :
121- message = "b" ;
122- try {
123- outputStream .write (message .getBytes ());
124- } catch (IOException e ) {
125- e .printStackTrace ();
126- }
127- break ;
128- case R .id .ibStop :
129- message = "c" ;
130- try {
131- outputStream .write (message .getBytes ());
132- } catch (IOException e ) {
133- e .printStackTrace ();
134- }
135- break ;
136- case R .id .ibLeft :
137- message = "d" ;
138- try {
139- outputStream .write (message .getBytes ());
140- } catch (IOException e ) {
141- e .printStackTrace ();
142- }
143- break ;
144- case R .id .ibDown :
145- message = "e" ;
146- try {
147- outputStream .write (message .getBytes ());
148- } catch (IOException e ) {
149- e .printStackTrace ();
150- }
151- break ;
152- case R .id .ibBuzzer :
153- if (statusBuzzer ){
154- message = "z" ;
155- }else {
156- message = "p" ;
157- }
158- statusBuzzer =!statusBuzzer ;
159- try {
160- outputStream .write (message .getBytes ());
161- } catch (IOException e ) {
162- e .printStackTrace ();
163- }
164- break ;
165- case R .id .ibLeds :
166- if (statusLed ){
167- message = "w" ;
168- }else {
169- message = "q" ;
170- }
171- statusLed =!statusLed ;
172- try {
173- outputStream .write (message .getBytes ());
174- } catch (IOException e ) {
175- e .printStackTrace ();
176- }
177- break ;
87+ }
88+ if (deviceConnected ) {
89+ switch (view .getId ()) {
90+ case R .id .ibUp :
91+ message = "a" ;
92+ try {
93+ outputStream .write (message .getBytes ());
94+ } catch (IOException e ) {
95+ e .printStackTrace ();
96+ }
97+ break ;
98+ case R .id .ibRight :
99+ message = "b" ;
100+ try {
101+ outputStream .write (message .getBytes ());
102+ } catch (IOException e ) {
103+ e .printStackTrace ();
104+ }
105+ break ;
106+ case R .id .ibStop :
107+ message = "c" ;
108+ try {
109+ outputStream .write (message .getBytes ());
110+ } catch (IOException e ) {
111+ e .printStackTrace ();
112+ }
113+ break ;
114+ case R .id .ibLeft :
115+ message = "d" ;
116+ try {
117+ outputStream .write (message .getBytes ());
118+ } catch (IOException e ) {
119+ e .printStackTrace ();
120+ }
121+ break ;
122+ case R .id .ibDown :
123+ message = "e" ;
124+ try {
125+ outputStream .write (message .getBytes ());
126+ } catch (IOException e ) {
127+ e .printStackTrace ();
128+ }
129+ break ;
130+ case R .id .ibBuzzer :
131+ if (statusBuzzer ) {
132+ message = "z" ;
133+ } else {
134+ message = "p" ;
135+ }
136+ statusBuzzer = !statusBuzzer ;
137+ try {
138+ outputStream .write (message .getBytes ());
139+ } catch (IOException e ) {
140+ e .printStackTrace ();
141+ }
142+ break ;
143+ case R .id .ibLeds :
144+ if (statusLed ) {
145+ message = "w" ;
146+ } else {
147+ message = "q" ;
148+ }
149+ statusLed = !statusLed ;
150+ try {
151+ outputStream .write (message .getBytes ());
152+ } catch (IOException e ) {
153+ e .printStackTrace ();
154+ }
155+ break ;
156+ }
157+ }else {
158+ Toast .makeText (getApplicationContext (), "First connect the bluetooth" , Toast .LENGTH_SHORT ).show ();
178159 }
179160 }
180161 public boolean BTconnect (){
@@ -260,4 +241,42 @@ public void run(){
260241
261242 thread .start ();
262243 }
244+ public void connectDisconnect (){
245+ if (DEVICE_ADDRESS !="" && DEVICE_ADDRESS !=null ) {
246+ if (deviceConnected ) {
247+ stopThread = true ;
248+ try {
249+ outputStream .close ();
250+ } catch (IOException e ) {
251+ e .printStackTrace ();
252+ }
253+ try {
254+ inputStream .close ();
255+ } catch (IOException e ) {
256+ e .printStackTrace ();
257+ }
258+ try {
259+ socket .close ();
260+ } catch (IOException e ) {
261+ e .printStackTrace ();
262+ }
263+ deviceConnected = false ;
264+ ibConnect .setImageResource (R .drawable .connect );
265+ ibConnect .setBackgroundResource (R .color .colorAccent );
266+ if (!deviceConnected ) Toast .makeText (getApplicationContext (), "Disconnected" , Toast .LENGTH_SHORT ).show ();
267+ else Toast .makeText (getApplicationContext (), "The device could not disconnect" , Toast .LENGTH_SHORT ).show ();
268+ } else {
269+ if (BTinit ()) {
270+ if (BTconnect ()) {
271+ deviceConnected = true ;
272+ beginListenForData ();
273+ ibConnect .setImageResource (R .drawable .disconnect );
274+ ibConnect .setBackgroundColor (Color .RED );
275+ }
276+ }
277+ if (deviceConnected ) Toast .makeText (getApplicationContext (), "Connected" , Toast .LENGTH_SHORT ).show ();
278+ else Toast .makeText (getApplicationContext (), "The device could not connect" , Toast .LENGTH_SHORT ).show ();
279+ }
280+ }
281+ }
263282}
0 commit comments