Projet Darts  0.2
BTS SNIR LaSalle Avignon 2020
Peripherique.java
Aller à la documentation de ce fichier.
1 package projet.lasalle84.darts;
2 
3 import android.bluetooth.BluetoothDevice;
4 import android.bluetooth.BluetoothSocket;
5 import android.os.Bundle;
6 import android.os.Handler;
7 import android.os.Message;
8 import android.util.Log;
9 
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import java.util.UUID;
14 
26 public class Peripherique extends Thread
27 {
31  private final static String TAG = "Peripherique";
32  public final static int CODE_CONNEXION = 0;
33  public final static int CODE_RECEPTION = 1;
34  public final static int CODE_DECONNEXION = 2;
35  public final static int CODE_ERREUR_ENVOYER = -1;
36  public final static int CODE_ERREUR_RECEVOIR = -2;
37  public final static int CODE_ERREUR_CONNECTER = -3;
38 
41  private BluetoothDevice device;
42  private String nom;
43  private String adresse;
44  private Handler handler;
45  private BluetoothSocket socket = null;
46  private InputStream receiveStream = null;
47  private OutputStream sendStream = null;
48  private TReception tReception;
49 
55  public Peripherique(BluetoothDevice device, Handler handler)
56  {
57  Log.d(TAG,"Peripherique() " + device.getName() + "[" + device.getAddress() + "]");
58  if(device != null)
59  {
60  this.device = device;
61  this.nom = device.getName();
62  this.adresse = device.getAddress();
63  this.handler = handler;
64  }
65  else
66  {
67  this.device = device;
68  this.nom = "Aucun";
69  this.adresse = "";
70  this.handler = handler;
71  }
72 
73  try
74  {
75  assert device != null;
76  socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
77  receiveStream = socket.getInputStream();
78  sendStream = socket.getOutputStream();
79  }
80  catch (IOException e)
81  {
82  e.printStackTrace();
83  socket = null;
84  }
85 
86  if(socket != null)
87  tReception = new TReception(this, handler, receiveStream);
88  }
89 
95  public String getNom()
96  {
97  return nom;
98  }
99 
105  public String getAdresse()
106  {
107  return adresse;
108  }
109 
114  public void connecter()
115  {
116  Log.d(TAG,"connecter() " + device.getName() + "[" + device.getAddress() + "]");
117  new Thread()
118  {
119  @Override public void run()
120  {
121  try
122  {
123  socket.connect();
124 
125  Message msg = Message.obtain();
126  Bundle b = new Bundle();
127  b.putString("nom", getNom());
128  b.putString("adresse", getAdresse());
129  b.putInt("etat", CODE_CONNEXION);
130  msg.setData(b);
131  handler.sendMessage(msg);
132 
133  // on démarre le thread de réception
134  tReception.start();
135  Log.d(TAG,"connexion reussie " + getNom());
136 
137  }
138  catch (IOException e)
139  {
140  e.printStackTrace();
141  Log.d(TAG,"erreur connexion " + getNom());
142  Message msg = Message.obtain();
143  Bundle b = new Bundle();
144  b.putString("nom", getNom());
145  b.putString("adresse", getAdresse());
146  b.putInt("etat", CODE_ERREUR_CONNECTER);
147  msg.setData(b);
148  handler.sendMessage(msg);
149  }
150  }
151  }.start();
152  }
153 
158  public boolean deconnecter()
159  {
160  Log.d(TAG,"deconnecter() " + device.getName() + "[" + device.getAddress() + "]");
161  try
162  {
163  tReception.arreter();
164 
165  socket.close();
166  return true;
167  }
168  catch (IOException e)
169  {
170  System.out.println("<Socket> error close");
171  e.printStackTrace();
172  return false;
173  }
174  }
175 
180  public void envoyer(final String data)
181  {
182  Log.d(TAG,"envoyer() " + device.getName() + "[" + device.getAddress() + "]");
183  if(socket == null)
184  {
185  Log.d(TAG,"pas d'envoi");
186  return;
187  }
188  new Thread()
189  {
190  @Override public void run()
191  {
192  try
193  {
194  if(socket.isConnected())
195  {
196  sendStream.write(data.getBytes());
197  sendStream.flush();
198  Log.d(TAG, "envoyer() trame envoyée : " + data);
199  }
200  }
201  catch (IOException e)
202  {
203  e.printStackTrace();
204  Log.d(TAG, "envoyer() Erreur socket write : " + getNom());
205  Message msg = Message.obtain();
206  Bundle b = new Bundle();
207  b.putString("nom", getNom());
208  b.putString("adresse", getAdresse());
209  b.putInt("etat", CODE_ERREUR_ENVOYER);
210  msg.setData(b);
211  handler.sendMessage(msg);
212  }
213  }
214  }.start();
215  }
216 }
BluetoothDevice device
Objet BluetoothDevice.
static final int CODE_RECEPTION
Code de Reception.
TReception tReception
Thread pour traiter les trames.
void connecter()
Méthode pour se connecter sur le perihperique en Bluetooth.
static final int CODE_ERREUR_ENVOYER
Code erreur lors de l&#39;envoi.
Peripherique(BluetoothDevice device, Handler handler)
Constructeur de la classe Peripherique.
Déclaration de la classe Peripherique.
static final int CODE_DECONNEXION
Code de Deconnexion.
String getNom()
Méthode qui retourne le nom du périphérique.
boolean deconnecter()
Méthode pour se deconnecter sur le perihperique en Bluetooth.
BluetoothSocket socket
Socket Bluetooth.
static final int CODE_ERREUR_RECEVOIR
Code erreur lors de la réception.
InputStream receiveStream
Input du Bluetooth.
String nom
Nom du peripherique bluetooth.
String getAdresse()
Méthode qui retourne l&#39;adresse du périphérique.
static final String TAG
TAG pour log.
static final int CODE_CONNEXION
Code de Connection.
void envoyer(final String data)
Méthode pour envoyer une trame en Bluetooth.
OutputStream sendStream
Output du BLuetooth.
Handler handler
Handler permet de traiter les trames.
static final int CODE_ERREUR_CONNECTER
Code erreur lors de la connexion.
String adresse
Adresse du peripherique bluetooth.