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

Commit 71a86a64 authored by Wei Wang's avatar Wei Wang Committed by Android (Google) Code Review
Browse files

Merge "Unregister app when advertise failed after registration." into lmp-dev

parents 6398bb32 e1a357c1
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -119,8 +119,8 @@ public final class AdvertiseData implements Parcelable {
        }
        AdvertiseData other = (AdvertiseData) obj;
        return Objects.equals(mServiceUuids, other.mServiceUuids) &&
                Utils.equals(mManufacturerSpecificData, other.mManufacturerSpecificData) &&
                Utils.equals(mServiceData, other.mServiceData) &&
                BluetoothLeUtils.equals(mManufacturerSpecificData, other.mManufacturerSpecificData) &&
                BluetoothLeUtils.equals(mServiceData, other.mServiceData) &&
                        mIncludeDeviceName == other.mIncludeDeviceName &&
                        mIncludeTxPowerLevel == other.mIncludeTxPowerLevel;
    }
@@ -128,8 +128,8 @@ public final class AdvertiseData implements Parcelable {
    @Override
    public String toString() {
        return "AdvertiseData [mServiceUuids=" + mServiceUuids + ", mManufacturerSpecificData="
                + Utils.toString(mManufacturerSpecificData) + ", mServiceData="
                + Utils.toString(mServiceData)
                + BluetoothLeUtils.toString(mManufacturerSpecificData) + ", mServiceData="
                + BluetoothLeUtils.toString(mServiceData)
                + ", mIncludeTxPowerLevel=" + mIncludeTxPowerLevel + ", mIncludeDeviceName="
                + mIncludeDeviceName + "]";
    }
+12 −10
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@ public final class BluetoothLeAdvertiser {
            AdvertiseData advertiseData, AdvertiseData scanResponse,
            final AdvertiseCallback callback) {
        synchronized (mLeAdvertisers) {
            checkAdapterState();
            BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
            if (callback == null) {
                throw new IllegalArgumentException("callback cannot be null");
            }
@@ -150,7 +150,7 @@ public final class BluetoothLeAdvertiser {
     */
    public void stopAdvertising(final AdvertiseCallback callback) {
        synchronized (mLeAdvertisers) {
            checkAdapterState();
            BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
            if (callback == null) {
                throw new IllegalArgumentException("callback cannot be null");
            }
@@ -265,9 +265,18 @@ public final class BluetoothLeAdvertiser {
                }
                if (mClientIf > 0 && mIsAdvertising) {
                    mLeAdvertisers.put(mAdvertiseCallback, this);
                } else {
                } else if (mClientIf <= 0) {
                    // Post internal error if registration failed.
                    postStartFailure(mAdvertiseCallback,
                            AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
                } else {
                    // Unregister application if it's already registered but advertise failed.
                    try {
                        mBluetoothGatt.unregisterClient(mClientIf);
                        mClientIf = -1;
                    } catch (RemoteException e) {
                        Log.e(TAG, "remote exception when unregistering", e);
                    }
                }
            }
        }
@@ -342,13 +351,6 @@ public final class BluetoothLeAdvertiser {
        }
    }

    // TODO: move this api to a common util class.
    private void checkAdapterState() {
        if (mBluetoothAdapter.getState() != mBluetoothAdapter.STATE_ON) {
            throw new IllegalStateException("BT Adapter is not turned ON");
        }
    }

    private void postStartFailure(final AdvertiseCallback callback, final int error) {
        mHandler.post(new Runnable() {
            @Override
+4 −12
Original line number Diff line number Diff line
@@ -80,11 +80,10 @@ public final class BluetoothLeScanner {
     * @throws IllegalArgumentException If {@code callback} is null.
     */
    public void startScan(final ScanCallback callback) {
        checkAdapterState();
        if (callback == null) {
            throw new IllegalArgumentException("callback is null");
        }
        this.startScan(null, new ScanSettings.Builder().build(), callback);
        startScan(null, new ScanSettings.Builder().build(), callback);
    }

    /**
@@ -104,7 +103,7 @@ public final class BluetoothLeScanner {

    private void startScan(List<ScanFilter> filters, ScanSettings settings,
            final ScanCallback callback, List<List<ResultStorageDescriptor>> resultStorages) {
        checkAdapterState();
        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        if (settings == null || callback == null) {
            throw new IllegalArgumentException("settings or callback is null");
        }
@@ -142,7 +141,7 @@ public final class BluetoothLeScanner {
     * @param callback
     */
    public void stopScan(ScanCallback callback) {
        checkAdapterState();
        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        synchronized (mLeScanClients) {
            BleScanCallbackWrapper wrapper = mLeScanClients.remove(callback);
            if (wrapper == null) {
@@ -162,7 +161,7 @@ public final class BluetoothLeScanner {
     *            used to start scan.
     */
    public void flushPendingScanResults(ScanCallback callback) {
        checkAdapterState();
        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        if (callback == null) {
            throw new IllegalArgumentException("callback cannot be null!");
        }
@@ -373,13 +372,6 @@ public final class BluetoothLeScanner {
        }
    }

    // TODO: move this api to a common util class.
    private void checkAdapterState() {
        if (mBluetoothAdapter.getState() != mBluetoothAdapter.STATE_ON) {
            throw new IllegalStateException("BT Adapter is not turned ON");
        }
    }

    private void postCallbackError(final ScanCallback callback, final int errorCode) {
        mHandler.post(new Runnable() {
            @Override
+15 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.bluetooth.le;

import android.bluetooth.BluetoothAdapter;
import android.util.SparseArray;

import java.util.Arrays;
@@ -29,7 +30,7 @@ import java.util.Set;
 *
 * @hide
 */
public class Utils {
public class BluetoothLeUtils {

    /**
     * Returns a string composed from a {@link SparseArray}.
@@ -123,4 +124,17 @@ public class Utils {
        }
        return true;
    }

    /**
     * Ensure Bluetooth is turned on.
     *
     * @throws IllegalStateException If {@code adapter} is null or Bluetooth state is not
     *             {@link BluetoothAdapter#STATE_ON}.
     */
    static void checkAdapterStateOn(BluetoothAdapter adapter) {
        if (adapter == null || adapter.getState() != BluetoothAdapter.STATE_ON) {
            throw new IllegalStateException("BT Adapter is not turned ON");
        }
    }

}
+2 −2
Original line number Diff line number Diff line
@@ -268,8 +268,8 @@ public final class ScanRecord {
    @Override
    public String toString() {
        return "ScanRecord [mAdvertiseFlags=" + mAdvertiseFlags + ", mServiceUuids=" + mServiceUuids
                + ", mManufacturerSpecificData=" + Utils.toString(mManufacturerSpecificData)
                + ", mServiceData=" + Utils.toString(mServiceData)
                + ", mManufacturerSpecificData=" + BluetoothLeUtils.toString(mManufacturerSpecificData)
                + ", mServiceData=" + BluetoothLeUtils.toString(mServiceData)
                + ", mTxPowerLevel=" + mTxPowerLevel + ", mDeviceName=" + mDeviceName + "]";
    }