Bonjour,
j'ai une question en ce qui concerne le scan bluetooth.
J'ai une application qui doit récupérer les matériaux bluetooth environnent.
Pour cela j'ai une Activity qui lance le scan, et une class qui gère la récupération des données bluetooth etc :
Activity :
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 public class Main extends Activity { private BluetoothUtilities _bluetooth; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); _bluetooth = new BluetoothUtilities(this); if(!_bluetooth.isAvailable()) System.exit(0); if(!_bluetooth.isEnable()) _bluetooth.activer(); _bluetooth.startScan(); //while(_bluetooth.isDiscovering()); //((TextView) findViewById(R.id.TextViewResult)).append("FINISH \n"); } }
BlutoothClass :
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class BluetoothUtilities { private BluetoothAdapter _bluetoothAdapter; private Vector<String> _deviceDiscovered; private Activity _act; private final BroadcastReceiver _Receiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); _deviceDiscovered.add(device.getName()); ((TextView) _act.findViewById(R.id.TextViewResult)).append(_deviceDiscovered.lastElement()+"\n"); } } }; public BluetoothUtilities(Activity act) { _act = act; _deviceDiscovered = new Vector<String>(); _bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } public void startScan() { _bluetoothAdapter.startDiscovery(); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); _act.registerReceiver(_Receiver, filter); } public boolean isDiscovering() { return _bluetoothAdapter.isDiscovering(); } public void activer() { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); _act.startActivityForResult(enableBtIntent, 2); } public boolean isAvailable() { return !(_bluetoothAdapter == null); } public boolean isEnable() { return _bluetoothAdapter.isEnabled(); } }
Ici ce que je fais, c'est afficher dans mon TextView les périphériques détectes au fur et a mesure.
Mais ce que je voudrais, c'est attendre la fin du scan pour ensuite afficher le résultat.
J'ai donc rajouté les ligne commentées dans Activity, mais mon while(isDiscovering()) n'a pas l'air de fonctionner.
Je sais pas si je my prend de la bonne façons, mais il me faudrais une valeur de retour en ce qui concerne l'état du scan (ce que fait isDiscovering() il me semble).
Merci
Partager