Bonjour ,
après avoir bloqué toute l'après midi sur mon problème , je me décide à demander de l'aide ...

Voilà mon programme envoie une liste de commande à une carte électronique puis récupère le résultat et l'affiche .
Ça fonctionne bien sauf pour la commande "!v" qui utilise la fonction "public void recevoirMonitoring ()" pour se lancer .
La commande en elle même ainsi que son resultat fonctionne bien .

Le problème est que lorsque je clique sur un des boutons pour passer à la commande suivante , mon application freeze et fini donc par se faire fermer par android.
Je ne vois pas du tout d'où vient le problème avec des "Log.d" je me suis aperçu que le thread se terminais bien , je ne comprends donc pas ce qu'il se passe.

voici des extraits de mon code :

ChecklistActivity :
Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package com.example.projetcomplet;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
 
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
 
import android.app.Activity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import bluetooth.HexToString;
import bluetooth.SocketClient;
 
public class ChecklistActivity extends Activity {
 
 
	private HashMap<Integer,String> ListePourPrerequis;
	private HashMap<Integer, Test> tests;
	private ArrayList<Integer> checklist;
 
	private LogXML log;
 
	private boolean finish ;
	private int index = 0;
	private String resultat ="";
 
	private String OkorNok = "";
	private String CheckListName;
	private String currOk=null;
 
	// attributs pour le bluetooth
	private String ModuleName;
	private SocketClient socket;
 
	private String Version;
 
	private TextView Titre;
	private TextView NomTest;
	private TextView CmdEnvoyee;
	private TextView info;
	private TextView CmdResultat;
	private Button buttonOK;
	private Button buttonNOK;
 
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checklist_activity_layout);
 
		CheckListName = getIntent().getExtras().getString("nameList");
		ModuleName = getIntent().getExtras().getString("nameModule");
		tests = (HashMap<Integer, Test>) getIntent().getSerializableExtra("MapTests");	
		Version = getIntent().getExtras().getString("VersionLogiciel");
 
		ListePourPrerequis = new HashMap<>();
 
		Titre = (TextView)findViewById(R.id.Titre);
		NomTest = (TextView)findViewById(R.id.NomTest);
		CmdEnvoyee = (TextView)findViewById(R.id.CmdEnvoyee);
		info = (TextView)findViewById(R.id.Info);
		CmdResultat = (TextView)findViewById(R.id.CmdResultat);
		buttonOK = (Button)findViewById(R.id.ButtonOK);
		buttonNOK = (Button)findViewById(R.id.ButtonNOK);
 
		finish=false;
 
		new Thread(new Runnable() {
 
			@Override
			public void run() {
				try {
					checklist = parseCheckLists(CheckListName);
				} catch (XmlPullParserException | IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finish = true;
			}
		}).start();
 
		while(!finish)
		{
 
		}
 
		socket = new SocketClient(ModuleName, this);
		socket.connect();
 
 
		if(savedInstanceState != null)
		{
			index = savedInstanceState.getInt("index");
			log = (LogXML) savedInstanceState.getSerializable("log");
			resultat = savedInstanceState.getString("resultat");
			if(index<checklist.size())
			{
				AfficherTest(index,	OkorNok,resultat);
			}
			else
			{
				AfficherTerminer();	
			}
		}
		else
		{
			index = 0;
			OkorNok = "OK";
 
			log = new LogXML();
 
			HashMap<String, String> map = new HashMap<>();
			map.put("version_logiciel", Version);
			map.put("nom_checklist", CheckListName);
			map.put("date", new Date().toString());
 
			log.DebutChecklist(map);
 
			Log.d("DEBUG","tests ="+tests);
			Log.d("DEBUG","checklist ="+checklist);
			Log.d("DEBUG","checklist.get(index) = "+ checklist.get(index));
			Test currTest = tests.get(checklist.get(index));
			if(!currTest.getCommandeComplete().equals(""))
			{
				resultat = socket.SendAndReceive(currTest.getCommandeComplete());
			}
			else
			{
				resultat = "";
			}
			log.LogTest(currTest, OkorNok, resultat);
			AfficherTest(index,OkorNok,resultat);
		}
 
				buttonOK.setOnClickListener(new View.OnClickListener() {
 
					@Override
					public void onClick(View v) {
 
						OkorNok = "OK";
						ListePourPrerequis.put(index, OkorNok);
						index++;
 
						socket.cancel();
						HexToString hextostr = new HexToString();
						String arret = hextostr.convertHexToString("030A0D");
						socket.SendAndReceive(arret);
						if(index<checklist.size())
						{
 
							 currOk = tests.get(checklist.get(index)).getPrerequis();
							 int currPrerequis = tests.get(checklist.get(index)).getTestprerequis();
							if(currOk!=null)
							{
								if(currOk.equalsIgnoreCase(ListePourPrerequis.get(currPrerequis)))
								{
									Test currTest = tests.get(checklist.get(index));
									if(!currTest.getCommandeComplete().equals(""))
									{
										resultat = socket.SendAndReceive(currTest.getCommandeComplete());
									}
									else
									{
										resultat="";
									}
									log.LogTest( currTest, OkorNok, resultat);
 
									AfficherTest(index,OkorNok,resultat);
								}
								else
								{
 
									index++;
									if(index<checklist.size())
									{
										Test currTest = tests.get(checklist.get(index));
										if(!currTest.getCommandeComplete().equals(""))
										{
											resultat = socket.SendAndReceive(currTest.getCommandeComplete());
										}
										else
										{
											resultat="";
										}
										log.LogTest( currTest, OkorNok, resultat);
										AfficherTest(index,OkorNok,resultat);
									}
									else if(index>checklist.size())
									{
										 log.FinChecklist();
										 finish();
									}
									else
									{
										AfficherTerminer();
									}
								}
							}
							else
							{
								Test currTest = tests.get(checklist.get(index));
								if(!currTest.getCommandeComplete().equals(""))
								{
									resultat = socket.SendAndReceive(currTest.getCommandeComplete());
								}
								else
								{
									resultat = "";
								}
								log.LogTest( currTest, OkorNok, resultat);
								AfficherTest(index,OkorNok,resultat);
							}
						}
						else if(index>checklist.size())
						{
							log.FinChecklist();
							 finish();
						}
						else
						{	
							AfficherTerminer();
						}
					}
 
				});
				buttonNOK.setOnClickListener(new View.OnClickListener() {
 
					@Override
					public void onClick(View v) {
 
						OkorNok = "NOK";
						socket.cancel();
						ListePourPrerequis.put(index, OkorNok);
						index++;
 
						HexToString hextostr = new HexToString();
						String arret = hextostr.convertHexToString("030A0D");
						socket.SendAndReceive(arret);
						if(index<checklist.size())
						{
 
							 currOk = tests.get(checklist.get(index)).getPrerequis();
							 int currPrerequis = tests.get(checklist.get(index)).getTestprerequis();
							if(currOk!=null)
							{
								if(currOk.equalsIgnoreCase(ListePourPrerequis.get(currPrerequis)))
								{
									Test currTest = tests.get(checklist.get(index));
									if(!currTest.getCommandeComplete().equals(""))
									{
										resultat = socket.SendAndReceive(currTest.getCommandeComplete());
									}
									else
									{
										resultat = "";
									}
									log.LogTest(currTest, OkorNok, resultat);
									AfficherTest(index,OkorNok,resultat);
								}
								else
								{
 
									index++;
									if(index<checklist.size())
									{
										Test currTest = tests.get(checklist.get(index));
										if(!currTest.getCommandeComplete().equals(""))
										{
											resultat = socket.SendAndReceive(currTest.getCommandeComplete());
										}
										else
										{
											resultat = "";
										}
										log.LogTest(currTest, OkorNok, resultat);
										AfficherTest(index,OkorNok,resultat);
									}
									else if(index>checklist.size())
									{
										 log.FinChecklist();
										 finish();
									}
									else
									{
 
										AfficherTerminer();
									}
								}
							}
							else
							{
 
								Test currTest = tests.get(checklist.get(index));
								if(!currTest.getCommandeComplete().equals(""))
								{
									resultat = socket.SendAndReceive(currTest.getCommandeComplete());
								}
								else
								{
									resultat = "";
								}
								log.LogTest(currTest, OkorNok, resultat);
								AfficherTest(index,OkorNok,resultat);
							}
						}
						else if(index>checklist.size())
						{
							 log.FinChecklist();
							 finish();
						}
						else
						{
							AfficherTerminer();
						}
					}
 
				});
 
	}
 
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(ChecklistActivity.this,ChooseCheckListActivity.class);
		startActivity(intent);
		finish();
	}
 
	public ArrayList<Integer> parseCheckLists(String nom) throws XmlPullParserException, IOException
	{
		ArrayList<Integer> list = null;
		XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
		XmlPullParser parser = pullParserFactory.newPullParser();;
		try {
 
 
				InputStream in_s = new FileInputStream("/sdcard/Download/CheckLists.xml");
 
				parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
				parser.setInput(in_s,null);
 
 
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		String text = null;
		String NomChecklist = null;
		int eventType = parser.getEventType();
		boolean continuer = true;
		while(eventType != XmlPullParser.END_DOCUMENT && continuer)
		{
			String name = null;
			name = parser.getName();
			switch (eventType) 
			{
				case XmlPullParser.START_TAG:
					if(name.equals("checklist"))
					{
						NomChecklist = parser.getAttributeValue(null, "name");
						if(NomChecklist.equals(nom))
						{
							list = new ArrayList<>();
						}
					}
					break;
				case XmlPullParser.TEXT:	
					text = parser.getText();
					break;
				case XmlPullParser.END_TAG:
					if(list!=null)
					{
						if(name.equals("id"))
						{
							list.add(Integer.parseInt(text));
						}
						else if(name.equals("checklist"))
						{
							if(NomChecklist.equals(nom))
							{
								continuer = false;
							}
						}
					}
 
			}
			eventType = parser.next();	
		}
 
		return list;
	}
 
	public void AfficherTest(int i,String okOrnot,String resultat)
	{
		Log.d("check","AfficherTest lancé");
		info.setText("");
		CmdEnvoyee.setText("");
		CmdResultat.setText("");
 
 
		Test currTest = tests.get(checklist.get(i));
 
 
		Titre.setText("CheckList : " + CheckListName);
		NomTest.setText("Test n°"+checklist.get(i)+" : " + currTest.getNom());
		if(currTest.getInfo() != null)
		{
			info.setText("info :" + currTest.getInfo());
		}
 
		if(!currTest.getCommandeComplete().equals(""))
		{
			CmdEnvoyee.setText("Commande envoyée : " + currTest.getCommandeComplete());
			CmdResultat.setText("Resultat :"+resultat);
		}		
 
	}
 
	public void SocketConnect()
	{
		socket = new SocketClient(ModuleName, this);
		socket.connect();
		while(!socket.isConnected())
		{
 
		}		
	}
 
	public void addResultat(String string)
	{
		CmdResultat.append(string+"\n");
	}
 
 
	public void AfficherTerminer()
	{
		Log.d("check","afficherTerminer lancé");
 
		info.setText("");
		CmdEnvoyee.setText("");
		CmdResultat.setText("");
 
		// 4 = INVISIBLE
		CmdResultat.setVisibility(4);
 
		Titre.setText("CheckList : " + CheckListName);
		NomTest.setText("La checklist est terminée !");
		buttonNOK.setEnabled(false);
	}
 
 
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putInt("index", index);
		outState.putSerializable("log", (Serializable) log);
		outState.putString("resultat", resultat);
	}
 
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		socket.close();
	}
}
SocketClient :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	public String SendAndReceive(String commande)
	{
		Log.d("sendAndReceive","commande envoyée :"+commande);
		emission.send(commande+"\r\n");
		commande = commande.trim().toLowerCase();
		if(commande.equalsIgnoreCase("!v"))
		{
			reception.recevoirMonitoring();
			return "";
		}
		else
		{
			return reception.recevoir();
		}
	}
 
 
	public void cancel()
	{
		reception.cancel();
	}
reception :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
	public void recevoirMonitoring ()
	{
		thread = new ThreadReceptMonitoring();
		thread.start();
	}
 
	// Thread pour la fonction recevoir normale
	public class ThreadReceptMonitoring extends Thread
	{
 
 
		private boolean run;
 
		@Override
		public void run() {
			try {
				run=true;
				while(run)
				{
					Log.d("continuer","run="+run);
					message = "";
					Log.d("socket","reception message...");
					char caractere = '0';
 
					while(run && ((caractere=(char) bufferedReader.read()) != '>') )
					{
						Log.d("socket","Reception caractere");
						message+=caractere;
					}
 
					message=message.trim();
					message+='>';
					Log.d("socket","message final = " + message );
					final String recu = message;
 
					activity.runOnUiThread(new Runnable() {
 
						@Override
						public void run() {
							if(!recu.equals(""))
							{
								((ChecklistActivity)activity).addResultat(recu);
							}
 
						}
					});
					Log.d("thread","fin thread");
 
				}
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
 
		public void cancel()
		{
			run=false;
		}
	}
 
 
	public void cancel()
	{
		if(thread!=null)
		{
			thread.cancel();
		}
		else
		{
			Log.d("Thread","cancel impossible thread = null");
		}
	}

merci d'avance pour toute aide apportée ... j'en ai vraiment besoin .