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

Commit f92d0272 authored by Jack He's avatar Jack He
Browse files

GATT: Expose opportunistic client API to Java

* Allow Java based programs to create an opportunistic GATT client
* Such client does not hold a GATT connection. It automatically
  disconnects when no other GATT connections are active for the
  remote device.

Bug: 63347806
Test: make, run battery service
Change-Id: Ib9333817d7f17a1fa3ddacfa51c680890bac19ec
Merged-In: Ib9333817d7f17a1fa3ddacfa51c680890bac19ec
(cherry picked from commit dafdf265)
parent 89c0c054
Loading
Loading
Loading
Loading
+33 −1
Original line number Original line Diff line number Diff line
@@ -1758,6 +1758,38 @@ public final class BluetoothDevice implements Parcelable {
    public BluetoothGatt connectGatt(Context context, boolean autoConnect,
    public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                     BluetoothGattCallback callback, int transport, int phy,
                                     BluetoothGattCallback callback, int transport, int phy,
                                     Handler handler) {
                                     Handler handler) {
        return connectGatt(context, autoConnect, callback, transport, false, phy, handler);
    }

    /**
     * Connect to GATT Server hosted by this device. Caller acts as GATT client.
     * The callback is used to deliver results to Caller, such as connection status as well
     * as any further GATT client operations.
     * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct
     * GATT client operations.
     * @param callback GATT callback handler that will receive asynchronous callbacks.
     * @param autoConnect Whether to directly connect to the remote device (false)
     *                    or to automatically connect as soon as the remote
     *                    device becomes available (true).
     * @param transport preferred transport for GATT connections to remote dual-mode devices
     *             {@link BluetoothDevice#TRANSPORT_AUTO} or
     *             {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE}
     * @param opportunistic Whether this GATT client is opportunistic. An opportunistic GATT client
     *                      does not hold a GATT connection. It automatically disconnects when no
     *                      other GATT connections are active for the remote device.
     * @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of
     *             {@link BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK},
     *             an d{@link BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect
     *             if {@code autoConnect} is set to true.
     * @param handler The handler to use for the callback. If {@code null}, callbacks will happen
     *             on an un-specified background thread.
     * @return A BluetoothGatt instance. You can use BluetoothGatt to conduct GATT client
     *         operations.
     * @hide
     */
    public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                     BluetoothGattCallback callback, int transport,
                                     boolean opportunistic, int phy, Handler handler) {
        if (callback == null)
        if (callback == null)
            throw new NullPointerException("callback is null");
            throw new NullPointerException("callback is null");


@@ -1771,7 +1803,7 @@ public final class BluetoothDevice implements Parcelable {
                // BLE is not supported
                // BLE is not supported
                return null;
                return null;
            }
            }
            BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, phy);
            BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, opportunistic, phy);
            gatt.connect(autoConnect, callback, handler);
            gatt.connect(autoConnect, callback, handler);
            return gatt;
            return gatt;
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        } catch (RemoteException e) {Log.e(TAG, "", e);}
+6 −4
Original line number Original line Diff line number Diff line
@@ -53,6 +53,7 @@ public final class BluetoothGatt implements BluetoothProfile {
    private Boolean mDeviceBusy = false;
    private Boolean mDeviceBusy = false;
    private int mTransport;
    private int mTransport;
    private int mPhy;
    private int mPhy;
    private boolean mOpportunistic;


    private static final int AUTH_RETRY_STATE_IDLE = 0;
    private static final int AUTH_RETRY_STATE_IDLE = 0;
    private static final int AUTH_RETRY_STATE_NO_MITM = 1;
    private static final int AUTH_RETRY_STATE_NO_MITM = 1;
@@ -172,7 +173,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                }
                }
                try {
                try {
                    mService.clientConnect(mClientIf, mDevice.getAddress(),
                    mService.clientConnect(mClientIf, mDevice.getAddress(),
                                           !mAutoConnect, mTransport, mPhy); // autoConnect is inverse of "isDirect"
                                           !mAutoConnect, mTransport, mOpportunistic, mPhy); // autoConnect is inverse of "isDirect"
                } catch (RemoteException e) {
                } catch (RemoteException e) {
                    Log.e(TAG,"",e);
                    Log.e(TAG,"",e);
                }
                }
@@ -628,11 +629,12 @@ public final class BluetoothGatt implements BluetoothProfile {
        };
        };


    /*package*/ BluetoothGatt(IBluetoothGatt iGatt, BluetoothDevice device,
    /*package*/ BluetoothGatt(IBluetoothGatt iGatt, BluetoothDevice device,
                                int transport, int phy) {
                                int transport, boolean opportunistic, int phy) {
        mService = iGatt;
        mService = iGatt;
        mDevice = device;
        mDevice = device;
        mTransport = transport;
        mTransport = transport;
        mPhy = phy;
        mPhy = phy;
        mOpportunistic = opportunistic;
        mServices = new ArrayList<BluetoothGattService>();
        mServices = new ArrayList<BluetoothGattService>();


        mConnState = CONN_STATE_IDLE;
        mConnState = CONN_STATE_IDLE;
@@ -839,8 +841,8 @@ public final class BluetoothGatt implements BluetoothProfile {
     */
     */
    public boolean connect() {
    public boolean connect() {
        try {
        try {
            mService.clientConnect(mClientIf, mDevice.getAddress(),
            mService.clientConnect(mClientIf, mDevice.getAddress(), false, mTransport,
                                   false, mTransport, mPhy); // autoConnect is inverse of "isDirect"
                    mOpportunistic, mPhy); // autoConnect is inverse of "isDirect"
            return true;
            return true;
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            Log.e(TAG,"",e);
            Log.e(TAG,"",e);
+1 −1
Original line number Original line Diff line number Diff line
@@ -74,7 +74,7 @@ interface IBluetoothGatt {
    void registerClient(in ParcelUuid appId, in IBluetoothGattCallback callback);
    void registerClient(in ParcelUuid appId, in IBluetoothGattCallback callback);


    void unregisterClient(in int clientIf);
    void unregisterClient(in int clientIf);
    void clientConnect(in int clientIf, in String address, in boolean isDirect, in int transport, in int phy);
    void clientConnect(in int clientIf, in String address, in boolean isDirect, in int transport, in boolean opportunistic, in int phy);
    void clientDisconnect(in int clientIf, in String address);
    void clientDisconnect(in int clientIf, in String address);
    void clientSetPreferredPhy(in int clientIf, in String address, in int txPhy, in int rxPhy, in int phyOptions);
    void clientSetPreferredPhy(in int clientIf, in String address, in int txPhy, in int rxPhy, in int phyOptions);
    void clientReadPhy(in int clientIf, in String address);
    void clientReadPhy(in int clientIf, in String address);