Ekawa  1.0
BTS SNIR LaSalle Avignon 2021
Communication.java
Aller à la documentation de ce fichier.
1 package com.example.ekawa;
2 
3 import android.bluetooth.BluetoothAdapter;
4 import android.bluetooth.BluetoothDevice;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.IntentFilter;
9 import android.os.Build;
10 import android.os.Handler;
11 import android.os.Message;
12 import android.util.Log;
13 import android.widget.Toast;
14 
15 import androidx.appcompat.app.AppCompatActivity;
16 
17 import java.util.Set;
18 
31 public class Communication
32 {
36  private static final String TAG = "Communication";
37  private final static String EKAWA = "ekawa-";
38  public final static String NOM_CAFETIERE_NON_CONNECTEE = "Aucune";
39 
43  private boolean activee = false;
44  private boolean connectee = false;
45  private BluetoothAdapter bluetooth = null;
46  private Peripherique peripherique = null;
47  private Context context;
48  private Cafetiere cafetiere;
49 
53  private final Handler handler = new Handler()
54  {
55  public void handleMessage(Message msg)
56  {
57  super.handleMessage(msg);
58  switch (msg.what)
59  {
61  connectee = true;
62  cafetiere.actualiserIHM();
64  break;
66  cafetiere.changerEtats(recevoirTrame((String) msg.obj));
67  break;
69  connectee = false;
70  cafetiere.actualiserIHM();
71  break;
72  }
73  }
74  };
75 
81  private final BroadcastReceiver detectionChangementEtatBluetooth()
82  {
83  BroadcastReceiver receiverEtatBluetooth = new BroadcastReceiver()
84  {
85  @Override
86  public void onReceive(Context context, Intent intent)
87  {
88  final String action = intent.getAction();
89  Log.d(TAG,"[detectionChangementEtatBluetooth] action : " + action);
90  if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED))
91  {
92  final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
93  switch (state)
94  {
95  case BluetoothAdapter.STATE_OFF:
96  Log.d(TAG,"[detectionChangementEtatBluetooth] bluetooth désactivé !");
97  activee = false;
98  cafetiere.actualiserIHM();
99  break;
100  case BluetoothAdapter.STATE_TURNING_OFF:
101  Log.d(TAG,"[detectionChangementEtatBluetooth] bluetooth en cours de désactivation !");
102  break;
103  case BluetoothAdapter.STATE_ON:
104  Log.d(TAG,"[detectionChangementEtatBluetooth] bluetooth activé !");
105  activee = true;
106  cafetiere.actualiserIHM();
108  break;
109  case BluetoothAdapter.STATE_TURNING_ON:
110  Log.d(TAG,"[detectionChangementEtatBluetooth] bluetooth en cours d'activation !");
111  break;
112  default:
113  Log.d(TAG,"[detectionChangementEtatBluetooth] etat : " + state);
114  break;
115  }
116  }
117  else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED))
118  {
119  Log.d(TAG,"[detectionChangementEtatBluetooth] bluetooth déconnecté !");
120  connectee = false;
121  cafetiere.remettreAZero();
122  }
123  else if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
124  {
125  final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
126  if(state == BluetoothDevice.BOND_BONDED)
127  {
128  Log.d(TAG,"[detectionChangementEtatBluetooth] cafetière appairée !");
129  peripherique.connecter();
130  if(peripherique.estConnecte())
131  connectee = true;
132  }
133  }
134  }
135  };
136 
137  return receiverEtatBluetooth;
138  }
139 
145  private final BroadcastReceiver detectionPeripherique()
146  {
147  BroadcastReceiver receiverDetectionBluetooth = new BroadcastReceiver()
148  {
149  public void onReceive(Context context, Intent intent)
150  {
151  boolean appaire = false;
152 
153  String action = intent.getAction();
154  Log.d(TAG, "[receiverDetectionBluetooth] action : " + action);
155 
156  if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
157  {
158  Log.d(TAG, "[receiverDetectionBluetooth] découverte démarrée");
159  }
160  else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
161  {
162  Log.d(TAG, "[receiverDetectionBluetooth] découverte terminée");
163  }
164  else if (BluetoothDevice.ACTION_FOUND.equals(action))
165  {
166  BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
167  Log.d(TAG,"[receiverDetectionBluetooth] device détecté : " + device.getName() + " [" + device.getAddress() + "]");
168  if (device.getName() != null && device.getName().startsWith(EKAWA))
169  {
170  Toast.makeText(context, "Trouvé : " + device.getName(), Toast.LENGTH_LONG).show();
171  if (device.getBondState() != BluetoothDevice.BOND_BONDED)
172  {
173  Log.d(TAG,"[receiverDetectionBluetooth] cafetière détectée mais non appairée !");
174  if(Build.VERSION.SDK_INT >= 19)
175  {
176  appaire = device.createBond(); // appairage
177  }
178  else
179  {
180  Toast.makeText(context, "Veuillez associer votre cafetière Ekawa au bluetooth manuellement.", Toast.LENGTH_LONG).show();
181  }
182  }
183  if (device.getBondState() == BluetoothDevice.BOND_BONDED || appaire)
184  {
185  Log.d(TAG, "[receiverDetectionBluetooth] cafetière appairée : " + device.getName() + " [" + device.getAddress() + "]");
186  creerPeripherique(device);
187  bluetooth.cancelDiscovery();
188  }
189  }
190  }
191  }
192  };
193 
194  return receiverDetectionBluetooth;
195  };
196 
203  public Communication(AppCompatActivity activity, Cafetiere cafetiere)
204  {
205  Log.d(TAG,"Communication()");
206  this.context = activity.getApplicationContext();
207  this.cafetiere = cafetiere;
208  bluetooth = BluetoothAdapter.getDefaultAdapter();
209  if(bluetooth != null)
210  {
212  activer();
213  }
214  else
215  {
216  Log.d(TAG,"Pas de bluetooth ?");
217  }
218  }
219 
224  public void activer()
225  {
226  if (!bluetooth.isEnabled())
227  bluetooth.enable();
228  activee = true;
230  }
231 
236  public void connecter()
237  {
238  if(bluetooth.isEnabled())
239  {
241  }
242  }
243 
248  public void desactiver()
249  {
250  if(bluetooth.isEnabled())
251  {
252  deconnecter();
253  bluetooth.disable();
254  activee = false;
255  }
256  }
257 
262  public void deconnecter()
263  {
264  if(bluetooth.isEnabled())
265  peripherique.deconnecter();
266  connectee = false;
267  }
268 
274  public boolean estConnectee()
275  {
276  return connectee;
277  }
278 
284  public boolean estActivee()
285  {
286  return activee;
287  }
288 
293  private void chercherCafetiere()
294  {
295  Set<BluetoothDevice> devices;
296 
297  if(peripherique != null)
298  peripherique = null;
299 
300  devices = bluetooth.getBondedDevices();
301  for(BluetoothDevice blueDevice : devices)
302  {
303  Log.d(TAG,"[chercherCafetiere] device : " + blueDevice.getName() + " [" + blueDevice.getAddress() + "]");
304  if(blueDevice.getName().startsWith(EKAWA))
305  {
306  Log.d(TAG,"[chercherCafetiere] cafetière : " + blueDevice.getName() + " [" + blueDevice.getAddress() + "]");
307  creerPeripherique(blueDevice);
308  peripherique.connecter();
309  if(peripherique.estConnecte())
310  connectee = true;
311  Toast.makeText(context, "Cafetière : " + peripherique.obtenirNom(), Toast.LENGTH_LONG).show();
312  break;
313  }
314  }
315 
316  if(peripherique == null)
317  {
318  Log.d(TAG,"[chercherCafetiere] cafetière ekawa non trouvée !");
320  }
321  }
322 
328  public void envoyerTrame(String trame)
329  {
330  Log.d(TAG, "envoyerTrame()");
331  if(peripherique != null)
332  peripherique.envoyer(trame);
333  }
334 
341  public String recevoirTrame(String trame)
342  {
343  Log.d(TAG," Recu : " + trame);
344  if(verifierTrame(trame))
345  return trame;
346  return null;
347  }
348 
355  private boolean verifierTrame(String trame)
356  {
357  if(trame.startsWith(Protocole.DEBUT_TRAME))
358  return true;
359  return false;
360  }
361 
367  {
368  IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
369  filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
370  filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
371  context.registerReceiver(detectionChangementEtatBluetooth(), filter);
372  }
373 
378  private void demarrerRecherche()
379  {
380  IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
381  filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
382  filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
383  context.registerReceiver(detectionPeripherique(), filter);
384  if(bluetooth.isDiscovering())
385  bluetooth.cancelDiscovery();
386  boolean etatDemarrageDecouverte = bluetooth.startDiscovery();
387  Log.d(TAG,"[chercherCafetiere] démarrage découverte bluetooth " + etatDemarrageDecouverte);
388  }
389 
394  private void creerPeripherique(BluetoothDevice peripherique)
395  {
396  Log.d(TAG,"[definirPeripherique] nom : " + peripherique.getName());
397  if(this.peripherique != null)
398  {
399  this.peripherique.deconnecter();
400  this.peripherique = null;
401  }
402  this.peripherique = new Peripherique(peripherique, handler);
403  }
404 
410  public String obtenirNomPeripherique()
411  {
412  if(peripherique != null)
413  return peripherique.obtenirNom();
414  return null;
415  }
416 }
Permet le dialogue avec le périphérique Bluetooth de la cafetière.
void remettreAZero()
Méthode qui permet de remettre les arguments à zéro.
Definition: Cafetiere.java:762
static final int CODE_RECEPTION
Le code de réception.
static final String NOM_CAFETIERE_NON_CONNECTEE
Le nom de la cafetière si non-connectée.
Context context
Le contexte de l&#39;application.
static String fabriquerTrameTestAlive()
Méthode qui permet de créer une trame pour tester la connection avec la machine.
Definition: Protocole.java:137
boolean estConnectee()
Méthode qui retourne l&#39;état de la connection avec la cafetière.
Permet la communication Bluetooth avec la cafetière.
String obtenirNomPeripherique()
Méthode qui retourne le nom du périphérique.
Définit les caractéristiques du protocole EKAWA.
Definition: Protocole.java:17
static final String DEBUT_TRAME
Le début de la trame.
Definition: Protocole.java:25
static final int CODE_DECONNEXION
Le code de déconnexion.
static final String TAG
TAG pour les logs.
void deconnecter()
Méthode qui permet de déconnecter le bluetooth de la cafetière.
void connecter()
Méthode qui permet de connecter le bluetooth à la cafetière.
Communication(AppCompatActivity activity, Cafetiere cafetiere)
Constructeur de la classe Communication.
void activer()
Méthode qui permet d&#39;allumer le bluetooth.
boolean activee
Indique si le bluetooth est activée.
void connecter()
Méthode qui permet de connecter le bluetooth à la cafetière.
final BroadcastReceiver detectionPeripherique()
Détecteur de périphériques.
Déclaration de la classe principale de l&#39;application.
Definition: Cafetiere.java:19
void desactiver()
Méthode qui permet d&#39;éteindre le bluetooth.
static final String EKAWA
Le nom du périphérique bluetooth.
boolean envoyer(String trame)
Méthode qui permet d&#39;envoyer des trames à la cafetière.
void deconnecter()
Méthode qui permet de déconnecter le bluetooth de la cafetière.
Peripherique peripherique
Le périphérique bluetooth distant.
void changerEtats(String trame)
Méthode qui permet d&#39;actualiser les états de la cafetière, la tasse, le bac et le niveau d&#39;eau + les ...
Definition: Cafetiere.java:522
void actualiserIHM()
Méthode qui permet d&#39;actualiser l&#39;IHM.
Definition: Cafetiere.java:779
void chercherCafetiere()
Méthode qui permet de chercher da cafetière dans les périphérique bluetooth apparié ...
boolean verifierTrame(String trame)
Méthode qui permet de vérifier la trame reçue.
boolean estConnecte()
Méthode qui renvoie si le périphérique est connecté ou non.
final BroadcastReceiver detectionChangementEtatBluetooth()
Détecteur de changement d&#39;état du bluetooth.
boolean estActivee()
Méthode qui retourne si le bluetooth est activé ou non.
BluetoothAdapter bluetooth
L&#39;adaptateur Bluetooth de la tablette.
String recevoirTrame(String trame)
Méthode qui permet de recevoir des trames de la cafetière.
void installerDetectionEtatBluetooth()
Méthode qui installe la détection des changements d&#39;états bluetooth.
String obtenirNom()
Méthode qui renvoie le nom du périphérique.
boolean connectee
Indique l&#39;état de la connexion avec la cafetière.
void creerPeripherique(BluetoothDevice peripherique)
Méthode qui permet de créer le périphérique.
void envoyerTrame(String trame)
Méthode qui permet d&#39;envoyer des trames à la cafetière.
static final int CODE_CONNEXION
Le code de connexion.
final Handler handler
Traitement des messages en provenance des Threads.
Cafetiere cafetiere
Relation avec l&#39;objet principal Cafetiere.
void demarrerRecherche()
Méthode qui permet de lancer la recherche de périphérique non apparié