Skip to content

Commit

Permalink
Add broadcast profile id
Browse files Browse the repository at this point in the history
- Add broadcast profile id
- Add support for broadcast api

CRs-fixed: 2856233
Change-Id: If229688ecd0ad4c2ca3d058b4ebf69b19dede6b2
  • Loading branch information
pramod kotreshappa authored and MocaRafee committed Jun 20, 2021
1 parent d5b58f6 commit 0479a67
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
79 changes: 79 additions & 0 deletions core/java/android/bluetooth/BluetoothAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,22 @@ public boolean setActiveDevice(@NonNull BluetoothDevice device,
return false;
}

/** @hide */
@RequiresPermission(Manifest.permission.BLUETOOTH)
public boolean isBroadcastActive() {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.isBroadcastActive();
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
}
return false;
}

/**
* Connects all enabled and supported bluetooth profiles between the local and remote device.
* Connection is asynchronous and you should listen to each profile's broadcast intent
Expand Down Expand Up @@ -2935,6 +2951,8 @@ public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener
return true;
} else if (profile == BluetoothProfile.BC_PROFILE) {
return getBCProfile(context, listener);
} else if (profile == BluetoothProfile.BROADCAST) {
return getBroadcastProfile(context, listener);
} else if (profile == BluetoothProfile.HEARING_AID) {
if (isHearingAidProfileSupported()) {
BluetoothHearingAid hearingAid = new BluetoothHearingAid(context, listener);
Expand Down Expand Up @@ -3025,12 +3043,73 @@ public void closeProfileProxy(int profile, BluetoothProfile proxy) {
case BluetoothProfile.BC_PROFILE:
closeBCProfile(proxy);
break;
case BluetoothProfile.BROADCAST:
closeBroadcastProfile(proxy);
break;
case BluetoothProfile.HEARING_AID:
BluetoothHearingAid hearingAid = (BluetoothHearingAid) proxy;
hearingAid.close();
}
}

private boolean getBroadcastProfile(Context context,
BluetoothProfile.ServiceListener listener) {
boolean ret = true;
Class<?> broadcastClass = null;
Constructor bcastConstructor = null;
Object broadcastObj = null;
try {
broadcastClass = Class.forName("android.bluetooth.BluetoothBroadcast");
} catch (ClassNotFoundException ex) {
Log.e(TAG, "no BluetoothBroadcast: exists");
}
if (broadcastClass != null) {
try {
bcastConstructor =
broadcastClass.getDeclaredConstructor(new Class[]{Context.class,
BluetoothProfile.ServiceListener.class});
} catch (NoSuchMethodException ex) {
Log.e(TAG, "bcastConstructor: NoSuchMethodException: gdm" + ex);
}
}
if (bcastConstructor != null) {
try {
broadcastObj = bcastConstructor.newInstance(context, listener);
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException ex) {
ex.printStackTrace();
}
}
if (broadcastObj == null) {
return false;
}
return true;
}

private void closeBroadcastProfile(BluetoothProfile proxy) {
Class<?> broadcastClass = null;
Method broadcastClose = null;
try {
broadcastClass = Class.forName("android.bluetooth.BluetootBroadcast");
} catch (ClassNotFoundException ex) {
Log.e(TAG, "no BluetoothBroadcast: exists");
}
if (broadcastClass != null) {
try {
broadcastClose = broadcastClass.getDeclaredMethod("close", null);
} catch (NoSuchMethodException e) {
Log.e(TAG, "no Broadcast:close method exists");
}
if (broadcastClose != null) {
try {
broadcastClose.invoke(proxy, null);
} catch(IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
}
}
}

private final IBluetoothManagerCallback mManagerCallback =
new IBluetoothManagerCallback.Stub() {
public void onBluetoothServiceUp(IBluetooth bluetoothService) {
Expand Down
10 changes: 9 additions & 1 deletion core/java/android/bluetooth/BluetoothProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,19 @@ public interface BluetoothProfile {
*/
public static final int CC_SERVER = 26;

/**
* Broadcast
* @hide
*/
public static final int BROADCAST = 27;

/**
* Max profile ID. This value should be updated whenever a new profile is added to match
* the largest value assigned to a profile.
*
* @hide
*/
int MAX_PROFILE_ID = 26;
int MAX_PROFILE_ID = 27;

/**
* Default priority for devices that we try to auto-connect to and
Expand Down Expand Up @@ -428,6 +434,8 @@ static String getProfileName(int profile) {
return "OPP";
case HEARING_AID:
return "HEARING_AID";
case BROADCAST:
return "BROADCAST";
default:
return "UNKNOWN_PROFILE";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ public void onServiceDisconnected(int profile) {

@VisibleForTesting
public boolean isA2dpOrHearingAidConnected() {
return isA2dpConnected() || isHearingAidConnected();
return isA2dpConnected() || isHearingAidConnected() ||
isBroadcastActive();
}

@VisibleForTesting
Expand Down Expand Up @@ -256,5 +257,11 @@ private boolean isHearingAidConnected() {
}
return hearingAid.getConnectedDevices().size() > 0;
}

private boolean isBroadcastActive() {
boolean ret = false;
ret = mAdapter.isBroadcastActive();
return ret;
}
};
}

0 comments on commit 0479a67

Please sign in to comment.