Io-Trucks  0.2
BTS SNIR LaSalle Avignon 2020
Communication.java
Aller à la documentation de ce fichier.
1 package com.lasalle.io_trucks;
2 
3 import android.app.Activity;
4 import android.bluetooth.BluetoothAdapter;
5 import android.bluetooth.BluetoothDevice;
6 import android.content.BroadcastReceiver;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.util.Log;
10 import android.widget.Toast;
11 
12 import java.util.Set;
13 
25 public class Communication
26 {
30  private static final String TAG = "Communication";
34  private BroadcastReceiver receiverEtatBluetooth;
35  private BroadcastReceiver receiverScan;
36  private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
37  private Set<BluetoothDevice> listeAppareilConnus;
38 
43  public void demanderActivationBluetooth(Context contextAcceuil)
44  {
45  if (bluetoothAdapter == null)
46  {
47  Log.i(TAG,"demanderActivationBluetooth() bluetoothAdapter = null");
48  Toast.makeText(contextAcceuil, R.string.str_bluetoot_inexistant, Toast.LENGTH_SHORT).show();
49  }
50  else if (!bluetoothAdapter.isEnabled())
51  {
52  Log.i(TAG,"demanderActivationBluetooth() bluetooth désactivé");
53  Toast.makeText(contextAcceuil, R.string.str_bluetooth_eteint, Toast.LENGTH_SHORT).show();
54  Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
55  ((Activity) contextAcceuil).startActivityForResult(enableBtIntent,1);
56  }
57  else
58  {
59  Log.i(TAG,"demanderActivationBluetooth() bluetooth activé");
60  Toast.makeText(contextAcceuil, R.string.str_bluetooth_allumer, Toast.LENGTH_SHORT).show();
61  }
62  }
63 
68  public BroadcastReceiver ecouterEtatBluetooth()
69  {
70  receiverEtatBluetooth = new BroadcastReceiver()
71  {
72  @Override
73  public void onReceive(Context context, Intent intent)
74  {
75  final String action = intent.getAction();
76  Log.i(TAG,"onReceive() " + action);
77  if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED))
78  {
79  final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
80  switch (state)
81  {
82  case BluetoothAdapter.STATE_OFF:
83  Log.i(TAG,"onReceive() Bluetooth désactivé !");
84  Toast.makeText(context, R.string.str_bluetooth_eteint, Toast.LENGTH_SHORT).show();
85  break;
86  case BluetoothAdapter.STATE_TURNING_OFF:
87  Toast.makeText(context, R.string.str_bluetooth_eteint_en_cours, Toast.LENGTH_LONG).show();
88  break;
89  case BluetoothAdapter.STATE_ON:
90  Log.i(TAG,"onReceive() Bluetooth activé !");
91  Toast.makeText(context, R.string.str_bluetooth_allumer, Toast.LENGTH_SHORT).show();
92  break;
93  case BluetoothAdapter.STATE_TURNING_ON:
94  Log.i(TAG,"onReceive() Bluetooth s'active !");
95  Toast.makeText(context, R.string.str_bluetooth_allumer_en_cours, Toast.LENGTH_LONG).show();
96  break;
97  }
98  }
99  }
100  };
101 
102  return receiverEtatBluetooth;
103  }
104 
109  public void rechercherAppareilConnu(Context contextAcceuil)
110  {
111  listeAppareilConnus = bluetoothAdapter.getBondedDevices();
112  for(BluetoothDevice blueDevice : listeAppareilConnus)
113  {
114  Toast.makeText(contextAcceuil, "Appareil " + blueDevice.getName(), Toast.LENGTH_SHORT).show();
115  }
116  }
117 
123  public BluetoothDevice recupererAppareilBluetooth(String nomAppareil)
124  {
125  listeAppareilConnus = bluetoothAdapter.getBondedDevices();
126  for(BluetoothDevice blueDevice : listeAppareilConnus)
127  {
128  if(blueDevice.getName().equals(nomAppareil))
129  {
130  Log.d(TAG, "recupererAppareilBluetooth() io-trucks trouvé : " + blueDevice.getName() + " (" + blueDevice.getAddress() + ")");
131  return blueDevice;
132  }
133  }
134  return null;
135  }
136 
141  public void unregisterBluetooth(Context contextAcceuil)
142  {
143  if (bluetoothAdapter != null)
144  {
145  bluetoothAdapter.cancelDiscovery();
146  contextAcceuil.unregisterReceiver(receiverEtatBluetooth);
147  }
148  }
149 }
void rechercherAppareilConnu(Context contextAcceuil)
Méthode de recherche des appareils qui ont déjà était appairer.
Set< BluetoothDevice > listeAppareilConnus
BroadcastReceiver ecouterEtatBluetooth()
Vérifie les modification d&#39;état du bluetooth.
BluetoothDevice recupererAppareilBluetooth(String nomAppareil)
Méthode qui retourne l&#39;appareil Bluetooth io-trucks.
Classe de Communication et de connexion bluetooth.
void unregisterBluetooth(Context contextAcceuil)
Méthode pour unregister les receiver à la destruction de l&#39;application.
void demanderActivationBluetooth(Context contextAcceuil)
Vérifie si le bluetooth est disponible et activé, sinon demande l&#39;autorisation de l&#39;activé ...