Index: device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java |
diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java |
index 928d3baf654aacc92ecf66f3e9d4d6ec2579572a..67e7610dc3817b7aaa7776b6c1b4e9e490ccc328 100644 |
--- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java |
+++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java |
@@ -8,6 +8,8 @@ import android.Manifest; |
import android.annotation.TargetApi; |
import android.bluetooth.BluetoothAdapter; |
import android.bluetooth.BluetoothDevice; |
+import android.bluetooth.BluetoothGatt; |
+import android.bluetooth.BluetoothGattCallback; |
import android.bluetooth.le.BluetoothLeScanner; |
import android.bluetooth.le.ScanCallback; |
import android.bluetooth.le.ScanFilter; |
@@ -225,6 +227,12 @@ class Wrappers { |
mDevice = device; |
} |
+ public BluetoothGattWrapper connectGatt( |
+ Context context, boolean autoConnect, BluetoothGattCallbackWrapper callback) { |
+ return new BluetoothGattWrapper(mDevice.connectGatt( |
+ context, autoConnect, new ForwardBluetoothGattCallbackToWrapper(callback))); |
+ } |
+ |
public String getAddress() { |
return mDevice.getAddress(); |
} |
@@ -241,4 +249,52 @@ class Wrappers { |
return mDevice.getName(); |
} |
} |
+ |
+ /** |
+ * Wraps android.bluetooth.BluetoothGatt. |
+ */ |
+ static class BluetoothGattWrapper { |
+ private final BluetoothGatt mGatt; |
+ |
+ BluetoothGattWrapper(BluetoothGatt gatt) { |
+ mGatt = gatt; |
+ } |
+ |
+ public void disconnect() { |
+ mGatt.disconnect(); |
+ } |
+ } |
+ |
+ /** |
+ * Implements android.bluetooth.BluetoothGattCallback and forwards calls through |
+ * to a provided BluetoothGattCallbackWrapper instance. |
+ * |
+ * This class is required so that Fakes can use BluetoothGattCallbackWrapper |
+ * without it extending from BluetoothGattCallback. Fakes must function even on |
+ * Android versions where BluetoothGattCallback class is not defined. |
+ */ |
+ static class ForwardBluetoothGattCallbackToWrapper extends BluetoothGattCallback { |
+ final BluetoothGattCallbackWrapper mWrapperCallback; |
+ |
+ ForwardBluetoothGattCallbackToWrapper(BluetoothGattCallbackWrapper wrapperCallback) { |
+ mWrapperCallback = wrapperCallback; |
+ } |
+ |
+ @Override |
+ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { |
+ mWrapperCallback.onConnectionStateChange(status, newState); |
+ } |
+ } |
+ |
+ /** |
+ * Wraper alternative to android.bluetooth.BluetoothGattCallback allowing clients and Fakes to |
Jeffrey Yasskin
2015/09/16 01:40:49
sp: Wraper
scheib
2015/09/16 21:55:17
Done.
|
+ * work on older SDK versions without having a dependency on the class not defined there. |
+ * |
+ * BluetoothGatt gatt parameters are omitted from methods as each call would |
+ * need to wrap them in a BluetoothGattWrapper. Client code should cache the |
+ * BluetoothGattWrapper provided on the initial BluetoothDeviceWrapper.connectGatt call. |
+ */ |
+ abstract static class BluetoothGattCallbackWrapper { |
+ public abstract void onConnectionStateChange(int status, int newState); |
+ } |
} |