1 package com.lasalle.io_trucks;
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;
30 private static final String TAG =
"Communication";
36 private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
45 if (bluetoothAdapter == null)
47 Log.i(TAG,
"demanderActivationBluetooth() bluetoothAdapter = null");
48 Toast.makeText(contextAcceuil, R.string.str_bluetoot_inexistant, Toast.LENGTH_SHORT).show();
50 else if (!bluetoothAdapter.isEnabled())
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);
59 Log.i(TAG,
"demanderActivationBluetooth() bluetooth activé");
60 Toast.makeText(contextAcceuil, R.string.str_bluetooth_allumer, Toast.LENGTH_SHORT).show();
70 receiverEtatBluetooth =
new BroadcastReceiver()
73 public void onReceive(Context context, Intent intent)
75 final String action = intent.getAction();
76 Log.i(TAG,
"onReceive() " + action);
77 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED))
79 final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
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();
86 case BluetoothAdapter.STATE_TURNING_OFF:
87 Toast.makeText(context, R.string.str_bluetooth_eteint_en_cours, Toast.LENGTH_LONG).show();
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();
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();
111 listeAppareilConnus = bluetoothAdapter.getBondedDevices();
112 for(BluetoothDevice blueDevice : listeAppareilConnus)
114 Toast.makeText(contextAcceuil,
"Appareil " + blueDevice.getName(), Toast.LENGTH_SHORT).show();
125 listeAppareilConnus = bluetoothAdapter.getBondedDevices();
126 for(BluetoothDevice blueDevice : listeAppareilConnus)
128 if(blueDevice.getName().equals(nomAppareil))
130 Log.d(TAG,
"recupererAppareilBluetooth() io-trucks trouvé : " + blueDevice.getName() +
" (" + blueDevice.getAddress() +
")");
143 if (bluetoothAdapter != null)
145 bluetoothAdapter.cancelDiscovery();
146 contextAcceuil.unregisterReceiver(receiverEtatBluetooth);
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'état du bluetooth.
BluetoothDevice recupererAppareilBluetooth(String nomAppareil)
Méthode qui retourne l'appareil Bluetooth io-trucks.
Classe de Communication et de connexion bluetooth.
BroadcastReceiver receiverEtatBluetooth
void unregisterBluetooth(Context contextAcceuil)
Méthode pour unregister les receiver à la destruction de l'application.
BroadcastReceiver receiverScan
void demanderActivationBluetooth(Context contextAcceuil)
Vérifie si le bluetooth est disponible et activé, sinon demande l'autorisation de l'activé ...