Bonjour !
Je n'arrive pas à afficher les périphériques bluetooth dans une listview ; j'ai besoin d'aide svp.. Voici mon code :
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 public class Bluetooth extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bluetooth); ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(Bluetooth.this, android.R.layout.simple_list_item_1); final ListView Deviceslist = (ListView)findViewById(R.id.listView1); final Button scanb = (Button)findViewById(R.id.button); final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); scanb.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(Bluetooth.this, android.R.layout.simple_list_item_1); btArrayAdapter.clear(); bluetoothAdapter.startDiscovery(); Toast.makeText(Bluetooth.this, "Scanning Devices", Toast.LENGTH_LONG).show(); Deviceslist.setAdapter(btArrayAdapter); } }); registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(FoundReceiver); } private final BroadcastReceiver FoundReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(Bluetooth.this, android.R.layout.simple_list_item_1); if(BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); btArrayAdapter.add(device.getName() + "\n" + device.getAddress()); btArrayAdapter.notifyDataSetChanged(); } }}; }
Partager