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

Commit 63bd0345 authored by Pulkit Bhuwalka's avatar Pulkit Bhuwalka Committed by android-build-merger
Browse files

Merge "Get Bluetooth Class of Device"

am: ae16fe73

Change-Id: Iea787a0c4c98a34119158cb50ea2cea343901181
parents 375d8ced ae16fe73
Loading
Loading
Loading
Loading
+35 −9
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothA2dpSink;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAvrcpController;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHeadsetClient;
@@ -57,7 +58,7 @@ class AdapterProperties {

    private volatile String mName;
    private volatile byte[] mAddress;
    private volatile int mBluetoothClass;
    private volatile BluetoothClass mBluetoothClass;
    private volatile int mScanMode;
    private volatile int mDiscoverableTimeout;
    private volatile ParcelUuid[] mUuids;
@@ -217,21 +218,38 @@ class AdapterProperties {
    /**
     * Set the Bluetooth Class of Device (CoD) of the adapter.
     *
     * @param bytes BluetoothClass of the device
     * <p>Bluetooth stack stores some adapter properties in native BT stack storage and some in the
     * Java Android stack. Bluetooth CoD is stored in the Android layer through
     * {@link android.provider.Settings.Global#BLUETOOTH_CLASS_OF_DEVICE}.
     *
     * <p>Due to this, the getAdapterPropertyNative and adapterPropertyChangedCallback methods don't
     * actually update mBluetoothClass. Hence, we update the field mBluetoothClass every time we
     * successfully update BluetoothClass.
     *
     * @param bluetoothClass BluetoothClass of the device
     */
    boolean setBluetoothClass(byte[] bytes) {
    boolean setBluetoothClass(BluetoothClass bluetoothClass) {
        synchronized (mObject) {
            return mService.setAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE,
                    bytes);
            boolean result =
                    mService.setAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE,
                            bluetoothClass.getClassOfDeviceBytes());

            if (result) {
                mBluetoothClass = bluetoothClass;
            }

            return result;
        }
    }

    /**
     * @return the mClass
     * @return the BluetoothClass of the Bluetooth adapter.
     */
    int getBluetoothClass() {
    BluetoothClass getBluetoothClass() {
        synchronized (mObject) {
            return mBluetoothClass;
        }
    }

    /**
     * @return the mScanMode
@@ -678,7 +696,15 @@ class AdapterProperties {
                                AdapterService.BLUETOOTH_PERM);
                        break;
                    case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:
                        mBluetoothClass = Utils.byteArrayToInt(val, 0);
                        if (val == null || val.length != 3) {
                            debugLog("Invalid BT CoD value from stack.");
                            return;
                        }
                        int bluetoothClass =
                                ((int) val[0] << 16) + ((int) val[1] << 8) + (int) val[2];
                        if (bluetoothClass != 0) {
                            mBluetoothClass = new BluetoothClass(bluetoothClass);
                        }
                        debugLog("BT Class:" + mBluetoothClass);
                        break;
                    case AbstractionLayer.BT_PROPERTY_ADAPTER_SCAN_MODE:
+20 −4
Original line number Diff line number Diff line
@@ -406,6 +406,7 @@ public class AdapterService extends Service {
        //Load the name and address
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR);
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME);
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE);
        mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
@@ -517,8 +518,7 @@ public class AdapterService extends Service {
    void setBluetoothClassFromConfig() {
        int bluetoothClassConfig = retrieveBluetoothClassConfig();
        if (bluetoothClassConfig != 0) {
            mAdapterProperties.setBluetoothClass(
                    new BluetoothClass(bluetoothClassConfig).getClassOfDeviceBytes());
            mAdapterProperties.setBluetoothClass(new BluetoothClass(bluetoothClassConfig));
        }
    }

@@ -928,6 +928,17 @@ public class AdapterService extends Service {
            return service.setName(name);
        }

        public BluetoothClass getBluetoothClass() {
            if (!Utils.checkCaller()) {
                Log.w(TAG, "getBluetoothClass() - Not allowed for non-active user");
                return null;
            }

            AdapterService service = getService();
            if (service == null) return null;
            return service.getBluetoothClass();
        }

        public boolean setBluetoothClass(BluetoothClass bluetoothClass) {
            if (!Utils.checkCaller()) {
                Log.w(TAG, "setBluetoothClass() - Not allowed for non-active user");
@@ -1714,6 +1725,12 @@ public class AdapterService extends Service {
        return mAdapterProperties.setName(name);
    }

    BluetoothClass getBluetoothClass() {
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");

        return mAdapterProperties.getBluetoothClass();
    }

    /**
     * Sets the Bluetooth CoD on the local adapter and also modifies the storage config for it.
     *
@@ -1723,8 +1740,7 @@ public class AdapterService extends Service {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED,
                "Need BLUETOOTH PRIVILEGED permission");

        boolean result =
                mAdapterProperties.setBluetoothClass(bluetoothClass.getClassOfDeviceBytes());
        boolean result = mAdapterProperties.setBluetoothClass(bluetoothClass);

        if (!result) {
            Log.e(TAG,