Skip to content

Commit

Permalink
Add check to enable A2dpSinkProfile
Browse files Browse the repository at this point in the history
If another app disables the a2dp sink profile, enable it

Bug: 64480524
Test: App works even after disabling A2dpSinkProfile
Change-Id: I9b55473df10db2eedb0814bf6c0471c237d192bf
  • Loading branch information
daverim committed Sep 1, 2017
1 parent 1233a98 commit fef32ea
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
import android.util.Log;
import android.view.KeyEvent;

import com.google.android.things.bluetooth.BluetoothProfileManager;
import com.google.android.things.bluetooth.BluetoothProfileManager.ServiceListener;
import com.google.android.things.contrib.driver.button.Button;
import com.google.android.things.contrib.driver.button.ButtonInputDriver;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

Expand Down Expand Up @@ -70,6 +74,7 @@ public class A2dpSinkActivity extends Activity {
private ButtonInputDriver mDisconnectAllButtonDriver;

private TextToSpeech mTtsEngine;
private boolean mRegisteredReceivers;

/**
* Handle an intent that is broadcast by the Bluetooth adapter whenever it changes its
Expand Down Expand Up @@ -152,24 +157,17 @@ protected void onCreate(Bundle savedInstanceState) {
return;
}

// We use Text-to-Speech to indicate status change to the user
initTts();

registerReceiver(mAdapterStateChangeReceiver, new IntentFilter(
BluetoothAdapter.ACTION_STATE_CHANGED));
registerReceiver(mSinkProfileStateChangeReceiver, new IntentFilter(
A2dpSinkHelper.ACTION_CONNECTION_STATE_CHANGED));
registerReceiver(mSinkProfilePlaybackChangeReceiver, new IntentFilter(
A2dpSinkHelper.ACTION_PLAYING_STATE_CHANGED));

if (mBluetoothAdapter.isEnabled()) {
Log.d(TAG, "Bluetooth Adapter is already enabled.");
initA2DPSink();
} else {
Log.d(TAG, "Bluetooth adapter not enabled. Enabling.");
mBluetoothAdapter.enable();
}
ServiceListener serviceListener = new ServiceListener() {
@Override
public void onServiceConnected(BluetoothProfileManager bluetoothProfileManager) {
setupBTProfiles(bluetoothProfileManager);
}

@Override
public void onServiceDisconnected() {
}
};
new BluetoothProfileManager(this, serviceListener);
}

@Override
Expand Down Expand Up @@ -199,9 +197,11 @@ protected void onDestroy() {
if (mDisconnectAllButtonDriver != null) mDisconnectAllButtonDriver.close();
} catch (IOException e) { /* close quietly */}

unregisterReceiver(mAdapterStateChangeReceiver);
unregisterReceiver(mSinkProfileStateChangeReceiver);
unregisterReceiver(mSinkProfilePlaybackChangeReceiver);
if (mRegisteredReceivers) {
unregisterReceiver(mAdapterStateChangeReceiver);
unregisterReceiver(mSinkProfileStateChangeReceiver);
unregisterReceiver(mSinkProfilePlaybackChangeReceiver);
}

if (mA2DPSinkProxy != null) {
mBluetoothAdapter.closeProfileProxy(A2dpSinkHelper.A2DP_SINK_PROFILE,
Expand All @@ -217,6 +217,42 @@ protected void onDestroy() {
// without having to initialize it.
}

private void setupBTProfiles(BluetoothProfileManager bluetoothProfileManager) {
List<Integer> enabledProfiles = bluetoothProfileManager.getEnabledProfiles();
if (!enabledProfiles.contains(A2dpSinkHelper.A2DP_SINK_PROFILE)) {
Log.d(TAG, "Enabling A2dp sink mode.");
List<Integer> toDisable = Arrays.asList(BluetoothProfile.A2DP);
List<Integer> toEnable = Arrays.asList(
A2dpSinkHelper.A2DP_SINK_PROFILE,
A2dpSinkHelper.AVRCP_CONTROLLER_PROFILE);
bluetoothProfileManager.enableAndDisableProfiles(toEnable, toDisable);
} else {
Log.d(TAG, "A2dp sink profile is enabled.");
}
onProfilesConfigured();
}

private void onProfilesConfigured() {
// We use Text-to-Speech to indicate status change to the user
initTts();

registerReceiver(mAdapterStateChangeReceiver, new IntentFilter(
BluetoothAdapter.ACTION_STATE_CHANGED));
registerReceiver(mSinkProfileStateChangeReceiver, new IntentFilter(
A2dpSinkHelper.ACTION_CONNECTION_STATE_CHANGED));
registerReceiver(mSinkProfilePlaybackChangeReceiver, new IntentFilter(
A2dpSinkHelper.ACTION_PLAYING_STATE_CHANGED));
mRegisteredReceivers = true;

if (mBluetoothAdapter.isEnabled()) {
Log.d(TAG, "Bluetooth Adapter is already enabled.");
initA2DPSink();
} else {
Log.d(TAG, "Bluetooth adapter not enabled. Enabling.");
mBluetoothAdapter.enable();
}
}

/**
* Initiate the A2DP sink.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public final class A2dpSinkHelper {
*/
public static final int A2DP_SINK_PROFILE = 11;

/**
* Profile number for AVRCP_CONTROLLER profile.
*/
public static final int AVRCP_CONTROLLER_PROFILE = 12;

/**
* Intent used to broadcast the change in connection state of the A2DP Sink
* profile.
Expand Down

0 comments on commit fef32ea

Please sign in to comment.