2011-03-22 10 views
12

Actualmente estoy trabajando en una aplicación para Android .. tengo que notificar al usuario cada vez que el bluetooth del dispositivo se apaga mientras la aplicación está funcionando .. Actualmente Cómo notificar al dispositivo remoto tat BT es apagado? Gracias de antemanonotificación si el Bluetooth está desactivado en la aplicación para Android

+0

Off topic comentario ya que no hay sistema de PM aquí: Por favor, dejar de añadir ruido a los mensajes que edita. Está agregando palabras inútiles y audaces a las publicaciones y no es algo bueno. Tus ediciones son bienvenidas, solo sin este ruido inútil e irritante. –

Respuesta

20

Registro BroadcastReceiver con la acción intención BluetoothAdapter.ACTION_STATE_CHANGED y mover su código notifiyng en onReceive método. No se olvide de comprobar si el nuevo estado es OFF

if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) { 
    if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
     == BluetoothAdapter.STATE_OFF) 
     // Bluetooth was disconnected 
} 
+0

Gracias una tonelada ... Está funcionando bien ... – Hussain

7

Si desea detectar cuando el usuario está desconectando su Bluetooth, y más tarde, detectar cuando el usuario tiene su DESCONECTADO Bluetooth, usted debe hacer los siguientes pasos:

1) Obtener el BluetoothAdapter usuario:

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();  

2) Crear y configurar el receptor, con un código como este:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     String action = intent.getAction(); 

     // It means the user has changed his bluetooth state. 
     if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 

      if (btAdapter.getState() == BluetoothAdapter.STATE_TURNING_OFF) { 
       // The user bluetooth is turning off yet, but it is not disabled yet. 
       return; 
      } 

      if (btAdapter.getState() == BluetoothAdapter.STATE_OFF) { 
       // The user bluetooth is already disabled. 
       return; 
      } 

     } 
    } 
};  

3) Registre su BroadcastReceiver en su actividad:

this.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));  
Cuestiones relacionadas