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

Commit 18b2c9aa authored by Matthew Xie's avatar Matthew Xie
Browse files

Change BluetoothGattCallback methods argument from BluetoothDevice to BluetoothGatt

Change name of BluetoothDevice#connectGattServer to BluetoothDevice#connectGatt
Add BluetoothGatt#getDevice to retrieve device from BluetoothGatt
Add BluetoothGatt#connect() to reconnect back to the server.
Make BluetoothGatt#close() public to clean up/unregister callback
Add BluetoothDevice.getType() to return int of
bug 8529188

Change-Id: Iebd9ac68cc7a64c43972e617dd3068f66c8ea0b2
parent ee0032ba
Loading
Loading
Loading
Loading
+42 −2
Original line number Diff line number Diff line
@@ -261,6 +261,26 @@ public final class BluetoothDevice implements Parcelable {
    /** @hide */
    public static final String EXTRA_PAIRING_KEY = "android.bluetooth.device.extra.PAIRING_KEY";

    /**
     * Bluetooth device type, Unknown
     */
    public static final int DEVICE_TYPE_UNKNOWN = 0;

    /**
     * Bluetooth device type, Classic - BR/EDR devices
     */
    public static final int DEVICE_TYPE_CLASSIC = 1;

    /**
     * Bluetooth device type, Low Energy - LE-only
     */
    public static final int DEVICE_TYPE_LE = 2;

    /**
     * Bluetooth device type, Dual Mode - BR/EDR/LE
     */
    public static final int DEVICE_TYPE_DUAL = 3;

    /**
     * Broadcast Action: This intent is used to broadcast the {@link UUID}
     * wrapped as a {@link android.os.ParcelUuid} of the remote device after it
@@ -601,6 +621,26 @@ public final class BluetoothDevice implements Parcelable {
        return null;
    }

    /**
     * Get the Bluetooth device type of the remote device.
     *
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
     *
     * @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE}
     *                         {@link #DEVICE_TYPE_DUAL}.
     *         {@link #DEVICE_TYPE_UNKNOWN} if it's not available
     */
    public int getType() {
        if (sService == null) {
            Log.e(TAG, "BT not enabled. Cannot get Remote Device type");
            return DEVICE_TYPE_UNKNOWN;
        }
        try {
            return sService.getRemoteType(this);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return DEVICE_TYPE_UNKNOWN;
    }

    /**
     * Get the Bluetooth alias of the remote device.
     * <p>Alias is the locally modified name of a remote device.
@@ -1139,7 +1179,7 @@ public final class BluetoothDevice implements Parcelable {
     *                    device becomes available (true).
     * @throws IllegalArgumentException if callback is null
     */
    public BluetoothGatt connectGattServer(Context context, boolean autoConnect,
    public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                     BluetoothGattCallback callback) {
        // TODO(Bluetooth) check whether platform support BLE
        //     Do the check here or in GattServer?
+44 −14
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ import java.util.UUID;
 * with Bluetooth Smart or Smart Ready devices.
 *
 * <p>To connect to a remote peripheral device, create a {@link BluetoothGattCallback}
 * and call {@link BluetoothDevice#connectGattServer} to get a instance of this class.
 * and call {@link BluetoothDevice#connectGatt} to get a instance of this class.
 * GATT capable devices can be discovered using the Bluetooth device discovery or BLE
 * scan process.
 */
@@ -66,6 +66,7 @@ public final class BluetoothGatt implements BluetoothProfile {
    private static final int CONN_STATE_CONNECTING = 1;
    private static final int CONN_STATE_CONNECTED = 2;
    private static final int CONN_STATE_DISCONNECTING = 3;
    private static final int CONN_STATE_CLOSED = 4;

    private List<BluetoothGattService> mServices;

@@ -135,7 +136,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                }
                mClientIf = clientIf;
                if (status != GATT_SUCCESS) {
                    mCallback.onConnectionStateChange(mDevice, GATT_FAILURE,
                    mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
                                                      BluetoothProfile.STATE_DISCONNECTED);
                    synchronized(mStateLock) {
                        mConnState = CONN_STATE_IDLE;
@@ -164,7 +165,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                int profileState = connected ? BluetoothProfile.STATE_CONNECTED :
                                               BluetoothProfile.STATE_DISCONNECTED;
                try {
                    mCallback.onConnectionStateChange(mDevice, status, profileState);
                    mCallback.onConnectionStateChange(BluetoothGatt.this, status, profileState);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -291,7 +292,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                    return;
                }
                try {
                    mCallback.onServicesDiscovered(mDevice, status);
                    mCallback.onServicesDiscovered(BluetoothGatt.this, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -338,7 +339,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                if (status == 0) characteristic.setValue(value);

                try {
                    mCallback.onCharacteristicRead(characteristic, status);
                    mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -384,7 +385,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                mAuthRetry = false;

                try {
                    mCallback.onCharacteristicWrite(characteristic, status);
                    mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -415,7 +416,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                characteristic.setValue(value);

                try {
                    mCallback.onCharacteristicChanged(characteristic);
                    mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -464,7 +465,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                mAuthRetry = true;

                try {
                    mCallback.onDescriptorRead(descriptor, status);
                    mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -512,7 +513,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                mAuthRetry = false;

                try {
                    mCallback.onDescriptorWrite(descriptor, status);
                    mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -529,7 +530,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                    return;
                }
                try {
                    mCallback.onReliableWriteCompleted(mDevice, status);
                    mCallback.onReliableWriteCompleted(BluetoothGatt.this, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -546,7 +547,7 @@ public final class BluetoothGatt implements BluetoothProfile {
                    return;
                }
                try {
                    mCallback.onReadRemoteRssi(mDevice, rssi, status);
                    mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
                } catch (Exception ex) {
                    Log.w(TAG, "Unhandled exception: " + ex);
                }
@@ -563,12 +564,13 @@ public final class BluetoothGatt implements BluetoothProfile {
    }

    /**
     * Close the connection to the gatt service.
     * Close this Bluetooth GATT client.
     */
    /*package*/ void close() {
    public void close() {
        if (DBG) Log.d(TAG, "close()");

        unregisterApp();
        mConnState = CONN_STATE_CLOSED;
    }

    /**
@@ -694,7 +696,35 @@ public final class BluetoothGatt implements BluetoothProfile {
        } catch (RemoteException e) {
            Log.e(TAG,"",e);
        }
        // TBD deregister after conneciton is torn down
    }

    /**
     * Connect back to remote device.
     *
     * <p>This method is used to re-connect to a remote device after the
     * connection has been dropped. If the device is not in range, the
     * re-connection will be triggered once the device is back in range.
     *
     * @return true, if the connection attempt was initiated successfully
     */
    public boolean connect() {
        try {
            mService.clientConnect(mClientIf, mDevice.getAddress(),
                                   false); // autoConnect is inverse of "isDirect"
            return true;
        } catch (RemoteException e) {
            Log.e(TAG,"",e);
            return false;
        }
    }

    /**
     * Return the remote bluetooth device this GATT client targets to
     *
     * @return remote bluetooth device
     */
    public BluetoothDevice getDevice() {
        return mDevice;
    }

    /**
+24 −17
Original line number Diff line number Diff line
@@ -16,23 +16,22 @@

package android.bluetooth;

import android.bluetooth.BluetoothDevice;

/**
 * This abstract class is used to implement {@link BluetoothGatt} callbacks.
 */
public abstract class BluetoothGattCallback {

    /**
     * Callback indicating when a remote device has been connected or disconnected.
     * Callback indicating when GATT client has connected/disconnected to/from a remote
     * GATT server.
     *
     * @param device Remote device that has been connected or disconnected.
     * @param gatt GATT client
     * @param status Status of the connect or disconnect operation.
     * @param newState Returns the new connection state. Can be one of
     *                  {@link BluetoothProfile#STATE_DISCONNECTED} or
     *                  {@link BluetoothProfile#STATE_CONNECTED}
     */
    public void onConnectionStateChange(BluetoothDevice device, int status,
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                        int newState) {
    }

@@ -40,22 +39,23 @@ public abstract class BluetoothGattCallback {
     * Callback invoked when the list of remote services, characteristics and descriptors
     * for the remote device have been updated, ie new services have been discovered.
     *
     * @param device Remote device
     * @param gatt GATT client invoked {@link BluetoothGatt#discoverServices}
     * @param status {@link BluetoothGatt#GATT_SUCCESS} if the remote device
     *               has been explored successfully.
     */
    public void onServicesDiscovered(BluetoothDevice device, int status) {
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    }

    /**
     * Callback reporting the result of a characteristic read operation.
     *
     * @param gatt GATT client invoked {@link BluetoothGatt#readCharacteristic}
     * @param characteristic Characteristic that was read from the associated
     *                       remote device.
     * @param status {@link BluetoothGatt#GATT_SUCCESS} if the read operation
     *               was completed successfully.
     */
    public void onCharacteristicRead(BluetoothGattCharacteristic characteristic,
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                                     int status) {
    }

@@ -68,52 +68,59 @@ public abstract class BluetoothGattCallback {
     * value to the desired value to be written. If the values don't match,
     * the application must abort the reliable write transaction.
     *
     * @param gatt GATT client invoked {@link BluetoothGatt#writeCharacteristic}
     * @param characteristic Characteristic that was written to the associated
     *                       remote device.
     * @param status The result of the write operation
     */
    public void onCharacteristicWrite(BluetoothGattCharacteristic characteristic,
                                      int status) {
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic, int status) {
    }

    /**
     * Callback triggered as a result of a remote characteristic notification.
     *
     * @param gatt GATT client the characteristic is associated with
     * @param characteristic Characteristic that has been updated as a result
     *                       of a remote notification event.
     */
    public void onCharacteristicChanged(BluetoothGattCharacteristic characteristic) {
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
    }

    /**
     * Callback reporting the result of a descriptor read operation.
     *
     * @param gatt GATT client invoked {@link BluetoothGatt#readDescriptor}
     * @param descriptor Descriptor that was read from the associated
     *                   remote device.
     * @param status {@link BluetoothGatt#GATT_SUCCESS} if the read operation
     *               was completed successfully
     */
    public void onDescriptorRead(BluetoothGattDescriptor descriptor, int status) {
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
                                 int status) {
    }

    /**
     * Callback indicating the result of a descriptor write operation.
     *
     * @param gatt GATT client invoked {@link BluetoothGatt#writeDescriptor}
     * @param descriptor Descriptor that was writte to the associated
     *                   remote device.
     * @param status The result of the write operation
     */
    public void onDescriptorWrite(BluetoothGattDescriptor descriptor, int status) {
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor,
                                  int status) {
    }

    /**
     * Callback invoked when a reliable write transaction has been completed.
     *
     * @param device Remote device
     * @param gatt GATT client invoked {@link BluetoothGatt#executeReliableWrite}
     * @param status {@link BluetoothGatt#GATT_SUCCESS} if the reliable write
     *               transaction was executed successfully
     */
    public void onReliableWriteCompleted(BluetoothDevice device, int status) {
    public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
    }

    /**
@@ -122,10 +129,10 @@ public abstract class BluetoothGattCallback {
     * This callback is triggered in response to the
     * {@link BluetoothGatt#readRemoteRssi} function.
     *
     * @param device Identifies the remote device
     * @param gatt GATT client invoked {@link BluetoothGatt#readRemoteRssi}
     * @param rssi The RSSI value for the remote device
     * @param status {@link BluetoothGatt#GATT_SUCCESS} if the RSSI was read successfully
     */
    public void onReadRemoteRssi(BluetoothDevice device, int rssi, int status) {
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -22,6 +22,10 @@ import java.util.UUID;

/**
 * Represents a Bluetooth GATT Characteristic
 *
 * <p>A GATT characteristic is a basic data element used to construct a GATT service,
 * {@link BluetoothGattService}. The characteristic contains a value as well as
 * additional information and optional GATT descriptors, {@link BluetoothGattDescriptor}.
 */
public class BluetoothGattCharacteristic {

+4 −0
Original line number Diff line number Diff line
@@ -20,6 +20,10 @@ import java.util.UUID;

/**
 * Represents a Bluetooth GATT Descriptor
 *
 * <p> GATT Descriptors contain additional information and attributes of a GATT
 * characteristic, {@link BluetoothGattCharacteristic}. They can be used to describe
 * the characteristic's features or to control certain behaviours of the characteristic.
 */
public class BluetoothGattDescriptor {

Loading