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

Commit 1ceb0121 authored by Wei Wang's avatar Wei Wang
Browse files

Remove MR2 BLE Advertising hidden API from L codebase (1/2).

Change-Id: I78bb8b89de56bddb7422da1d1d4468c88a68ea11
parent 7faed2f1
Loading
Loading
Loading
Loading
+7 −246
Original line number Diff line number Diff line
@@ -218,22 +218,6 @@ public final class BluetoothAdapter {
    public static final String ACTION_SCAN_MODE_CHANGED =
            "android.bluetooth.adapter.action.SCAN_MODE_CHANGED";

    /**
     * Broadcast Action: Indicate BLE Advertising is started.
     * @hide
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_BLUETOOTH_ADVERTISING_STARTED =
            "android.bluetooth.adapter.action.ADVERTISING_STARTED";

    /**
     * Broadcast Action: Indicated BLE Advertising is stopped.
     * @hide
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_BLUETOOTH_ADVERTISING_STOPPED =
            "android.bluetooth.adapter.action.ADVERTISING_STOPPED";

    /**
     * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
     * intents to request the current scan mode. Possible values are:
@@ -403,8 +387,6 @@ public final class BluetoothAdapter {
    private IBluetooth mService;

    private final Map<LeScanCallback, GattCallbackWrapper> mLeScanClients;
    private BluetoothAdvScanData mBluetoothAdvScanData = null;
    private GattCallbackWrapper mAdvertisingGattCallback;
    private final Handler mHandler;  // Handler to post the advertise callback to run on main thread.
    private final Object mLock = new Object();

@@ -480,29 +462,6 @@ public final class BluetoothAdapter {
                address[0], address[1], address[2], address[3], address[4], address[5]));
    }

    /**
     * Returns a {@link BluetoothAdvScanData} object representing advertising data.
     * Data will be reset when bluetooth service is turned off.
     * @hide
     */
    public BluetoothAdvScanData getAdvScanData() {
      try {
          IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
          if (iGatt == null) {
              // BLE is not supported
              Log.e(TAG, "failed to start, iGatt null");
              return null;
          }
          if (mBluetoothAdvScanData == null) {
              mBluetoothAdvScanData = new BluetoothAdvScanData(iGatt, BluetoothAdvScanData.AD);
          }
          return mBluetoothAdvScanData;
      } catch (RemoteException e) {
          Log.e(TAG, "failed to get advScanData, error: " + e);
          return null;
      }
    }

    /**
     * Returns a {@link BluetoothLeAdvertiser} object for Bluetooth LE Advertising operations.
     */
@@ -519,106 +478,6 @@ public final class BluetoothAdapter {
        return new BluetoothLeScanner(mManagerService);
    }

    /**
     * Interface for BLE advertising callback.
     *
     * @hide
     */
    public interface AdvertiseCallback {
        /**
         * Callback when advertise starts.
         * @param status - {@link #ADVERTISE_CALLBACK_SUCCESS} for success, others for failure.
         */
        void onAdvertiseStart(int status);
        /**
         * Callback when advertise stops.
         * @param status - {@link #ADVERTISE_CALLBACK_SUCCESS} for success, others for failure.
         */
        void onAdvertiseStop(int status);
    }

    /**
     * Start BLE advertising using current {@link BluetoothAdvScanData}.
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}
     *
     * @param callback - {@link AdvertiseCallback}
     * @return true if BLE advertising succeeds, false otherwise.
     * @hide
     */
    public boolean startAdvertising(final AdvertiseCallback callback) {
        if (getState() != STATE_ON) return false;
        try {
            IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
            if (iGatt == null) {
                // BLE is not supported.
                return false;
            }
            // Restart/reset advertising packets if advertising is in progress.
            if (isAdvertising()) {
                // Invalid advertising callback.
                if (mAdvertisingGattCallback == null || mAdvertisingGattCallback.mLeHandle == -1) {
                    Log.e(TAG, "failed to restart advertising, invalid callback");
                    return false;
                }
                iGatt.startAdvertising(mAdvertisingGattCallback.mLeHandle);
                // Run the callback from main thread.
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        // callback with status success.
                        callback.onAdvertiseStart(ADVERTISE_CALLBACK_SUCCESS);
                    }
                });
                return true;
            }
            UUID uuid = UUID.randomUUID();
            GattCallbackWrapper wrapper =
                new GattCallbackWrapper(this, null, null, callback);
            iGatt.registerClient(new ParcelUuid(uuid), wrapper);
            if (!wrapper.advertiseStarted()) {
                return false;
            }
            synchronized (mLock) {
                mAdvertisingGattCallback = wrapper;
            }
            return true;
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
            return false;
        }
    }

    /**
     * Stop BLE advertising. The callback has to be the same one used for start advertising.
     *
     * @param callback - {@link AdvertiseCallback}
     * @return true if BLE advertising stops, false otherwise.
     * @hide
     */
    public boolean stopAdvertising(AdvertiseCallback callback) {
        try {
            IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
            if (iGatt == null) {
                // BLE is not supported
                return false;
            }
            if (mAdvertisingGattCallback == null) {
                // no callback.
                return false;
            }
            // Make sure same callback is used for start and stop advertising.
            if (callback != mAdvertisingGattCallback.mAdvertiseCallback) {
                Log.e(TAG, "must use the same callback for star/stop advertising");
                return false;
            }
            mAdvertisingGattCallback.stopAdvertising();
            return true;
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
            return false;
        }
    }

    /**
     * Return true if Bluetooth is currently enabled and ready for use.
     * <p>Equivalent to:
@@ -1075,23 +934,6 @@ public final class BluetoothAdapter {
        return false;
    }

    /**
     * Returns whether BLE is currently advertising.
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}.
     *
     * @hide
     */
    public boolean isAdvertising() {
        if (getState() != STATE_ON) return false;
        try {
            IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
            return iGatt.isAdvertising();
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
        return false;
    }

    /**
     * Return the set of {@link BluetoothDevice} objects that are bonded
     * (paired) to the local adapter.
@@ -1537,8 +1379,6 @@ public final class BluetoothAdapter {
                if (VDBG) Log.d(TAG, "onBluetoothServiceDown: " + mService);
                synchronized (mManagerCallback) {
                    mService = null;
                    // Reset bluetooth adv scan data when Gatt service is down.
                    mBluetoothAdvScanData = null;
                    for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){
                        try {
                            if (cb != null) {
@@ -1822,7 +1662,6 @@ public final class BluetoothAdapter {
        private static final int LE_CALLBACK_REG_TIMEOUT = 2000;
        private static final int LE_CALLBACK_REG_WAIT_COUNT = 5;

        private final AdvertiseCallback mAdvertiseCallback;
        private final LeScanCallback mLeScanCb;

        // mLeHandle 0: not registered
@@ -1838,27 +1677,12 @@ public final class BluetoothAdapter {
            mLeScanCb = leScanCb;
            mScanFilter = uuid;
            mLeHandle = 0;
            mAdvertiseCallback = null;
        }

        public GattCallbackWrapper(BluetoothAdapter bluetoothAdapter, LeScanCallback leScanCb,
            UUID[] uuid, AdvertiseCallback callback) {
          mBluetoothAdapter = new WeakReference<BluetoothAdapter>(bluetoothAdapter);
          mLeScanCb = leScanCb;
          mScanFilter = uuid;
          mLeHandle = 0;
          mAdvertiseCallback = callback;
        }

        public boolean scanStarted() {
            return waitForRegisteration(LE_CALLBACK_REG_WAIT_COUNT);
        }

        public boolean advertiseStarted() {
            // Wait for registeration callback.
            return waitForRegisteration(1);
        }

        private boolean waitForRegisteration(int maxWaitCount) {
            boolean started = false;
            synchronized(this) {
@@ -1878,27 +1702,6 @@ public final class BluetoothAdapter {
            return started;
        }

        public void stopAdvertising() {
            synchronized (this) {
                if (mLeHandle <= 0) {
                    Log.e(TAG, "Error state, mLeHandle: " + mLeHandle);
                    return;
                }
                BluetoothAdapter adapter = mBluetoothAdapter.get();
                if (adapter != null) {
                    try {
                        IBluetoothGatt iGatt = adapter.getBluetoothManager().getBluetoothGatt();
                        iGatt.stopAdvertising();
                    } catch (RemoteException e) {
                        Log.e(TAG, "Failed to stop advertising" + e);
                    }
                } else {
                    Log.e(TAG, "stopAdvertising, BluetoothAdapter is null");
                }
                notifyAll();
            }
        }

        public void stopLeScan() {
            synchronized(this) {
                if (mLeHandle <= 0) {
@@ -1940,9 +1743,6 @@ public final class BluetoothAdapter {
                        BluetoothAdapter adapter = mBluetoothAdapter.get();
                        if (adapter != null) {
                            iGatt = adapter.getBluetoothManager().getBluetoothGatt();
                            if (mAdvertiseCallback != null) {
                                iGatt.startAdvertising(mLeHandle);
                            } else {
                            if (mScanFilter == null) {
                                iGatt.startScan(mLeHandle, false);
                            } else {
@@ -1952,7 +1752,6 @@ public final class BluetoothAdapter {
                                }
                                iGatt.startScanWithUuids(mLeHandle, false, uuids);
                            }
                            }
                        } else {
                            Log.e(TAG, "onClientRegistered, BluetoothAdapter null");
                            mLeHandle = -1;
@@ -2080,44 +1879,6 @@ public final class BluetoothAdapter {
        }

        public void onAdvertiseStateChange(int advertiseState, int status) {
            Log.d(TAG, "on advertise call back, state: " + advertiseState + " status: " + status);
            if (advertiseState == STATE_ADVERTISE_STARTED) {
                if (status == ADVERTISE_CALLBACK_SUCCESS) {
                    mAdvertiseCallback.onAdvertiseStart(status);
                } else {
                    // If status is unsuccessful and advertise state is started, it means stop
                    // advertising fails.
                    mAdvertiseCallback.onAdvertiseStop(status);
                }
            } else {
                synchronized (this) {
                    if (status == ADVERTISE_CALLBACK_SUCCESS) {
                        BluetoothAdapter adapter = mBluetoothAdapter.get();
                        if (adapter != null) {
                            try {
                                IBluetoothGatt iGatt =
                                        adapter.getBluetoothManager().getBluetoothGatt();
                                Log.d(TAG, "unregistering client " + mLeHandle);
                                iGatt.unregisterClient(mLeHandle);
                                // Reset advertise app handle.
                                mLeHandle = -1;
                                adapter.mAdvertisingGattCallback = null;
                            } catch (RemoteException e) {
                                Log.e(TAG, "Failed to unregister client" + e);
                            }
                        } else {
                            Log.e(TAG, "cannot unregister client, BluetoothAdapter is null");
                        }
                    }
                }
                if (status == ADVERTISE_CALLBACK_SUCCESS) {
                    mAdvertiseCallback.onAdvertiseStop(status);
                } else{
                    // If status is unsuccesful and advertise state is stopped, it means start
                    // advertising fails.
                    mAdvertiseCallback.onAdvertiseStart(status);
                }
            }
        }

        @Override
+0 −152
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.bluetooth;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;

import java.util.Collections;
import java.util.List;


/**
 * This class provides the public APIs to set advertising and scan response data when BLE device
 * operates in peripheral mode. <br>
 * The exact format is defined in Bluetooth 4.0 specification, Volume 3, Part C, Section 11
 * @hide
 */
public final class BluetoothAdvScanData {

  /**
   * Available data types of {@link BluetoothAdvScanData}.
   */
  public static final int AD = 0;  // Advertising Data
  public static final int SCAN_RESPONSE = 1;  // Scan Response
  public static final int EIR = 2;  // Extended Inquiry Response

  private static final String TAG = "BluetoothAdvScanData";

  /**
   * Data type of BluetoothAdvScanData.
   */
  private final int mDataType;
  /**
   * Bluetooth Gatt Service.
   */
  private IBluetoothGatt mBluetoothGatt;

  /**
   * @param mBluetoothGatt
   * @param dataType
   */
  public BluetoothAdvScanData(IBluetoothGatt mBluetoothGatt, int dataType) {
    this.mBluetoothGatt = mBluetoothGatt;
    this.mDataType = dataType;
  }

  /**
   * @return advertising data type.
   */
  public int getDataType() {
    return mDataType;
  }

  /**
   * Set manufactureCode and manufactureData.
   * Returns true if manufacturer data is set, false if there is no enough room to set
   * manufacturer data or the data is already set.
   * @param manufacturerCode - unique identifier for the manufacturer
   * @param manufacturerData - data associated with the specific manufacturer.
   */
  public boolean setManufacturerData(int manufacturerCode, byte[] manufacturerData) {
    if (mDataType != AD) return false;
    try {
      return mBluetoothGatt.setAdvManufacturerCodeAndData(manufacturerCode, manufacturerData);
    } catch (RemoteException e) {
      Log.e(TAG, "Unable to set manufacturer id and data.", e);
      return false;
    }
  }

  /**
   * Set service data.  Note the service data can only be set when the data type is {@code AD};
   * @param serviceData
   */
  public boolean setServiceData(byte[] serviceData) {

    if (mDataType != AD) return false;
    if (serviceData == null) return false;
    try {
      return mBluetoothGatt.setAdvServiceData(serviceData);
    } catch (RemoteException e) {
      Log.e(TAG, "Unable to set service data.", e);
      return false;
    }
  }

  /**
   * Returns an immutable list of service uuids that will be advertised.
   */
  public List<ParcelUuid> getServiceUuids() {
    try {
      return Collections.unmodifiableList(mBluetoothGatt.getAdvServiceUuids());
    } catch (RemoteException e) {
      Log.e(TAG, "Unable to get service uuids.", e);
      return null;
    }
  }

  /**
   * Returns manufacturer data.
   */
  public byte[] getManufacturerData() {
    if (mBluetoothGatt == null) return null;
    try {
      return mBluetoothGatt.getAdvManufacturerData();
    } catch (RemoteException e) {
      Log.e(TAG, "Unable to get manufacturer data.", e);
      return null;
    }
  }

  /**
   * Returns service data.
   */
  public byte[] getServiceData() {
    if (mBluetoothGatt == null) return null;
    try {
      return mBluetoothGatt.getAdvServiceData();
    } catch (RemoteException e) {
      Log.e(TAG, "Unable to get service data.", e);
      return null;
    }
  }

  /**
   * Remove manufacturer data based on given manufacturer code.
   * @param manufacturerCode
   */
  public void removeManufacturerCodeAndData(int manufacturerCode) {
    if (mBluetoothGatt != null) {
      try {
        mBluetoothGatt.removeAdvManufacturerCodeAndData(manufacturerCode);
      } catch (RemoteException e) {
        Log.e(TAG, "Unable to remove manufacturer : " + manufacturerCode, e);
      }
    }
  }
}
+0 −9
Original line number Diff line number Diff line
@@ -48,15 +48,6 @@ interface IBluetoothGatt {
    void unregisterClient(in int clientIf);
    void clientConnect(in int clientIf, in String address, in boolean isDirect, in int transport);
    void clientDisconnect(in int clientIf, in String address);
    void startAdvertising(in int appIf);
    void stopAdvertising();
    boolean setAdvServiceData(in byte[] serviceData);
    byte[] getAdvServiceData();
    boolean setAdvManufacturerCodeAndData(int manufactureCode, in byte[] manufacturerData);
    byte[] getAdvManufacturerData();
    List<ParcelUuid> getAdvServiceUuids();
    void removeAdvManufacturerCodeAndData(int manufacturerCode);
    boolean isAdvertising();
    void refreshDevice(in int clientIf, in String address);
    void discoverServices(in int clientIf, in String address);
    void readCharacteristic(in int clientIf, in String address, in int srvcType,
+0 −1
Original line number Diff line number Diff line
@@ -64,7 +64,6 @@ oneway interface IBluetoothGattCallback {
                             in int charInstId, in ParcelUuid charUuid,
                             in byte[] value);
    void onReadRemoteRssi(in String address, in int rssi, in int status);
    void onAdvertiseStateChange(in int advertiseState, in int status);
    void onMultiAdvertiseCallback(in int status);
    void onConfigureMTU(in String address, in int mtu, in int status);
    void onConnectionCongested(in String address, in boolean congested);
+0 −5
Original line number Diff line number Diff line
@@ -331,11 +331,6 @@ public final class BluetoothLeAdvertiser {
            // no op
        }

        @Override
        public void onAdvertiseStateChange(int advertiseState, int status) {
            // no op
        }

        @Override
        public void onMultiAdvertiseCallback(int status) {
            // TODO: This logic needs to be re-visited to account
Loading