Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/04/2016, 04:59
phyronx
 
Fecha de Ingreso: mayo-2009
Mensajes: 232
Antigüedad: 15 años
Puntos: 3
Mostrar dispositivos bluetooth

Buenas!

Estoy intentando crear una app sencilla donde activar el bluetooth, y buscar los dispositivos que encuentre.

He seguido mas o menos los pasos de developer.android, y todo correcto hasta el momento que quiero mostrar los dispositivos.

No me da ningún error y la app no falla en ni ningún momento, pero no se ven las listas.
Si pongo un toast justo después del if donde debo ir recibiendo los dispositivos, no se ejecta, si lo pongo justo antes si.

Código Java:
Ver original
  1. buscarBl.setOnClickListener(new View.OnClickListener() {
  2.             public void onClick(View v) {
  3.  
  4.                 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  5.  
  6.                 if (pairedDevices.size() >= 0) {
  7.                     Toast.makeText(getBaseContext(), "Your answer is correct!" , Toast.LENGTH_SHORT ).show();
  8.                     // Loop through paired devices
  9.                     for (BluetoothDevice device : pairedDevices) {
  10.                         // Add the name and address to an array adapter to show in a ListView
  11.                         listaBlue.add(device.getName() + "\n" + device.getAddress());
  12.                     }
  13.  
  14.                     listaDispositius = (ListView)findViewById(R.id.llistaDisp);
  15.                     ArrayAdapter<String> adaptador = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1, listaBlue);
  16.                     listaDispositius.setAdapter(adaptador);
  17.                 }
  18.  
  19.  
  20.                 // Create a BroadcastReceiver for ACTION_FOUND
  21.                 final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  22.                     public void onReceive(Context context, Intent intent) {
  23.                         String action = intent.getAction();
  24.                         // When discovery finds a device
  25.                         if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  26.                             // Get the BluetoothDevice object from the Intent
  27.                             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  28.                             // Add the name and address to an array adapter to show in a ListView
  29.                             listaBlue.add(device.getName() + "\n" + device.getAddress());
  30.                         }
  31.                         listaDispositius = (ListView)findViewById(R.id.llistaDisp);
  32.                         ArrayAdapter<String> adaptador = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1, listaBlue);
  33.                         listaDispositius.setAdapter(adaptador);
  34.                     }
  35.                 };
  36. // Register the BroadcastReceiver
  37.                 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  38.                 registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
  39.  
  40.             }
  41.         });