1 package com.example.ekawa;
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;
15 import androidx.appcompat.app.AppCompatActivity;
36 private static final String
TAG =
"Communication";
37 private final static String
EKAWA =
"ekawa-";
53 private final Handler
handler =
new Handler()
55 public void handleMessage(Message msg)
57 super.handleMessage(msg);
83 BroadcastReceiver receiverEtatBluetooth =
new BroadcastReceiver()
86 public void onReceive(Context context, Intent intent)
88 final String action = intent.getAction();
89 Log.d(TAG,
"[detectionChangementEtatBluetooth] action : " + action);
90 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED))
92 final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
95 case BluetoothAdapter.STATE_OFF:
96 Log.d(TAG,
"[detectionChangementEtatBluetooth] bluetooth désactivé !");
100 case BluetoothAdapter.STATE_TURNING_OFF:
101 Log.d(TAG,
"[detectionChangementEtatBluetooth] bluetooth en cours de désactivation !");
103 case BluetoothAdapter.STATE_ON:
104 Log.d(TAG,
"[detectionChangementEtatBluetooth] bluetooth activé !");
109 case BluetoothAdapter.STATE_TURNING_ON:
110 Log.d(TAG,
"[detectionChangementEtatBluetooth] bluetooth en cours d'activation !");
113 Log.d(TAG,
"[detectionChangementEtatBluetooth] etat : " + state);
117 else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED))
119 Log.d(TAG,
"[detectionChangementEtatBluetooth] bluetooth déconnecté !");
123 else if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
125 final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
126 if(state == BluetoothDevice.BOND_BONDED)
128 Log.d(TAG,
"[detectionChangementEtatBluetooth] cafetière appairée !");
137 return receiverEtatBluetooth;
147 BroadcastReceiver receiverDetectionBluetooth =
new BroadcastReceiver()
149 public void onReceive(Context context, Intent intent)
151 boolean appaire =
false;
153 String action = intent.getAction();
154 Log.d(TAG,
"[receiverDetectionBluetooth] action : " + action);
156 if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
158 Log.d(TAG,
"[receiverDetectionBluetooth] découverte démarrée");
160 else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
162 Log.d(TAG,
"[receiverDetectionBluetooth] découverte terminée");
164 else if (BluetoothDevice.ACTION_FOUND.equals(action))
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))
170 Toast.makeText(context,
"Trouvé : " + device.getName(), Toast.LENGTH_LONG).show();
171 if (device.getBondState() != BluetoothDevice.BOND_BONDED)
173 Log.d(TAG,
"[receiverDetectionBluetooth] cafetière détectée mais non appairée !");
174 if(Build.VERSION.SDK_INT >= 19)
176 appaire = device.createBond();
180 Toast.makeText(context,
"Veuillez associer votre cafetière Ekawa au bluetooth manuellement.", Toast.LENGTH_LONG).show();
183 if (device.getBondState() == BluetoothDevice.BOND_BONDED || appaire)
185 Log.d(TAG,
"[receiverDetectionBluetooth] cafetière appairée : " + device.getName() +
" [" + device.getAddress() +
"]");
187 bluetooth.cancelDiscovery();
194 return receiverDetectionBluetooth;
205 Log.d(TAG,
"Communication()");
206 this.context = activity.getApplicationContext();
208 bluetooth = BluetoothAdapter.getDefaultAdapter();
209 if(bluetooth != null)
216 Log.d(TAG,
"Pas de bluetooth ?");
226 if (!bluetooth.isEnabled())
238 if(bluetooth.isEnabled())
250 if(bluetooth.isEnabled())
264 if(bluetooth.isEnabled())
295 Set<BluetoothDevice> devices;
297 if(peripherique != null)
300 devices = bluetooth.getBondedDevices();
301 for(BluetoothDevice blueDevice : devices)
303 Log.d(TAG,
"[chercherCafetiere] device : " + blueDevice.getName() +
" [" + blueDevice.getAddress() +
"]");
304 if(blueDevice.getName().startsWith(EKAWA))
306 Log.d(TAG,
"[chercherCafetiere] cafetière : " + blueDevice.getName() +
" [" + blueDevice.getAddress() +
"]");
311 Toast.makeText(context,
"Cafetière : " + peripherique.
obtenirNom(), Toast.LENGTH_LONG).show();
316 if(peripherique == null)
318 Log.d(TAG,
"[chercherCafetiere] cafetière ekawa non trouvée !");
330 Log.d(TAG,
"envoyerTrame()");
331 if(peripherique != null)
343 Log.d(TAG,
" Recu : " + trame);
368 IntentFilter filter =
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
369 filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
370 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
380 IntentFilter filter =
new IntentFilter(BluetoothDevice.ACTION_FOUND);
381 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
382 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
384 if(bluetooth.isDiscovering())
385 bluetooth.cancelDiscovery();
386 boolean etatDemarrageDecouverte = bluetooth.startDiscovery();
387 Log.d(TAG,
"[chercherCafetiere] démarrage découverte bluetooth " + etatDemarrageDecouverte);
396 Log.d(TAG,
"[definirPeripherique] nom : " + peripherique.getName());
397 if(this.peripherique != null)
399 this.peripherique.deconnecter();
400 this.peripherique = null;
402 this.peripherique =
new Peripherique(peripherique, handler);
412 if(peripherique != null)
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.
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'application.
static String fabriquerTrameTestAlive()
Méthode qui permet de créer une trame pour tester la connection avec la machine.
boolean estConnectee()
Méthode qui retourne l'é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.
static final String DEBUT_TRAME
Le début de la trame.
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'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'application.
void desactiver()
Méthode qui permet d'éteindre le bluetooth.
static final String EKAWA
Le nom du périphérique bluetooth.
boolean envoyer(String trame)
Méthode qui permet d'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'actualiser les états de la cafetière, la tasse, le bac et le niveau d'eau + les ...
void actualiserIHM()
Méthode qui permet d'actualiser l'IHM.
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'état du bluetooth.
boolean estActivee()
Méthode qui retourne si le bluetooth est activé ou non.
BluetoothAdapter bluetooth
L'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'états bluetooth.
String obtenirNom()
Méthode qui renvoie le nom du périphérique.
boolean connectee
Indique l'é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'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'objet principal Cafetiere.
void demarrerRecherche()
Méthode qui permet de lancer la recherche de périphérique non apparié