| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.device.bluetooth; | 5 package org.chromium.device.bluetooth; |
| 6 | 6 |
| 7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
| 8 import android.bluetooth.BluetoothDevice; | 8 import android.bluetooth.BluetoothDevice; |
| 9 import android.bluetooth.le.ScanFilter; | 9 import android.bluetooth.le.ScanFilter; |
| 10 import android.bluetooth.le.ScanSettings; | 10 import android.bluetooth.le.ScanSettings; |
| 11 import android.content.Context; | 11 import android.content.Context; |
| 12 import android.os.Build; | 12 import android.os.Build; |
| 13 import android.os.ParcelUuid; | 13 import android.os.ParcelUuid; |
| 14 | 14 |
| 15 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
| 16 import org.chromium.base.annotations.CalledByNative; | 16 import org.chromium.base.annotations.CalledByNative; |
| 17 import org.chromium.base.annotations.JNINamespace; | 17 import org.chromium.base.annotations.JNINamespace; |
| 18 | 18 |
| 19 import java.util.ArrayList; | 19 import java.util.ArrayList; |
| 20 import java.util.HashMap; |
| 20 import java.util.List; | 21 import java.util.List; |
| 22 import java.util.UUID; |
| 21 | 23 |
| 22 /** | 24 /** |
| 23 * Fake implementations of android.bluetooth.* classes for testing. | 25 * Fake implementations of android.bluetooth.* classes for testing. |
| 24 */ | 26 */ |
| 25 @JNINamespace("device") | 27 @JNINamespace("device") |
| 26 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 28 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 27 class Fakes { | 29 class Fakes { |
| 28 private static final String TAG = "cr.Bluetooth"; | 30 private static final String TAG = "cr.Bluetooth"; |
| 29 | 31 |
| 30 /** | 32 /** |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 ChromeBluetoothDevice chromeDevice, int status, boolean connecte
d) { | 207 ChromeBluetoothDevice chromeDevice, int status, boolean connecte
d) { |
| 206 FakeBluetoothDevice fakeDevice = (FakeBluetoothDevice) chromeDevice.
mDevice; | 208 FakeBluetoothDevice fakeDevice = (FakeBluetoothDevice) chromeDevice.
mDevice; |
| 207 fakeDevice.mGattCallback.onConnectionStateChange(status, connected | 209 fakeDevice.mGattCallback.onConnectionStateChange(status, connected |
| 208 ? android.bluetooth.BluetoothProfile.STATE_CONNECTED | 210 ? android.bluetooth.BluetoothProfile.STATE_CONNECTED |
| 209 : android.bluetooth.BluetoothProfile.STATE_DISCONNEC
TED); | 211 : android.bluetooth.BluetoothProfile.STATE_DISCONNEC
TED); |
| 210 } | 212 } |
| 211 | 213 |
| 212 // Create a call to onServicesDiscovered on the |chrome_device| using pa
rameter | 214 // Create a call to onServicesDiscovered on the |chrome_device| using pa
rameter |
| 213 // |status|. | 215 // |status|. |
| 214 @CalledByNative("FakeBluetoothDevice") | 216 @CalledByNative("FakeBluetoothDevice") |
| 215 private static void servicesDiscovered(ChromeBluetoothDevice chromeDevic
e, int status) { | 217 private static void servicesDiscovered( |
| 218 ChromeBluetoothDevice chromeDevice, int status, String uuidsSpac
eDelimited) { |
| 216 FakeBluetoothDevice fakeDevice = (FakeBluetoothDevice) chromeDevice.
mDevice; | 219 FakeBluetoothDevice fakeDevice = (FakeBluetoothDevice) chromeDevice.
mDevice; |
| 217 | 220 |
| 218 // TODO(scheib): Add more control over how many services are created
and | |
| 219 // their properties. http://crbug.com/541400 | |
| 220 if (status == android.bluetooth.BluetoothGatt.GATT_SUCCESS) { | 221 if (status == android.bluetooth.BluetoothGatt.GATT_SUCCESS) { |
| 221 fakeDevice.mGatt.mServices.clear(); | 222 fakeDevice.mGatt.mServices.clear(); |
| 222 fakeDevice.mGatt.mServices.add(new FakeBluetoothGattService(0)); | 223 HashMap<String, Integer> uuidsToInstanceIdMap = new HashMap<Stri
ng, Integer>(); |
| 223 fakeDevice.mGatt.mServices.add(new FakeBluetoothGattService(1)); | 224 for (String uuid : uuidsSpaceDelimited.split(" ")) { |
| 225 Integer previousId = uuidsToInstanceIdMap.get(uuid); |
| 226 int instanceId = (previousId == null) ? 0 : previousId + 1; |
| 227 uuidsToInstanceIdMap.put(uuid, instanceId); |
| 228 fakeDevice.mGatt.mServices.add( |
| 229 new FakeBluetoothGattService(UUID.fromString(uuid),
instanceId)); |
| 230 } |
| 224 } | 231 } |
| 225 | 232 |
| 226 fakeDevice.mGattCallback.onServicesDiscovered(status); | 233 fakeDevice.mGattCallback.onServicesDiscovered(status); |
| 227 } | 234 } |
| 228 | 235 |
| 229 // ---------------------------------------------------------------------
-------------------- | 236 // ---------------------------------------------------------------------
-------------------- |
| 230 // Wrappers.BluetoothDeviceWrapper overrides: | 237 // Wrappers.BluetoothDeviceWrapper overrides: |
| 231 | 238 |
| 232 @Override | 239 @Override |
| 233 public Wrappers.BluetoothGattWrapper connectGatt(Context context, boolea
n autoConnect, | 240 public Wrappers.BluetoothGattWrapper connectGatt(Context context, boolea
n autoConnect, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 public List<Wrappers.BluetoothGattServiceWrapper> getServices() { | 297 public List<Wrappers.BluetoothGattServiceWrapper> getServices() { |
| 291 return mServices; | 298 return mServices; |
| 292 } | 299 } |
| 293 } | 300 } |
| 294 | 301 |
| 295 /** | 302 /** |
| 296 * Fakes android.bluetooth.BluetoothGattService. | 303 * Fakes android.bluetooth.BluetoothGattService. |
| 297 */ | 304 */ |
| 298 static class FakeBluetoothGattService extends Wrappers.BluetoothGattServiceW
rapper { | 305 static class FakeBluetoothGattService extends Wrappers.BluetoothGattServiceW
rapper { |
| 299 final int mInstanceId; | 306 final int mInstanceId; |
| 307 final UUID mUuid; |
| 300 | 308 |
| 301 public FakeBluetoothGattService(int instanceId) { | 309 public FakeBluetoothGattService(UUID uuid, int instanceId) { |
| 302 super(null); | 310 super(null); |
| 311 mUuid = uuid; |
| 303 mInstanceId = instanceId; | 312 mInstanceId = instanceId; |
| 304 } | 313 } |
| 305 | 314 |
| 306 @Override | 315 @Override |
| 307 public int getInstanceId() { | 316 public int getInstanceId() { |
| 308 return mInstanceId; | 317 return mInstanceId; |
| 309 } | 318 } |
| 319 |
| 320 @Override |
| 321 public UUID getUuid() { |
| 322 return mUuid; |
| 323 } |
| 310 } | 324 } |
| 311 | 325 |
| 312 // -------------------------------------------------------------------------
-------------------- | 326 // -------------------------------------------------------------------------
-------------------- |
| 313 // BluetoothTestAndroid C++ methods declared for access from java: | 327 // BluetoothTestAndroid C++ methods declared for access from java: |
| 314 | 328 |
| 315 // Binds to BluetoothAdapterAndroid::OnFakeBluetoothDeviceConnectGattCalled. | 329 // Binds to BluetoothAdapterAndroid::OnFakeBluetoothDeviceConnectGattCalled. |
| 316 private static native void nativeOnFakeBluetoothDeviceConnectGattCalled( | 330 private static native void nativeOnFakeBluetoothDeviceConnectGattCalled( |
| 317 long nativeBluetoothTestAndroid); | 331 long nativeBluetoothTestAndroid); |
| 318 | 332 |
| 319 // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattDisconnect. | 333 // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattDisconnect. |
| 320 private static native void nativeOnFakeBluetoothGattDisconnect(long nativeBl
uetoothTestAndroid); | 334 private static native void nativeOnFakeBluetoothGattDisconnect(long nativeBl
uetoothTestAndroid); |
| 321 | 335 |
| 322 // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattDiscoverServices. | 336 // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattDiscoverServices. |
| 323 private static native void nativeOnFakeBluetoothGattDiscoverServices( | 337 private static native void nativeOnFakeBluetoothGattDiscoverServices( |
| 324 long nativeBluetoothTestAndroid); | 338 long nativeBluetoothTestAndroid); |
| 325 } | 339 } |
| OLD | NEW |