¿Cómo puedo hacer una lista de todos los dispositivos bluetooth conectados en android?lista de dispositivos bluetooth conectados?
gracias!
¿Cómo puedo hacer una lista de todos los dispositivos bluetooth conectados en android?lista de dispositivos bluetooth conectados?
gracias!
Analice this class en línea.
Aquí encontrará cómo descubrir todos los dispositivos Bluetooth conectados (emparejados).
Bueno, aquí están los pasos:
En primer lugar, se inicia la intención de descubrir dispositivos
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
El registro de un reciver emisión para ello:
registerReceiver(mReceiver, filter);
Sobre la definición de mReceiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
arrayadapter.add(device.getName())//arrayadapter is of type ArrayAdapter<String>
lv.setAdapter(arrayadapter); //lv is the list view
arrayadapter.notifyDataSetChanged();
}
}
y la lista se rellena automáticamente en el nuevo descubrimiento de dispositivos.
Falta un}; al final del código. No puedo editarlo solo, ya que tiene que ser un cambio de 6 caracteres, y solo falta 2. –
El sistema Android no permite consultar todos los dispositivos conectados "actualmente". Sin embargo, puede consultar para dispositivos emparejados. Tendrá que usar un receptor de difusión para escuchar eventos ACTION_ACL_ {CONNECTED | DISCONNECTED} junto con el evento STATE_BONDED para actualizar sus estados de aplicación y rastrear lo que está actualmente conectado.
A partir del API 14 (helado), Android tiene un algunos nuevos métodos BluetoothAdapter incluyendo:
public int getProfileConnectionState (int profile)
donde el perfil es uno de HEALTH, HEADSET, A2DP
Comprobación de respuesta, si no es STATE_DISCONNECTED
sabes tienes una conexión en vivo
Aquí es ejemplo de código que funcione en cualquier dispositivo API:
BluetoothAdapter mAdapter;
/**
* Check if a headset type device is currently connected.
*
* Always returns false prior to API 14
*
* @return true if connected
*/
public boolean isVoiceConnected() {
boolean retval = false;
try {
Method method = mAdapter.getClass().getMethod("getProfileConnectionState", int.class);
// retval = mAdapter.getProfileConnectionState(android.bluetooth.BluetoothProfile.HEADSET) != android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
retval = (Integer)method.invoke(mAdapter, 1) != 0;
} catch (Exception exc) {
// nothing to do
}
return retval;
}
public void checkConnected()
{
// true == headset connected && connected headset is support hands free
int state = BluetoothAdapter.getDefaultAdapter().getProfileConnectionState(BluetoothProfile.HEADSET);
if (state != BluetoothProfile.STATE_CONNECTED)
return;
try
{
BluetoothAdapter.getDefaultAdapter().getProfileProxy(_context, serviceListener, BluetoothProfile.HEADSET);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private ServiceListener serviceListener = new ServiceListener()
{
@Override
public void onServiceDisconnected(int profile)
{
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
for (BluetoothDevice device : proxy.getConnectedDevices())
{
Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
+ BluetoothProfile.STATE_CONNECTED + ")");
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
}
};
¿Usted se ha la solución para esto. Estoy buscando lo mismo, por favor brinde respuesta. –