1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
/**
* Ecouteur d'évènements qui va écouter toutes les messages du context de
* l'activité afin d'agir en fonction des filtres ajoutés dans onResume
*
* @see onResume
*/
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// détection d'un nouveau périphérique et ajout à la liste des
// périphériques détectés
BluetoothDevice bluetoothDevice = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// if (bluetoothDevice.getBondState() !=
// BluetoothDevice.BOND_BONDED) {
bluetoothArrayAdapter.add(bluetoothDevice);
if (BuildConfig.DEBUG)
Log.i(Constants.LOG_BLUETOOTH_ACTIVITY, "Device = " + bluetoothDevice.getName() + "\n"
+ bluetoothDevice.getAddress());
// }
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
// Fin de la découverte des périphériques bluetooth
// changement d'état de l'interface graphique
startSearchItem.setVisible(true);
stopSearchItem.setVisible(false);
pbSearch.setVisibility(ProgressBarTextView.GONE);
Toast.makeText(context, R.string.sToastEndScan,
Toast.LENGTH_LONG).show();
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action))
{
final BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
//device associé
}else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDING){
//device en cours d'association
//affichage d'un message dans mon interface
}
}
}
}; |
Partager