GrOOm  0.2
BTS SNIR LaSalle Avignon 2020
Communication.java
Aller à la documentation de ce fichier.
1 package com.example.groom;
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 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.io.OutputStream;
13 import java.util.UUID;
14 
25 public class Communication extends Thread
26 {
30  private static final String TAG = "Communication";
31  public final static int CODE_CONNEXION = 0;
32  public final static int CODE_RECEPTION = 1;
33  public final static int CODE_DECONNEXION = 2;
34 
38  private BluetoothDevice device;
39  private String nom;
40  private String adresse;
41  private Handler handler;
42  private BluetoothSocket socket = null;
43  private InputStream receiveStream = null;
44  private OutputStream sendStream = null;
46 
53  public Communication(BluetoothDevice device, Handler handler)
54  {
55  this.handler = handler;
56  if (device != null)
57  {
58  this.device = device;
59  this.nom = device.getName();
60  this.adresse = device.getAddress();
61 
62  try
63  {
64  socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
65  receiveStream = socket.getInputStream();
66  sendStream = socket.getOutputStream();
67  }
68  catch (IOException e)
69  {
70  e.printStackTrace();
71  }
72  }
73  else
74  {
75  this.device = device;
76  this.nom = "Aucun";
77  this.adresse = "";
78  }
79 
80  if(socket != null)
81  tReception = new TReception(handler);
82  Log.v(TAG, "Communication() : Nom = " + getNom());
83  }
84 
91  public String getNom()
92  {
93  return nom;
94  }
95 
102  public String getAdresse()
103  {
104  return adresse;
105  }
106 
113  public BluetoothDevice getDevice()
114  {
115  return device;
116  }
117 
123  public void connecter()
124  {
125  new Thread()
126  {
127  @Override public void run()
128  {
129  try
130  {
131  socket.connect();
132 
133  Message msg = Message.obtain();
134  msg.what = CODE_CONNEXION;
135  handler.sendMessage(msg);
136 
137  if(tReception != null)
138  tReception.start();
139  Log.v(TAG, "connecter() : Nom = " + getNom());
140  }
141  catch (IOException e)
142  {
143  Log.d(TAG, "Erreur connect()");
144  e.printStackTrace();
145  }
146  }
147  }.start();
148  }
149 
156  public boolean deconnecter()
157  {
158  try
159  {
160  tReception.arreter();
161 
162  socket.close();
163 
164  Message msg = Message.obtain();
165  msg.what = CODE_DECONNEXION;
166  handler.sendMessage(msg);
167 
168  return true;
169  }
170  catch (IOException e)
171  {
172  Log.d(TAG, "Erreur close()");
173  e.printStackTrace();
174  return false;
175  }
176  }
177 
184  public void envoyer(String data)
185  {
186  final String trame = data;
187 
188  if(socket == null)
189  return;
190 
191  new Thread()
192  {
193  @Override public void run()
194  {
195  try
196  {
197  if(socket.isConnected())
198  {
199  sendStream.write(trame.getBytes());
200  sendStream.flush();
201  Log.d(TAG, "envoyer() trame : " + trame);
202  }
203  }
204  catch (IOException e)
205  {
206  Log.d(TAG, "Erreur write()");
207  e.printStackTrace();
208  }
209  }
210  }.start();
211  }
212 
217  public void attendre(int millis)
218  {
219  try
220  {
221  Thread.sleep(millis);
222  }
223  catch (InterruptedException e)
224  {
225  e.printStackTrace();
226  }
227  }
228 
233  private class TReception extends Thread
234  {
238  Handler handlerUI;
239  private boolean fini;
240 
247  TReception(Handler h)
248  {
249  handlerUI = h;
250  fini = false;
251  }
252 
258  @Override
259  public void run()
260  {
261  BufferedReader reception = new BufferedReader(new InputStreamReader(receiveStream));
262  while (!fini)
263  {
264  try
265  {
266  String trame = "";
267  if (reception.ready())
268  {
269  trame = reception.readLine();
270  }
271  if (trame.length() > 0)
272  {
273  Log.d(TAG, "run() trame : " + trame);
274  Message msg = Message.obtain();
275  msg.what = Communication.CODE_RECEPTION;
276  msg.obj = trame;
277  handlerUI.sendMessage(msg);
278  }
279  }
280  catch (IOException e)
281  {
282  Log.d(TAG, "Erreur read()");
283  e.printStackTrace();
284  }
285  try
286  {
287  Thread.sleep(250);
288  }
289  catch (InterruptedException e)
290  {
291  e.printStackTrace();
292  }
293  }
294  }
295 
301  public void arreter()
302  {
303  if (!fini)
304  {
305  fini = true;
306  }
307  try
308  {
309  Thread.sleep(250);
310  }
311  catch (InterruptedException e)
312  {
313  e.printStackTrace();
314  }
315  }
316  }
317 }
318 
319 
boolean fini
true si la réception est fini, false sinon
String getNom()
Accesseur get du nom.
BluetoothDevice getDevice()
Accesseur get de l'appareil.
void attendre(int millis)
Méthode pour attendre avant de passer à la prochaine ligne d'exécution.
boolean deconnecter()
Méthode pour se déconnecter de l'appareil.
String getAdresse()
Accesseur get de l'adresse.
static final String TAG
TAG pour les logs.
void envoyer(String data)
Méthode pour envoyer une trame.
Handler handler
L'objet handler.
static final int CODE_CONNEXION
Code de connexion.
InputStream receiveStream
L'objet receiveStream.
BluetoothDevice device
L'objet device.
OutputStream sendStream
L'objet sendStream.
static final int CODE_DECONNEXION
Code de déconnexion.
void run()
Méthode qui lance le thread.
Communication(BluetoothDevice device, Handler handler)
Constructeur de la classe Communication.
BluetoothSocket socket
L'objet socket.
TReception tReception
L'objet tReception.
String adresse
L'adresse.
static final int CODE_RECEPTION
Code de réception d'une trame.
Déclaration de la classe Communication.
void connecter()
Méthode pour se connecter à l'appareil.
Déclaration de la classe TReception.
void arreter()
Méthode pour arrêter la réception.