0

Tengo la siguiente función, pero cuando quiero correr la aplicación me sale el siguiente error:

error: incompatible types: BroadcastReceiver is not a functional interface

Además parece que no reconociera las funciones:

intent.getAction()
intent.getIntExtra()

No se que le puede estar faltando a la aplicacion. Gracias!

Private final BroadcastReceiver mBroadcastReceiver1  = (context, intent) -> {

    String action = intent.getAction();
    if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);

        switch (state) {
            case BluetoothAdapter.STATE_OFF:
                Log.d(TAG, "onReceive: STATE OFF");
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                Log.d(TAG, "mBroadcastReceiver1: STATE TURNING OFF");
                break;
            case BluetoothAdapter.STATE_ON:
                Log.d(TAG, "mBroadcastReceiver1: STATE ON");
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                Log.d(TAG, "mBroadcastReceiver1: STATE TURNING ON");
                break;
        }
    }
};

1 respuesta 1

0

Java solo sabe como convertir lambdas en interfaces, para usar BroadcastReceiver te recomiendo que uses una clase que herede de BR, si necesitas que el receiver se comunique con otra clase puedes usar una interfaz. Recuerda que debes llamar a Context.registerReceiver desde tu metodo onCreate u onResume y a Context.unregisterReceiver desde onPause u onDestroy

public class BluethoothReceiver extends BroadcastReceiver {

    final static String TAG="BluetoothReceiver";
    private OnBluetoothStateListener onBluetoothStateListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

            switch (state) {
                case BluetoothAdapter.STATE_OFF:
                    if(onBluetoothStateListener!=null) onBluetoothStateListener.onStateOff();
                    Log.d(TAG, "onReceive: STATE OFF");
                    
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    Log.d(TAG, "mBroadcastReceiver1: STATE TURNING OFF");
                    break;
                case BluetoothAdapter.STATE_ON:
                    Log.d(TAG, "mBroadcastReceiver1: STATE ON");
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    Log.d(TAG, "mBroadcastReceiver1: STATE TURNING ON");
                    break;
            }
        }
    }

    public void setOnBluetoothStateListener(OnBluetoothStateListener onBluetoothStateListener) {
        this.onBluetoothStateListener = onBluetoothStateListener;
    }

    public interface OnBluetoothStateListener{
        void onStateOff();
        void onStateTurningOff();
        void onStateOn();
        void onStateTurningOn();
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.