1

In my app, i want the user can listen to the audio from Bluetooth, headset and speaker so I provide one spinner and If Bluetooth is connected then the spinner add Bluetooth, and headset also connected then spinner add headset too.

But when I start service in concrete to register Bluetooth and headset broadcast receiver and run the app I get the headset plug out the action from the headset broadcast receiver even if the device is not connected to Bluetooth and headset device or plug out from them.

Oncreate

 startService(Intent(this, HeadsetService::class.java))
        if (AppUtils.isBluetoothSupported(packageManager)) {
            startService(Intent(this, BluetoothService::class.java))
        }

Ondestroy

stopService(Intent(this, HeadsetService::class.java))
        stopService(Intent(this, BluetoothService::class.java))

HeadsetService

public class HeadsetService extends Service {
    private HeadsetUtility mHeadsetUtility;

    @Nullable
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {
        super.onCreate();
        initHeadsetBroadcastReceiver();
    }

    public void onDestroy() {
        stopHeadsetBroadcastReceiver();
        super.onDestroy();
    }

    private void initHeadsetBroadcastReceiver() {
        if (this.mHeadsetUtility == null) {
            this.mHeadsetUtility = new HeadsetUtility();
            Log.e("videocall","headsetservice");
            registerReceiver(this.mHeadsetUtility, HeadsetUtility.getIntentFilter());
        }
    }

    private void stopHeadsetBroadcastReceiver() {
        HeadsetUtility headsetUtility = this.mHeadsetUtility;
        if (headsetUtility != null) {
            unregisterReceiver(headsetUtility);
        }
    }
}

HeadsetBroadcast

public final class HeadsetUtility extends BroadcastReceiver {
    private static final String TAG = "HTEventReceiver";
    private static boolean isHeadsetPluggedIn = false;

    public static IntentFilter getIntentFilter() {
        return new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    }

    public static boolean isIsHeadsetPluggedIn() {
        return isHeadsetPluggedIn;
    }

    public static void setIsHeadsetPluggedIn(boolean z) {
        isHeadsetPluggedIn = z;
    }

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == null) {
        } else if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int intExtra = intent.getIntExtra("state", -1);
            switch (intExtra) {
                case 0:
                    headsetIsPluggedOut();
                    return;
                case 1:
                    headsetIsPluggedIn();
                    return;
                default:
                    setIsHeadsetPluggedIn(false);
                    return;
            }
        }
    }

    private void headsetIsPluggedIn() {
        setIsHeadsetPluggedIn(true);
        AppUtils.setmAudioSource(AppUtils.AudioSource.HEADSET);
        Log.e("videocall","headsetservice1");
        RxBus.publish(new RxEvent.EventAudioBroadcast(new Event_AudioSource(AppUtils.AudioSource.HEADSET,1)));
    }

    private void headsetIsPluggedOut() {
        setIsHeadsetPluggedIn(false);
        if (BluetoothUtility.isIsBluetoothConnected()) {
            AppUtils.setmAudioSource(AppUtils.AudioSource.BLUETOOTH);
        } else {
            AppUtils.setmAudioSource(AppUtils.AudioSource.SPEAKER);
        }
        Log.e("videocall","headsetservice2");
        RxBus.publish(new RxEvent.EventAudioBroadcast(new Event_AudioSource(AppUtils.AudioSource.HEADSET,2)));
    }
}

Manifest

 <service android:name=".utils.BluetoothService" />
        <service android:name=".utils.HeadsetService" />

In manifest I didn't register broadcast, I have register broadcast in service with intent filter

So, My issue is when the app is started service is started and broadcast is registered and I get headset plug out the action from broadcast and call headsetIsPluggedOut(), even if device is not connected to Bluetooth and headset device or plug out from them

Please suggest what to do

mayur.pancholi
  • 446
  • 1
  • 8
  • 18

1 Answers1

1

Please check this post. There is an example for getting the Headset connected event. Hope this might help.

Eishon
  • 1,274
  • 1
  • 9
  • 17