diff options
author | scheib <scheib@chromium.org> | 2015-10-27 18:18:46 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-10-28 01:19:44 +0000 |
commit | 593a988fdef81c7679fc2b4c1e20349eb25fd5c4 (patch) | |
tree | 67e86a75c852232ec198acb88706a6b250a81631 | |
parent | 48480e6156b9f6275496b9b6a30f471df4be1d3e (diff) | |
download | chromium_src-593a988fdef81c7679fc2b4c1e20349eb25fd5c4.zip chromium_src-593a988fdef81c7679fc2b4c1e20349eb25fd5c4.tar.gz chromium_src-593a988fdef81c7679fc2b4c1e20349eb25fd5c4.tar.bz2 |
bluetooth: android: BluetoothRemoteGattCharacteristicAndroid::GetUUID.
Java object ChromeBluetoothRemoteGattCharacteristic created as well.
BUG=545682
Review URL: https://codereview.chromium.org/1422093002
Cr-Commit-Position: refs/heads/master@{#356474}
9 files changed, 153 insertions, 22 deletions
diff --git a/device/bluetooth/BUILD.gn b/device/bluetooth/BUILD.gn index f1b7bc9..25dfc44 100644 --- a/device/bluetooth/BUILD.gn +++ b/device/bluetooth/BUILD.gn @@ -202,6 +202,7 @@ if (is_android) { java_sources_needing_jni = [ "android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java", "android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java", + "android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java", "android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattService.java", "android/java/src/org/chromium/device/bluetooth/Wrappers.java", ] diff --git a/device/bluetooth/android/bluetooth_jni_registrar.cc b/device/bluetooth/android/bluetooth_jni_registrar.cc index 82b9894..08aca21 100644 --- a/device/bluetooth/android/bluetooth_jni_registrar.cc +++ b/device/bluetooth/android/bluetooth_jni_registrar.cc @@ -9,6 +9,7 @@ #include "device/bluetooth/android/wrappers.h" #include "device/bluetooth/bluetooth_adapter_android.h" #include "device/bluetooth/bluetooth_device_android.h" +#include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" namespace device { @@ -18,6 +19,8 @@ namespace { const base::android::RegistrationMethod kRegisteredMethods[] = { {"BluetoothAdapterAndroid", device::BluetoothAdapterAndroid::RegisterJNI}, {"BluetoothDeviceAndroid", device::BluetoothDeviceAndroid::RegisterJNI}, + {"BluetoothRemoteGattCharacteristicAndroid", + device::BluetoothRemoteGattCharacteristicAndroid::RegisterJNI}, {"BluetoothRemoteGattServiceAndroid", device::BluetoothRemoteGattServiceAndroid::RegisterJNI}, {"Wrappers", device::WrappersRegisterJNI}, diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java new file mode 100644 index 0000000..fbb7962 --- /dev/null +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java @@ -0,0 +1,47 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.device.bluetooth; + +import org.chromium.base.Log; +import org.chromium.base.annotations.CalledByNative; +import org.chromium.base.annotations.JNINamespace; + +/** + * Exposes android.bluetooth.BluetoothGattCharacteristic as necessary + * for C++ device::BluetoothRemoteGattCharacteristicAndroid. + * + * Lifetime is controlled by + * device::BluetoothRemoteGattCharacteristicAndroid. + */ +@JNINamespace("device") +final class ChromeBluetoothRemoteGattCharacteristic { + private static final String TAG = "Bluetooth"; + + final Wrappers.BluetoothGattCharacteristicWrapper mCharacteristic; + + private ChromeBluetoothRemoteGattCharacteristic( + Wrappers.BluetoothGattCharacteristicWrapper characteristicWrapper) { + mCharacteristic = characteristicWrapper; + Log.v(TAG, "ChromeBluetoothRemoteGattCharacteristic created."); + } + + // --------------------------------------------------------------------------------------------- + // BluetoothRemoteGattCharacteristicAndroid methods implemented in java: + + // Implements BluetoothRemoteGattCharacteristicAndroid::Create. + // 'Object' type must be used because inner class Wrappers.BluetoothGattCharacteristicWrapper + // reference is not handled by jni_generator.py JavaToJni. http://crbug.com/505554 + @CalledByNative + private static ChromeBluetoothRemoteGattCharacteristic create(Object characteristicWrapper) { + return new ChromeBluetoothRemoteGattCharacteristic( + (Wrappers.BluetoothGattCharacteristicWrapper) characteristicWrapper); + } + + // Implements BluetoothRemoteGattCharacteristicAndroid::GetUUID. + @CalledByNative + private String getUUID() { + return mCharacteristic.getUuid().toString(); + } +} diff --git a/device/bluetooth/bluetooth.gyp b/device/bluetooth/bluetooth.gyp index e9fa47c..f862660 100644 --- a/device/bluetooth/bluetooth.gyp +++ b/device/bluetooth/bluetooth.gyp @@ -258,6 +258,7 @@ 'sources': [ 'android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java', 'android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java', + 'android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java', 'android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattService.java', 'android/java/src/org/chromium/device/bluetooth/Wrappers.java', ], diff --git a/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc b/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc index 132a918..e7e412b 100644 --- a/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc +++ b/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc @@ -2,9 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "device/bluetooth/bluetooth_gatt_service.h" +#include "device/bluetooth/bluetooth_gatt_characteristic.h" -#include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" +#include "device/bluetooth/bluetooth_gatt_service.h" #include "testing/gtest/include/gtest/gtest.h" #if defined(OS_ANDROID) @@ -82,4 +82,42 @@ TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) { } #endif // defined(OS_ANDROID) +#if defined(OS_ANDROID) +TEST_F(BluetoothGattCharacteristicTest, GetUUID) { + InitWithFakeAdapter(); + StartLowEnergyDiscoverySession(); + BluetoothDevice* device = DiscoverLowEnergyDevice(3); + device->CreateGattConnection(GetGattConnectionCallback(), + GetConnectErrorCallback()); + SimulateGattConnection(device); + std::vector<std::string> services; + services.push_back("00000000-0000-1000-8000-00805f9b34fb"); + SimulateGattServicesDiscovered(device, services); + BluetoothGattService* service = device->GetGattServices()[0]; + + // Create 3 characteristics. Two of them are duplicates. + std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); + std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); + BluetoothUUID uuid1(uuid_str1); + BluetoothUUID uuid2(uuid_str2); + SimulateGattCharacteristic(service, uuid_str1); + SimulateGattCharacteristic(service, uuid_str2); + SimulateGattCharacteristic(service, uuid_str2); + BluetoothGattCharacteristic* char1 = service->GetCharacteristics()[0]; + BluetoothGattCharacteristic* char2 = service->GetCharacteristics()[1]; + BluetoothGattCharacteristic* char3 = service->GetCharacteristics()[2]; + + // Swap as needed to have char1 point to the the characteristic with uuid1. + if (char2->GetUUID() == uuid1) { + std::swap(char1, char2); + } else if (char3->GetUUID() == uuid1) { + std::swap(char1, char3); + } + + EXPECT_EQ(uuid1, char1->GetUUID()); + EXPECT_EQ(uuid2, char2->GetUUID()); + EXPECT_EQ(uuid2, char3->GetUUID()); +} +#endif // defined(OS_ANDROID) + } // namespace device diff --git a/device/bluetooth/bluetooth_gatt_service_unittest.cc b/device/bluetooth/bluetooth_gatt_service_unittest.cc index c3816f0..c435fba 100644 --- a/device/bluetooth/bluetooth_gatt_service_unittest.cc +++ b/device/bluetooth/bluetooth_gatt_service_unittest.cc @@ -4,7 +4,7 @@ #include "device/bluetooth/bluetooth_gatt_service.h" -#include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" +#include "device/bluetooth/bluetooth_gatt_characteristic.h" #include "testing/gtest/include/gtest/gtest.h" #if defined(OS_ANDROID) @@ -112,20 +112,30 @@ TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { services.push_back("00000000-0000-1000-8000-00805f9b34fb"); SimulateGattServicesDiscovered(device, services); BluetoothGattService* service = device->GetGattServices()[0]; - std::string characteristic_uuid1 = "00000001-0000-1000-8000-00805f9b34fb"; - std::string characteristic_uuid2 = "00000002-0000-1000-8000-00805f9b34fb"; + std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; + std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. + std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; SimulateGattCharacteristic(service, characteristic_uuid1); SimulateGattCharacteristic(service, characteristic_uuid2); SimulateGattCharacteristic(service, characteristic_uuid3); + SimulateGattCharacteristic(service, characteristic_uuid4); - EXPECT_EQ(3u, service->GetCharacteristics().size()); + // Verify that GetCharacteristic can retrieve characteristics again by ID, + // and that the same Characteristics come back. + EXPECT_EQ(4u, service->GetCharacteristics().size()); std::string char_id1 = service->GetCharacteristics()[0]->GetIdentifier(); std::string char_id2 = service->GetCharacteristics()[1]->GetIdentifier(); std::string char_id3 = service->GetCharacteristics()[2]->GetIdentifier(); - EXPECT_TRUE(service->GetCharacteristic(char_id1)); - EXPECT_TRUE(service->GetCharacteristic(char_id2)); - EXPECT_TRUE(service->GetCharacteristic(char_id3)); + std::string char_id4 = service->GetCharacteristics()[3]->GetIdentifier(); + BluetoothUUID char_uuid1 = service->GetCharacteristics()[0]->GetUUID(); + BluetoothUUID char_uuid2 = service->GetCharacteristics()[1]->GetUUID(); + BluetoothUUID char_uuid3 = service->GetCharacteristics()[2]->GetUUID(); + BluetoothUUID char_uuid4 = service->GetCharacteristics()[3]->GetUUID(); + EXPECT_EQ(char_uuid1, service->GetCharacteristic(char_id1)->GetUUID()); + EXPECT_EQ(char_uuid2, service->GetCharacteristic(char_id2)->GetUUID()); + EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); + EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); } #endif // defined(OS_ANDROID) diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc index 5286baa..aa85463 100644 --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc @@ -4,28 +4,48 @@ #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" +#include "base/android/jni_android.h" +#include "base/android/jni_string.h" #include "base/logging.h" +#include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" + +using base::android::AttachCurrentThread; namespace device { // static scoped_ptr<BluetoothRemoteGattCharacteristicAndroid> BluetoothRemoteGattCharacteristicAndroid::Create( - const std::string& instanceId) { - return make_scoped_ptr<BluetoothRemoteGattCharacteristicAndroid>( + const std::string& instanceId, + jobject /* BluetoothGattCharacteristicWrapper */ + bluetooth_gatt_characteristic_wrapper) { + scoped_ptr<BluetoothRemoteGattCharacteristicAndroid> characteristic( new BluetoothRemoteGattCharacteristicAndroid(instanceId)); + + characteristic->j_characteristic_.Reset( + Java_ChromeBluetoothRemoteGattCharacteristic_create( + AttachCurrentThread(), bluetooth_gatt_characteristic_wrapper)); + + return characteristic; } BluetoothRemoteGattCharacteristicAndroid:: ~BluetoothRemoteGattCharacteristicAndroid() {} +// static +bool BluetoothRemoteGattCharacteristicAndroid::RegisterJNI(JNIEnv* env) { + return RegisterNativesImpl( + env); // Generated in ChromeBluetoothRemoteGattCharacteristic_jni.h +} + std::string BluetoothRemoteGattCharacteristicAndroid::GetIdentifier() const { return instanceId_; } BluetoothUUID BluetoothRemoteGattCharacteristicAndroid::GetUUID() const { - NOTIMPLEMENTED(); - return BluetoothUUID(); + return device::BluetoothUUID(ConvertJavaStringToUTF8( + Java_ChromeBluetoothRemoteGattCharacteristic_getUUID( + AttachCurrentThread(), j_characteristic_.obj()))); } bool BluetoothRemoteGattCharacteristicAndroid::IsLocal() const { diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h index 7414977c..a97160f 100644 --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h @@ -5,6 +5,7 @@ #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_ANDROID_H_ #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_ANDROID_H_ +#include "base/android/jni_android.h" #include "base/macros.h" #include "device/bluetooth/bluetooth_gatt_characteristic.h" @@ -16,19 +17,24 @@ namespace device { class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid : public BluetoothGattCharacteristic { public: - // Create a BluetoothRemoteGattServiceAndroid instance and associated Java - // ChromeBluetoothRemoteGattService using the provided - // |bluetooth_remote_gatt_service_wrapper|. + // Create a BluetoothRemoteGattCharacteristicAndroid instance and associated + // Java + // ChromeBluetoothRemoteGattCharacteristic using the provided + // |bluetooth_gatt_characteristic_wrapper|. // - // The ChromeBluetoothRemoteGattService instance will hold a Java reference - // to |bluetooth_remote_gatt_service_wrapper|. - // - // TODO(scheib): Actually create the Java object. crbug.com/545682 + // The ChromeBluetoothRemoteGattCharacteristic instance will hold a Java + // reference + // to |bluetooth_gatt_characteristic_wrapper|. static scoped_ptr<BluetoothRemoteGattCharacteristicAndroid> Create( - const std::string& instanceId); + const std::string& instanceId, + jobject /* BluetoothGattCharacteristicWrapper */ + bluetooth_gatt_characteristic_wrapper); ~BluetoothRemoteGattCharacteristicAndroid() override; + // Register C++ methods exposed to Java using JNI. + static bool RegisterJNI(JNIEnv* env); + // BluetoothGattCharacteristic interface: std::string GetIdentifier() const override; BluetoothUUID GetUUID() const override; @@ -54,6 +60,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid private: BluetoothRemoteGattCharacteristicAndroid(const std::string& instanceId); + // Java object + // org.chromium.device.bluetooth.ChromeBluetoothRemoteGattCharacteristic. + base::android::ScopedJavaGlobalRef<jobject> j_characteristic_; + // Adapter unique instance ID. std::string instanceId_; diff --git a/device/bluetooth/bluetooth_remote_gatt_service_android.cc b/device/bluetooth/bluetooth_remote_gatt_service_android.cc index ed503d4..01e01a6 100644 --- a/device/bluetooth/bluetooth_remote_gatt_service_android.cc +++ b/device/bluetooth/bluetooth_remote_gatt_service_android.cc @@ -133,7 +133,8 @@ void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic( characteristics_.set( instanceIdString, - BluetoothRemoteGattCharacteristicAndroid::Create(instanceIdString)); + BluetoothRemoteGattCharacteristicAndroid::Create( + instanceIdString, bluetooth_gatt_characteristic_wrapper)); } BluetoothRemoteGattServiceAndroid::BluetoothRemoteGattServiceAndroid( |