Skip to content

Commit 251e249

Browse files
Subramanian SrinivasanMocaRafee
authored andcommitted
Enhanced Attribute protocol support
GATT client and server SDK APIs support for Enhanced Attribute protocol. CRs-Fixed: 2803585 Change-Id: I900e8829866323f57b9b5d9bcf93601e5f10a80e
1 parent ca6c833 commit 251e249

4 files changed

Lines changed: 149 additions & 6 deletions

File tree

core/java/android/bluetooth/BluetoothDevice.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,48 @@ public BluetoothGatt connectGatt(Context context, boolean autoConnect,
23552355
public BluetoothGatt connectGatt(Context context, boolean autoConnect,
23562356
BluetoothGattCallback callback, int transport,
23572357
boolean opportunistic, int phy, Handler handler) {
2358+
return connectGatt(context, autoConnect, callback, transport, opportunistic,
2359+
phy, handler, false);
2360+
}
2361+
2362+
/**
2363+
* Connect to GATT Server hosted by this device. Caller acts as GATT client.
2364+
* The callback is used to deliver results to Caller, such as connection status as well
2365+
* as any further GATT client operations.
2366+
* The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct
2367+
* GATT client operations.
2368+
*
2369+
* @param callback GATT callback handler that will receive asynchronous callbacks.
2370+
* @param autoConnect Whether to directly connect to the remote device (false) or to
2371+
* automatically connect as soon as the remote device becomes available (true).
2372+
* @param transport preferred transport for GATT connections to remote dual-mode devices {@link
2373+
* BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link
2374+
* BluetoothDevice#TRANSPORT_LE}
2375+
* @param opportunistic Whether this GATT client is opportunistic. An opportunistic GATT client
2376+
* does not hold a GATT connection. It automatically disconnects when no other GATT connections
2377+
* are active for the remote device.
2378+
* @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of {@link
2379+
* BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, an d{@link
2380+
* BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if {@code autoConnect}
2381+
* is set to true.
2382+
* @param handler The handler to use for the callback. If {@code null}, callbacks will happen on
2383+
* an un-specified background thread.
2384+
* @param eattSupport specifies whether client app needs EATT channel for client operations.
2385+
* If both local and remote devices support EATT and local app asks for EATT, GATT client
2386+
* operations will be performed using EATT channel.
2387+
* If either local or remote device doesn't support EATT but local App asks for EATT, GATT
2388+
* client operations will be performed using unenhanced ATT channel.
2389+
*
2390+
* @return A BluetoothGatt instance. You can use BluetoothGatt to conduct GATT client
2391+
* operations.
2392+
*
2393+
* @throws NullPointerException if callback is null
2394+
*
2395+
* @hide
2396+
*/
2397+
public BluetoothGatt connectGatt(Context context, boolean autoConnect,
2398+
BluetoothGattCallback callback, int transport, boolean opportunistic,
2399+
int phy, Handler handler, boolean eattSupport) {
23582400
if (callback == null) {
23592401
throw new NullPointerException("callback is null");
23602402
}
@@ -2370,7 +2412,7 @@ public BluetoothGatt connectGatt(Context context, boolean autoConnect,
23702412
return null;
23712413
}
23722414
BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, opportunistic, phy);
2373-
gatt.connect(autoConnect, callback, handler);
2415+
gatt.connect(autoConnect, callback, handler, eattSupport);
23742416
return gatt;
23752417
} catch (RemoteException e) {
23762418
Log.e(TAG, "", e);

core/java/android/bluetooth/BluetoothGatt.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,28 @@ private void runOrQueueCallback(final Runnable cb) {
799799
* error
800800
*/
801801
private boolean registerApp(BluetoothGattCallback callback, Handler handler) {
802+
return registerApp(callback, handler, false);
803+
}
804+
805+
/**
806+
* Register an application callback to start using GATT.
807+
*
808+
* <p>This is an asynchronous call. The callback {@link BluetoothGattCallback#onAppRegistered}
809+
* is used to notify success or failure if the function returns true.
810+
*
811+
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
812+
*
813+
* @param callback GATT callback handler that will receive asynchronous callbacks.
814+
* @param eattSupport specifies whether client app needs EATT channel for client operations.
815+
* If both local and remote devices support EATT and local app asks for EATT, GATT client
816+
* operations will be performed using EATT channel.
817+
* If either local or remote device doesn't support EATT but local App asks for EATT, GATT
818+
* client operations will be performed using unenhanced ATT channel.
819+
* @return If true, the callback will be called to notify success or failure, false on immediate
820+
* error
821+
* @hide
822+
*/
823+
private boolean registerApp(BluetoothGattCallback callback, Handler handler, boolean eattSupport) {
802824
if (DBG) Log.d(TAG, "registerApp()");
803825
if (mService == null) return false;
804826

@@ -808,7 +830,7 @@ private boolean registerApp(BluetoothGattCallback callback, Handler handler) {
808830
if (DBG) Log.d(TAG, "registerApp() - UUID=" + uuid);
809831

810832
try {
811-
mService.registerClient(new ParcelUuid(uuid), mBluetoothGattCallback);
833+
mService.registerClient(new ParcelUuid(uuid), mBluetoothGattCallback, eattSupport);
812834
} catch (RemoteException e) {
813835
Log.e(TAG, "", e);
814836
return false;
@@ -859,9 +881,45 @@ private void unregisterApp() {
859881
@UnsupportedAppUsage
860882
/*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback,
861883
Handler handler) {
884+
return connect(autoConnect, callback, handler, false);
885+
}
886+
887+
/**
888+
* Initiate a connection to a Bluetooth GATT capable device.
889+
*
890+
* <p>The connection may not be established right away, but will be
891+
* completed when the remote device is available. A
892+
* {@link BluetoothGattCallback#onConnectionStateChange} callback will be
893+
* invoked when the connection state changes as a result of this function.
894+
*
895+
* <p>The autoConnect parameter determines whether to actively connect to
896+
* the remote device, or rather passively scan and finalize the connection
897+
* when the remote device is in range/available. Generally, the first ever
898+
* connection to a device should be direct (autoConnect set to false) and
899+
* subsequent connections to known devices should be invoked with the
900+
* autoConnect parameter set to true.
901+
*
902+
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
903+
*
904+
* @param device Remote device to connect to
905+
* @param autoConnect Whether to directly connect to the remote device (false) or to
906+
* automatically connect as soon as the remote device becomes available (true).
907+
* @param eattSupport specifies whether client app needs EATT channel for client operations.
908+
* If both local and remote devices support EATT and local app asks for EATT, GATT client
909+
* operations will be performed using EATT channel.
910+
* If either local or remote device doesn't support EATT but local App asks for EATT, GATT
911+
* client operations will be performed using unenhanced ATT channel.
912+
* @return true, if the connection attempt was initiated successfully
913+
*
914+
* @hide
915+
*/
916+
@UnsupportedAppUsage
917+
/*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback,
918+
Handler handler, boolean eattSupport) {
862919
if (DBG) {
863920
Log.d(TAG,
864-
"connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect);
921+
"connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect
922+
+ ", eattSupport: " + eattSupport);
865923
}
866924
synchronized (mStateLock) {
867925
if (mConnState != CONN_STATE_IDLE) {
@@ -872,7 +930,7 @@ private void unregisterApp() {
872930

873931
mAutoConnect = autoConnect;
874932

875-
if (!registerApp(callback, handler)) {
933+
if (!registerApp(callback, handler, eattSupport)) {
876934
synchronized (mStateLock) {
877935
mConnState = CONN_STATE_IDLE;
878936
}

core/java/android/bluetooth/BluetoothGattServer.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,24 @@ public void close() {
443443
* error
444444
*/
445445
/*package*/ boolean registerCallback(BluetoothGattServerCallback callback) {
446+
return registerCallback(callback, false);
447+
}
448+
449+
/**
450+
* Register an application callback to start using GattServer.
451+
*
452+
* <p>This is an asynchronous call. The callback is used to notify
453+
* success or failure if the function returns true.
454+
*
455+
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
456+
*
457+
* @param callback GATT callback handler that will receive asynchronous callbacks.
458+
* @param eattSupport whether eattSupport is needed for Gatt server
459+
* @return true, the callback will be called to notify success or failure, false on immediate
460+
* error
461+
* @hide
462+
*/
463+
/*package*/ boolean registerCallback(BluetoothGattServerCallback callback, boolean eattSupport) {
446464
if (DBG) Log.d(TAG, "registerCallback()");
447465
if (mService == null) {
448466
Log.e(TAG, "GATT service not available");
@@ -459,7 +477,7 @@ public void close() {
459477

460478
mCallback = callback;
461479
try {
462-
mService.registerServer(new ParcelUuid(uuid), mBluetoothGattServerCallback);
480+
mService.registerServer(new ParcelUuid(uuid), mBluetoothGattServerCallback, eattSupport);
463481
} catch (RemoteException e) {
464482
Log.e(TAG, "", e);
465483
mCallback = null;

core/java/android/bluetooth/BluetoothManager.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,31 @@ public BluetoothGattServer openGattServer(Context context,
233233
*/
234234
public BluetoothGattServer openGattServer(Context context,
235235
BluetoothGattServerCallback callback, int transport) {
236+
return (openGattServer(context, callback, transport, false));
237+
}
238+
239+
/**
240+
* Open a GATT Server
241+
* The callback is used to deliver results to Caller, such as connection status as well
242+
* as the results of any other GATT server operations.
243+
* The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
244+
* to conduct GATT server operations.
245+
*
246+
* @param context App context
247+
* @param callback GATT server callback handler that will receive asynchronous callbacks.
248+
* @param transport preferred transport for GATT connections to remote dual-mode devices {@link
249+
* BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link
250+
* BluetoothDevice#TRANSPORT_LE}
251+
* @param eattSupport specifies whether server app needs EATT channel for server operations.
252+
* If both local and remote devices support EATT, local app asks for EATT using this API and
253+
* calls server connect, GATT server operations will be performed using EATT channel.
254+
* If either local or remote device doesn't support EATT but local App asks for EATT, GATT
255+
* server operations will be performed using unenhanced ATT channel.
256+
* @return BluetoothGattServer instance
257+
* @hide
258+
*/
259+
public BluetoothGattServer openGattServer(Context context,
260+
BluetoothGattServerCallback callback, int transport, boolean eattSupport) {
236261
if (context == null || callback == null) {
237262
throw new IllegalArgumentException("null parameter: " + context + " " + callback);
238263
}
@@ -248,7 +273,7 @@ public BluetoothGattServer openGattServer(Context context,
248273
return null;
249274
}
250275
BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt, transport);
251-
Boolean regStatus = mGattServer.registerCallback(callback);
276+
Boolean regStatus = mGattServer.registerCallback(callback, eattSupport);
252277
return regStatus ? mGattServer : null;
253278
} catch (RemoteException e) {
254279
Log.e(TAG, "", e);

0 commit comments

Comments
 (0)