Bonjour,
Je susi en train de développer une application Android qui utilise le Bluetooth, et j'utilise un BroadcastReceiver pour la recherche de peripherique Bluetooth,
J'aimerais pouvoir transmettre des données entre mon broadcastReceiver et la classe dans lequel je l'appel, en particulier je créer l'Arraylist dans le broadcast et y ajoute tous les appareils trouvés, puis j'aimerais envoyé cette list a la fin de la recherche Bluetooth à la class qui la créer.
La classe qui crée le broadcast
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class BroadcastRechercheBluetooth extends BroadcastReceiver { protected Activity context; protected InteractionActivity UInterface; public ArrayList<BluetoothDevice> devicesBT; public ArrayList<String> nomDevicesBT; public BroadcastRechercheBluetooth(Activity context) { this.devicesBT = new ArrayList<BluetoothDevice>(); this.nomDevicesBT = new ArrayList<String>(); this.UInterface = new InteractionActivity(context); } public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) UInterface.toast("Decouverte des peripherique en cours"); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); this.devicesBT.add(device); this.nomDevicesBT.add(device.getName()); UInterface.toast(device.getName()+" "+device.getAddress()); } if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) UInterface.toast("Decouverte des peripherique termine"); } public ArrayList<BluetoothDevice> getListDevicesBT() { return devicesBT; } public ArrayList<String> getListNomDevicesBT() { return nomDevicesBT; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 public void rechercheDevices() { recherchePeripheriqueBT = new BroadcastRechercheBluetooth(context); if(blueAdapter.isEnabled() && !blueAdapter.isDiscovering()){ blueAdapter.startDiscovery(); this.context.registerReceiver(recherchePeripheriqueBT, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED)); this.context.registerReceiver(recherchePeripheriqueBT, new IntentFilter(BluetoothDevice.ACTION_FOUND)); this.context.registerReceiver(recherchePeripheriqueBT, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)); } }
Partager