Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 99e87bb3 authored by pramod kotreshappa's avatar pramod kotreshappa Committed by Bruno Martins
Browse files

Add broadcast profile id

- Add broadcast profile id
- Add support for broadcast api

CRs-fixed: 2856233
Change-Id: If229688ecd0ad4c2ca3d058b4ebf69b19dede6b2
parent 69f29b96
Loading
Loading
Loading
Loading
+79 −0
Original line number Diff line number Diff line
@@ -1870,6 +1870,22 @@ public final class BluetoothAdapter {
        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
@@ -2935,6 +2951,8 @@ public final class BluetoothAdapter {
            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);
@@ -3025,12 +3043,73 @@ public final class BluetoothAdapter {
            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) {
+9 −1
Original line number Diff line number Diff line
@@ -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
@@ -428,6 +434,8 @@ public interface BluetoothProfile {
                return "OPP";
            case HEARING_AID:
                return "HEARING_AID";
            case BROADCAST:
                return "BROADCAST";
            default:
                return "UNKNOWN_PROFILE";
        }
+8 −1
Original line number Diff line number Diff line
@@ -198,7 +198,8 @@ class BluetoothAirplaneModeListener {

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

        @VisibleForTesting
@@ -256,5 +257,11 @@ class BluetoothAirplaneModeListener {
            }
            return hearingAid.getConnectedDevices().size() > 0;
        }

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