Ekawa  1.0
BTS SNIR LaSalle Avignon 2021
Peripherique.java
Aller à la documentation de ce fichier.
1 package com.example.ekawa;
2 
3 import android.bluetooth.BluetoothDevice;
4 import android.bluetooth.BluetoothSocket;
5 
6 import android.os.Handler;
7 import android.os.Message;
8 
9 import android.util.Log;
10 
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 
15 import java.util.UUID;
16 
29 public class Peripherique
30 {
31  private static final String TAG = "Peripherique";
32 
33  public final static int CODE_CONNEXION = 0;
34  public final static int CODE_RECEPTION = 1;
35  public final static int CODE_DECONNEXION = 2;
36 
37  private String nom;
38  private String adresse;
39  private BluetoothDevice peripherique = null;
40 
41  private BluetoothSocket socket = null;
42  private InputStream receiveStream = null;
43  private OutputStream sendStream = null;
44 
45  private Reception reception = null;
46 
47  private Handler handler;
48 
53  public Peripherique(BluetoothDevice peripherique, Handler handler)
54  {
55  Log.d(TAG,"Peripherique()");
56  if (peripherique != null)
57  {
58  this.peripherique = peripherique;
59  this.nom = peripherique.getName();
60  this.adresse = peripherique.getAddress();
61  this.handler = handler;
62 
63  try
64  {
65  socket = peripherique.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
66  receiveStream = socket.getInputStream();
67  sendStream = socket.getOutputStream();
68  }
69  catch(IOException e)
70  {
71  e.printStackTrace();
72  socket = null;
73  Log.d(TAG,"Erreur création socket !");
74  }
75  }
76  else
77  {
78  this.peripherique = peripherique;
79  this.nom = "Aucun";
80  this.adresse = "";
81  this.handler = null;
82  }
83 
84  if(socket != null)
85  {
86  reception = new Reception(this, handler);
87  Log.v(TAG, "Périphérique " + obtenirNom() + " prêt");
88  }
89  }
90 
96  public void changerHandler(Handler handler)
97  {
98  this.handler = handler;
99  }
100 
105  public void connecter()
106  {
107  if(peripherique == null)
108  return;
109 
110  new Thread()
111  {
112  @Override public void run()
113  {
114  try
115  {
116  if(socket != null)
117  {
118  if(!socket.isConnected())
119  {
120  Log.d(TAG, "Socket connexion à " + obtenirNom());
121  socket.connect();
122  }
123 
124  if(socket.isConnected())
125  {
126  Log.d(TAG, "Socket connecté à " + obtenirNom());
127  if(handler != null)
128  {
129  Message msg = Message.obtain();
130  msg.what = CODE_CONNEXION;
131  handler.sendMessage(msg);
132  }
133  }
134 
135  if (reception != null)
136  {
137  reception.start();
138  Log.d(TAG, "Démarrage thread réception");
139  }
140  }
141  }
142  catch (IOException e)
143  {
144  Log.d(TAG,"Erreur connexion socket !");
145  e.printStackTrace();
146  }
147  }
148  }.start();
149  }
150 
155  public void deconnecter()
156  {
157  if(peripherique == null)
158  return;
159 
160  new Thread()
161  {
162  @Override public void run()
163  {
164  try
165  {
166  if (reception != null)
167  {
168  Log.d(TAG,"Arrêt thread réception");
169  reception.arreter();
170  receiveStream.close();
171  sendStream.close();
172  if (socket != null)
173  {
174  Log.d(TAG, "Socket déconnexion de " + obtenirNom());
175  socket.close();
176 
177  if (!socket.isConnected())
178  {
179  Log.d(TAG, "Socket déconnecté de " + obtenirNom());
180  if (handler != null)
181  {
182  Message msg = Message.obtain();
183  msg.what = CODE_DECONNEXION;
184  handler.sendMessage(msg);
185  }
186  }
187  }
188  }
189  }
190  catch (IOException e)
191  {
192  Log.d(TAG,"Erreur fermeture socket !");
193  e.printStackTrace();
194  }
195  }
196  }.start();
197  }
198 
205  public boolean envoyer(String trame)
206  {
207  final String trameEnvoyee = trame;
208 
209  if(socket == null)
210  return false;
211 
212  if(!socket.isConnected())
213  return false;
214 
215  new Thread()
216  {
217  @Override public void run()
218  {
219  try
220  {
221  if(socket.isConnected())
222  {
223  sendStream.write(trameEnvoyee.getBytes());
224  sendStream.flush();
225  Log.d(TAG, "Envoyer : " + trameEnvoyee);
226  }
227  }
228  catch (IOException e)
229  {
230  Log.d(TAG,"Erreur écriture socket !");
231  e.printStackTrace();
232  }
233  }
234  }.start();
235 
236  return true;
237  }
238 
244  public boolean estConnecte()
245  {
246  if(socket != null)
247  return socket.isConnected();
248  return false;
249  }
250 
256  public String obtenirNom()
257  {
258  return nom;
259  }
260 
266  public InputStream obtenirFluxReception()
267  {
268  return receiveStream;
269  }
270 }
Permet le dialogue avec le périphérique Bluetooth de la cafetière.
static final int CODE_RECEPTION
Le code de réception.
InputStream obtenirFluxReception()
Méthode qui renvoie le flux de données entrant.
String adresse
L'adresse MAC du périphérique.
InputStream receiveStream
!< Le socket de connection
Peripherique(BluetoothDevice peripherique, Handler handler)
Constructeur de la classe Peripherique.
static final int CODE_DECONNEXION
Le code de déconnexion.
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.
void arreter()
Méthode qui permet d&#39;arrêter la réception de trame.
Definition: Reception.java:91
Permet la réception des trames du périphérique Bluetooth de la cafetière.
Definition: Reception.java:23
void changerHandler(Handler handler)
Méthode qui permet de changer le gestionnaire des messages.
Handler handler
La gestionnaire des messages.
OutputStream sendStream
Le flux de données sortant.
boolean envoyer(String trame)
Méthode qui permet d&#39;envoyer des trames à la cafetière.
boolean estConnecte()
Méthode qui renvoie si le périphérique est connecté ou non.
Reception reception
La réception des données.
String obtenirNom()
Méthode qui renvoie le nom du périphérique.
String nom
Le nom du périphérique.
BluetoothDevice peripherique
Le périphérique.
static final int CODE_CONNEXION
Le code de connexion.
static final String TAG
TAG pour les logs.