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

Commit 1f4e1118 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Bluetooth: Add handler parameter to connectGatt"

parents 06abc2c4 4eab4965
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7077,6 +7077,7 @@ package android.bluetooth {
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int, android.os.Handler);
    method public boolean createBond();
    method public android.bluetooth.BluetoothSocket createInsecureRfcommSocketToServiceRecord(java.util.UUID) throws java.io.IOException;
    method public android.bluetooth.BluetoothSocket createRfcommSocketToServiceRecord(java.util.UUID) throws java.io.IOException;
+1 −0
Original line number Diff line number Diff line
@@ -7380,6 +7380,7 @@ package android.bluetooth {
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int, android.os.Handler);
    method public boolean createBond();
    method public android.bluetooth.BluetoothSocket createInsecureRfcommSocketToServiceRecord(java.util.UUID) throws java.io.IOException;
    method public android.bluetooth.BluetoothSocket createRfcommSocketToServiceRecord(java.util.UUID) throws java.io.IOException;
+1 −0
Original line number Diff line number Diff line
@@ -7086,6 +7086,7 @@ package android.bluetooth {
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int);
    method public android.bluetooth.BluetoothGatt connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback, int, int, android.os.Handler);
    method public boolean createBond();
    method public android.bluetooth.BluetoothSocket createInsecureRfcommSocketToServiceRecord(java.util.UUID) throws java.io.IOException;
    method public android.bluetooth.BluetoothSocket createRfcommSocketToServiceRecord(java.util.UUID) throws java.io.IOException;
+39 −4
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.ParcelUuid;
@@ -1679,12 +1681,45 @@ public final class BluetoothDevice implements Parcelable {
     *             {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE}
     * @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},
     *             and {@link BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if
     *             {@code autoConnect} is set to true.
     * @throws IllegalArgumentException if callback is null
     *             and {@link BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect
     *             if {@code autoConnect} is set to true.
     * @throws NullPointerException if callback is null
     */
    public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                     BluetoothGattCallback callback, int transport, int phy) {
        return connectGatt(context, autoConnect,callback, TRANSPORT_AUTO, PHY_LE_1M_MASK, null);
    }

    /**
     * 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 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 the service's main thread.
     * @throws NullPointerException if callback is null
     */
    public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                     BluetoothGattCallback callback, int transport, int phy,
                                     Handler handler) {
        if (callback == null)
            throw new NullPointerException("callback is null");

        if (handler == null)
            handler = new Handler(Looper.getMainLooper());

        // TODO(Bluetooth) check whether platform support BLE
        //     Do the check here or in GattServer?
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
@@ -1696,7 +1731,7 @@ public final class BluetoothDevice implements Parcelable {
                return null;
            }
            BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, phy);
            gatt.connect(autoConnect, callback);
            gatt.connect(autoConnect, callback, handler);
            return gatt;
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
+97 −71
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.bluetooth;

import android.content.Context;
import android.os.Handler;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;
@@ -43,6 +44,7 @@ public final class BluetoothGatt implements BluetoothProfile {

    private IBluetoothGatt mService;
    private BluetoothGattCallback mCallback;
    private Handler mHandler;
    private int mClientIf;
    private BluetoothDevice mDevice;
    private boolean mAutoConnect;
@@ -154,8 +156,14 @@ public final class BluetoothGatt implements BluetoothProfile {
                }
                mClientIf = clientIf;
                if (status != GATT_SUCCESS) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
                                              BluetoothProfile.STATE_DISCONNECTED);
                        }
                    });

                    synchronized(mStateLock) {
                        mConnState = CONN_STATE_IDLE;
                    }
@@ -181,11 +189,12 @@ public final class BluetoothGatt implements BluetoothProfile {
                    return;
                }

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onPhyUpdate(BluetoothGatt.this, txPhy, rxPhy, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -200,11 +209,12 @@ public final class BluetoothGatt implements BluetoothProfile {
                    return;
                }

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onPhyRead(BluetoothGatt.this, txPhy, rxPhy, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -221,11 +231,13 @@ public final class BluetoothGatt implements BluetoothProfile {
                }
                int profileState = connected ? BluetoothProfile.STATE_CONNECTED :
                                               BluetoothProfile.STATE_DISCONNECTED;
                try {

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onConnectionStateChange(BluetoothGatt.this, status, profileState);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });

                synchronized(mStateLock) {
                    if (connected) {
@@ -279,11 +291,12 @@ public final class BluetoothGatt implements BluetoothProfile {
                    }
                }

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onServicesDiscovered(BluetoothGatt.this, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -328,11 +341,12 @@ public final class BluetoothGatt implements BluetoothProfile {

                if (status == 0) characteristic.setValue(value);

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -373,11 +387,12 @@ public final class BluetoothGatt implements BluetoothProfile {

                mAuthRetryState = AUTH_RETRY_STATE_IDLE;

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -398,11 +413,12 @@ public final class BluetoothGatt implements BluetoothProfile {

                characteristic.setValue(value);

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -442,11 +458,12 @@ public final class BluetoothGatt implements BluetoothProfile {

                mAuthRetryState = AUTH_RETRY_STATE_IDLE;

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -485,11 +502,12 @@ public final class BluetoothGatt implements BluetoothProfile {

                mAuthRetryState = AUTH_RETRY_STATE_IDLE;

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -508,11 +526,12 @@ public final class BluetoothGatt implements BluetoothProfile {
                    mDeviceBusy = false;
                }

                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onReliableWriteCompleted(BluetoothGatt.this, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -526,11 +545,12 @@ public final class BluetoothGatt implements BluetoothProfile {
                if (!address.equals(mDevice.getAddress())) {
                    return;
                }
                try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -544,11 +564,13 @@ public final class BluetoothGatt implements BluetoothProfile {
                if (!address.equals(mDevice.getAddress())) {
                    return;
                }
                try {

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onMtuChanged(BluetoothGatt.this, mtu, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }

            /**
@@ -564,12 +586,14 @@ public final class BluetoothGatt implements BluetoothProfile {
                if (!address.equals(mDevice.getAddress())) {
                    return;
                }
                try {

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onConnectionUpdated(BluetoothGatt.this, interval, latency,
                                                      timeout, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception in callback", ex);
                    }
                });
            }
        };

@@ -659,11 +683,12 @@ public final class BluetoothGatt implements BluetoothProfile {
     * @return If true, the callback will be called to notify success or failure,
     *         false on immediate error
     */
    private boolean registerApp(BluetoothGattCallback callback) {
    private boolean registerApp(BluetoothGattCallback callback, Handler handler) {
        if (DBG) Log.d(TAG, "registerApp()");
        if (mService == null) return false;

        mCallback = callback;
        mHandler = handler;
        UUID uuid = UUID.randomUUID();
        if (DBG) Log.d(TAG, "registerApp() - UUID=" + uuid);

@@ -716,7 +741,8 @@ public final class BluetoothGatt implements BluetoothProfile {
     *                    device becomes available (true).
     * @return true, if the connection attempt was initiated successfully
     */
    /*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback) {
    /*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback,
                                Handler handler) {
        if (DBG) Log.d(TAG, "connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect);
        synchronized(mStateLock) {
            if (mConnState != CONN_STATE_IDLE) {
@@ -727,7 +753,7 @@ public final class BluetoothGatt implements BluetoothProfile {

        mAutoConnect = autoConnect;

        if (!registerApp(callback)) {
        if (!registerApp(callback, handler)) {
            synchronized(mStateLock) {
                mConnState = CONN_STATE_IDLE;
            }