Io-Trucks  0.2
BTS SNIR LaSalle Avignon 2020
Peripherique.java
Aller à la documentation de ce fichier.
1 package com.lasalle.io_trucks;
2 
3 import android.bluetooth.BluetoothDevice;
4 import android.bluetooth.BluetoothSocket;
5 import android.os.Handler;
6 import android.os.Message;
7 import android.util.Log;
8 
9 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.io.OutputStream;
14 import java.util.UUID;
15 
27 public class Peripherique extends Thread
28 {
32  private static final String TAG = "Peripherique";
33  public final static int CODE_CONNEXION = 0;
34  public final static int CODE_RECEPTION = 1;
35  public final static int CODE_EMISSION = 2;
36  public final static int CODE_DECONNEXION = 3;
40  private String nom;
41  private String adresse;
42  private Handler handler = null;
43  private BluetoothDevice device = null;
44  private BluetoothSocket socket = null;
45  private InputStream receiveStream = null;
46  private OutputStream sendStream = null;
47  private TReception treception = null;
48 
54  public Peripherique(BluetoothDevice device, Handler handler)
55  {
56  if (device != null)
57  {
58  this.device = device;
59  this.nom = device.getName();
60  this.adresse = device.getAddress();
61  this.handler = handler;
62  }
63  else
64  {
65  this.device = device;
66  this.nom = "Aucun";
67  this.adresse = "";
68  this.handler = handler;
69  }
70 
71  creerSocket();
72  }
73 
77  private void creerSocket()
78  {
79  try
80  {
81  if(device != null)
82  {
83  socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
84  receiveStream = socket.getInputStream();
85  sendStream = socket.getOutputStream();
86  }
87  }
88  catch (IOException e)
89  {
90  Log.d(TAG, "Erreur createRfcommSocketToServiceRecord()");
91  e.printStackTrace();
92  socket = null;
93  }
94  if (socket != null)
95  {
96  treception = new TReception(handler);
97  }
98  }
99 
104  public String getNom()
105  {
106  return nom;
107  }
108 
113  public String getAdresse()
114  {
115  return adresse;
116  }
117 
122  public boolean estConnecte()
123  {
124  return socket.isConnected();
125  }
126 
131  public void setNom(String nom)
132  {
133  this.nom = nom;
134  }
135 
140  public String toString()
141  {
142  return "\nNom : " + nom + "\nAdresse : " + adresse;
143  }
144 
149  public void envoyer(final String data)
150  {
151  if (socket == null)
152  return;
153 
154  new Thread()
155  {
156  @Override
157  public void run()
158  {
159  try
160  {
161  if (socket.isConnected())
162  {
163  sendStream.write(data.getBytes());
164  sendStream.flush();
165  Message msg = Message.obtain();
166  msg.what = CODE_EMISSION;
167  msg.obj = data;
168  handler.sendMessage(msg);
169  }
170  }
171  catch (IOException e)
172  {
173  Log.d(TAG, "Erreur write()");
174  e.printStackTrace();
175  }
176  }
177  }.start();
178  }
179 
183  public void connecter()
184  {
185  new Thread()
186  {
187  @Override
188  public void run()
189  {
190  if (connecterSocket())
191  {
192  return;
193  }
194  // sinon reconnexion
195  creerSocket();
196  connecterSocket();
197 
198  }
199  }.start();
200  }
201 
206  private boolean connecterSocket()
207  {
208  try
209  {
210  if(socket == null)
211  return false;
212  if (!socket.isConnected())
213  {
214  socket.connect();
215  Message msg = Message.obtain();
216  msg.what = CODE_CONNEXION;
217  handler.sendMessage(msg);
218 
219  treception.start();
220  return true;
221  }
222  }
223  catch (IOException e)
224  {
225  Log.d(TAG, "Erreur connect()");
226  e.printStackTrace();
227  }
228  return false;
229  }
230 
235  public boolean deconnecter()
236  {
237  try
238  {
239  treception.arreter();
240 
241  socket.close();
242  Message msg = Message.obtain();
243  msg.what = CODE_DECONNEXION;
244  handler.sendMessage(msg);
245 
246  return true;
247  }
248  catch (IOException e)
249  {
250  Log.d(TAG, "Erreur close()");
251  e.printStackTrace();
252  return false;
253  }
254  }
255 
260  private class TReception extends Thread
261  {
262  Handler handlerUI;
263  private boolean fini;
264 
265  TReception(Handler h)
266  {
267  handlerUI = h;
268  fini = false;
269  }
270 
271  @Override
272  public void run()
273  {
274  Log.d(TAG, "TReception run() start");
275  BufferedReader reception = new BufferedReader(new InputStreamReader(receiveStream));
276  while (!fini)
277  {
278  try
279  {
280  String trame = "";
281  if (reception.ready())
282  {
283  trame = reception.readLine();
284  }
285  if (trame.length() > 0)
286  {
287  Log.d(TAG, "run() trame : " + trame);
288  Message msg = Message.obtain();
289  msg.what = Peripherique.CODE_RECEPTION;
290  msg.obj = trame;
291  handlerUI.sendMessage(msg);
292  }
293  }
294  catch (IOException e)
295  {
296  Log.d(TAG, "Erreur read()");
297  e.printStackTrace();
298  }
299 
300  try
301  {
302  Thread.sleep(250);
303  }
304  catch (InterruptedException e)
305  {
306  e.printStackTrace();
307  }
308  }
309  Log.d(TAG, "TReception run() stop");
310  }
311 
312  public void arreter()
313  {
314  if (fini == false)
315  {
316  fini = true;
317  }
318  try
319  {
320  Thread.sleep(250);
321  }
322  catch (InterruptedException e)
323  {
324  e.printStackTrace();
325  }
326  }
327  }
328 }
boolean deconnecter()
Méthode perméttant de se déconnecter du périphérique.
String getNom()
Méthode pour obtenir le nom du périphérique.
void creerSocket()
Méthode de création du socket bluetooth.
String toString()
Méthode perméttant de renvoyer le périphérique en un String.
void envoyer(final String data)
Méthode perméttant d'envoyer une trame à l'aide du Thread.
void setNom(String nom)
Méthode perméttant de set le nom du périphérique.
boolean estConnecte()
Méthode perméttant de savoir si on est connecter.
void connecter()
Méthode perméttant de se connecter à un périphérique.
Classe permettant de gérer les périphériques.
boolean connecterSocket()
Méthode perméttant de connecter le socket.
String getAdresse()
Méthode pour obtenir l'adresse du périphérique.
Peripherique(BluetoothDevice device, Handler handler)
Constructeur de la classe Périphérique.
Déclaration de la classe TReception.