¿Cómo recuperar la información del dispositivo Bluetooth con el selector de dispositivos Bluetooth de Android?

Aquí está el código de prueba que estoy usando:

public class IOConnectDirect extends Activity { private static final String TAG = "IOConnectDirect"; private static final int REQCODE_BLUETOOTH_RESULT = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate"); setTitle(getTitle() + "--" + TAG); Intent intentBluetooth = new Intent(); intentBluetooth.setAction("android.bluetooth.devicepicker.action.LAUNCH"); //android.bluetooth.devicepicker.action.DEVICE_SELECTED not working . startActivityForResult(intentBluetooth, REQCODE_BLUETOOTH_RESULT); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.i(TAG, "onActivityResult(" + requestCode +"," + resultCode + ")"); switch (requestCode) { case REQCODE_BLUETOOTH_RESULT: Log.i(TAG, "requestCode = REQCODE_BLUETOOTH_RESULT"); if(resultCode == RESULT_OK) { Log.i(TAG, "RESULT_OK"); // Retrieve the Info Bundle extras = data.getExtras(); if(extras != null) { Log.i(TAG, "Bundle ok"); } } else { Log.i(TAG, "!RESULT_OK = FAILED(" + resultCode + ")"); Toast.makeText(this, "Failed(" + resultCode +")", Toast.LENGTH_SHORT).show(); } break; default: Log.i(TAG, "requestCode = ????"); break; } } 

}

Aquí está la salida Logcat:

 I/IOConnectDirect(14956): onActivityResult(0,0) I/IOConnectDirect(14956): requestCode = REQCODE_BLUETOOTH_RESULT I/IOConnectDirect(14956): !RESULT_OK = FAILED(0) 

El código funciona (necesitas activar Bluetooth primero), simplemente no puedo hacer que haga lo que quiero, que es recuperar el nombre del dispositivo Bluetooth y la dirección que seleccioné de la actividad.

Nota :

  • No estoy tratando de conectar sólo quiero la información de dispositivo de selección.
  • Estoy familiarizado con otros métodos para hacer esto como en el Android Bluetooth Chat Sample

ACTUALIZAR

Termino usando un BroadcastReceiver

 public class IOConnectDirect extends Activity { private static final String TAG = "IOConnectDirect"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate"); BluetoothConnectActivityReceiver mBluetoothPickerReceiver = new BluetoothConnectActivityReceiver(); registerReceiver(mBluetoothPickerReceiver, new IntentFilter(BluetoothDevicePicker.ACTION_DEVICE_SELECTED)); startActivity(new Intent(BluetoothDevicePicker.ACTION_LAUNCH) .putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false) .putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, BluetoothDevicePicker.FILTER_TYPE_ALL) .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)); } public class BluetoothConnectActivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(BluetoothDevicePicker.ACTION_DEVICE_SELECTED.equals(intent.getAction())) { context.unregisterReceiver(this); BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show(); } } } 

Pruebe este código:

 private final BroadcastReceiver mBluetoothPickerReceiver = new BluetoothConnectActivityReceiver(this); void connectToService(String defaultAdapter) { if (defaultAdapter == null) { registerReceiver(mBluetoothPickerReceiver, new IntentFilter(BluetoothDevicePicker.ACTION_DEVICE_SELECTED)); startActivity(new Intent(BluetoothDevicePicker.ACTION_LAUNCH) .putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false) .putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, BluetoothDevicePicker.FILTER_TYPE_ALL) .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)); } else { mCardroid.getCardroidService().connectTo(defaultAdapter); } } public class BluetoothConnectActivityReceiver extends BroadcastReceiver { private BluetoothConnectActivity bluetoothConnectActivity; public BluetoothConnectActivityReceiver(BluetoothConnectActivity bluetoothConnectActivity) { this.bluetoothConnectActivity = bluetoothConnectActivity; } @Override public void onReceive(Context context, Intent intent) { if(BluetoothDevicePicker.ACTION_DEVICE_SELECTED.equals(intent.getAction())) { context.unregisterReceiver(this); BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); bluetoothConnectActivity.connectToService(device.getAddress()); } } } 

La referencia del código completo es:

http://code.google.com/p/carbot/source/browse/trunk/src/net/cardroid/?r=8

 Try this code: private ArrayAdapter<String> bondedAdapter = null,newScanedAdapter = null; listViewPairedDevices.setAdapter(bondedAdapter);//oncreate listViewScanedDevices.setAdapter(newScanedAdapter);//oncreate public void doDiscovery(){ setProgressBarIndeterminateVisibility(true); setTitle("Scanning.."); textViewScanedDevices.setVisibility(View.VISIBLE); if(!BluetoothDemoActivity.bluetoothAdapter.isDiscovering()){ BluetoothDemoActivity.bluetoothAdapter.cancelDiscovery(); } BluetoothDemoActivity.bluetoothAdapter.startDiscovery(); }//doDiscovery() ends IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(myBroadcastReceiver, intentFilter); intentFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(myBroadcastReceiver, intentFilter); Set<BluetoothDevice> bondedSet = BluetoothDemoActivity.bluetoothAdapter.getBondedDevices(); Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet); int count = 0; if(bondedSet.size() > 0){ for(BluetoothDevice device : bondedSet){ textViewPairedDevice.setVisibility(View.VISIBLE); bondedAdapter.add(device.getName()+"\n"+device.getAddress()); Log.v(BluetoothDemoActivity.LOG, " count = "+count++); } }else{ bondedAdapter.add("No Devices"); } private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)){ buttonScanScanDevices.setVisibility(View.GONE); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if(device.getBondState()!= BluetoothDevice.BOND_BONDED){ newScanedAdapter.add(device.getName()+"\n"+device.getAddress()); } }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ setProgressBarIndeterminateVisibility(false); setTitle("Select Device"); if(newScanedAdapter.getCount() == 0){ newScanedAdapter.add("no new device"); } }else if(intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")){ System.out.println("Pairing request came"); pair(); } } };//BroadcastReceiverends 
  • ¿Es posible obtener una lista de impresoras Bluetooth en Android?
  • Cómo asociar un dispositivo Bluetooth mediante programación a Android
  • Bluetooth LE aplicación, la forma de construir cross-plaftorm (iOS-Android)?
  • Android Bluetooth Bibliotecas que hacen la vida fácil?
  • Grabación de audio desde un dispositivo de audio Bluetooth en Android
  • Android Bluetooth: java.io.IOException: falla en el descubrimiento del servicio
  • Señal fatal 11 SIGSEGV al cerrar BluetoothSocket en Android 4.2.2 y 4.3
  • Detección de cambios de estado realizados en BluetoothAdapter?
  • Periférico Bluetooth ADVERTISE_FAILED_DATA_TOO_LARGE
  • ¿Solicitud de sincronización de Bluetooth en la barra de notificación?
  • StartBluetoothSco () lanza la excepción de seguridad (BROADCAST_STICKY) en ICS
  • Interesting Posts
    FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.