diff options
author | scheib <scheib@chromium.org> | 2015-10-26 22:40:12 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-10-27 05:41:41 +0000 |
commit | dd941ba40bbd76375e6fe43bb55aef55e9d3c282 (patch) | |
tree | e294d098862552fa1e68af9335904184fc560540 | |
parent | a8391ce197da44437521b0f4968f1c547dd13451 (diff) | |
download | chromium_src-dd941ba40bbd76375e6fe43bb55aef55e9d3c282.zip chromium_src-dd941ba40bbd76375e6fe43bb55aef55e9d3c282.tar.gz chromium_src-dd941ba40bbd76375e6fe43bb55aef55e9d3c282.tar.bz2 |
bluetooth: android: Implement Characteristic GetIdentifier, fix Service IDs too.
BluetoothGattCharacteristic::GetIdentifier is implemented.
BluetoothGattService::GetIdentifier is fixed to produce an adapter unique value.
BUG=545682, 546747
Review URL: https://codereview.chromium.org/1412963004
Cr-Commit-Position: refs/heads/master@{#356243}
14 files changed, 197 insertions, 59 deletions
diff --git a/device/BUILD.gn b/device/BUILD.gn index a7d91ed..bb27c5a 100644 --- a/device/BUILD.gn +++ b/device/BUILD.gn @@ -40,6 +40,7 @@ test("device_unittests") { "bluetooth/bluetooth_device_unittest.cc", "bluetooth/bluetooth_device_win_unittest.cc", "bluetooth/bluetooth_discovery_filter_unittest.cc", + "bluetooth/bluetooth_gatt_characteristic_unittest.cc", "bluetooth/bluetooth_gatt_chromeos_unittest.cc", "bluetooth/bluetooth_gatt_service_unittest.cc", "bluetooth/bluetooth_low_energy_win_unittest.cc", @@ -169,7 +170,10 @@ test("device_unittests") { # OSes older than OSX 10.10, the top level CoreBluetooth framework must be # weakly linked. if (mac_sdk_version == "10.10") { - ldflags += [ "-weak_framework", "CoreBluetooth" ] + ldflags += [ + "-weak_framework", + "CoreBluetooth", + ] } } } diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java index 382f75c..7d60844 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothDevice.java @@ -160,12 +160,10 @@ final class ChromeBluetoothDevice { if (mNativeBluetoothDeviceAndroid != 0) { for (Wrappers.BluetoothGattServiceWrapper service : mBluetoothGatt.getServices()) { - // Create a device unique service ID. getInstanceId only differs - // between service instances with the same UUID. - // TODO(scheib): Make instance IDs unique to the whole adapter. - // http://crbug.com/546747 - String serviceInstanceId = - service.getUuid().toString() + service.getInstanceId(); + // Create an adapter unique service ID. getInstanceId only differs + // between service instances with the same UUID on this device. + String serviceInstanceId = getAddress() + "/" + + service.getUuid().toString() + "," + service.getInstanceId(); nativeCreateGattRemoteService( mNativeBluetoothDeviceAndroid, serviceInstanceId, service); } diff --git a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattService.java b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattService.java index 2d9a0e4..86a79e8 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattService.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattService.java @@ -23,11 +23,13 @@ final class ChromeBluetoothRemoteGattService { private long mNativeBluetoothRemoteGattServiceAndroid; final Wrappers.BluetoothGattServiceWrapper mService; + final String mInstanceId; private ChromeBluetoothRemoteGattService(long nativeBluetoothRemoteGattServiceAndroid, - Wrappers.BluetoothGattServiceWrapper serviceWrapper) { + Wrappers.BluetoothGattServiceWrapper serviceWrapper, String instanceId) { mNativeBluetoothRemoteGattServiceAndroid = nativeBluetoothRemoteGattServiceAndroid; mService = serviceWrapper; + mInstanceId = instanceId; Log.v(TAG, "ChromeBluetoothRemoteGattService created."); } @@ -47,9 +49,10 @@ final class ChromeBluetoothRemoteGattService { // is not handled by jni_generator.py JavaToJni. http://crbug.com/505554 @CalledByNative private static ChromeBluetoothRemoteGattService create( - long nativeBluetoothRemoteGattServiceAndroid, Object serviceWrapper) { + long nativeBluetoothRemoteGattServiceAndroid, Object serviceWrapper, + String instanceId) { return new ChromeBluetoothRemoteGattService(nativeBluetoothRemoteGattServiceAndroid, - (Wrappers.BluetoothGattServiceWrapper) serviceWrapper); + (Wrappers.BluetoothGattServiceWrapper) serviceWrapper, instanceId); } // Implements BluetoothRemoteGattServiceAndroid::GetUUID. @@ -64,11 +67,10 @@ final class ChromeBluetoothRemoteGattService { List<Wrappers.BluetoothGattCharacteristicWrapper> characteristics = mService.getCharacteristics(); for (Wrappers.BluetoothGattCharacteristicWrapper characteristic : characteristics) { - // Create a unique characteristic ID. getInstanceId only differs between characteristic - // instances with the same UUID. - // TODO(scheib): Make instance IDs unique to the whole adapter. http://crbug.com/546747 - String characteristicInstanceId = - characteristic.getUuid().toString() + characteristic.getInstanceId(); + // Create an adapter unique characteristic ID. getInstanceId only differs between + // characteristic instances with the same UUID on this service. + String characteristicInstanceId = mInstanceId + "/" + + characteristic.getUuid().toString() + "," + characteristic.getInstanceId(); nativeCreateGattRemoteCharacteristic(mNativeBluetoothRemoteGattServiceAndroid, characteristicInstanceId, characteristic); } diff --git a/device/bluetooth/bluetooth_device_unittest.cc b/device/bluetooth/bluetooth_device_unittest.cc index 6a2f261..8015f21 100644 --- a/device/bluetooth/bluetooth_device_unittest.cc +++ b/device/bluetooth/bluetooth_device_unittest.cc @@ -7,6 +7,7 @@ #include "base/macros.h" #include "base/run_loop.h" #include "base/strings/utf_string_conversions.h" +#include "device/bluetooth/bluetooth_gatt_service.h" #include "testing/gtest/include/gtest/gtest.h" #if defined(OS_ANDROID) @@ -426,7 +427,7 @@ TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) { #endif // defined(OS_ANDROID) #if defined(OS_ANDROID) -TEST_F(BluetoothTest, SimulateGattServicesDiscovered) { +TEST_F(BluetoothTest, GetGattServices_and_GetGattService) { InitWithFakeAdapter(); StartLowEnergyDiscoverySession(); BluetoothDevice* device = DiscoverLowEnergyDevice(3); @@ -443,11 +444,19 @@ TEST_F(BluetoothTest, SimulateGattServicesDiscovered) { services.push_back("00000001-0000-1000-8000-00805f9b34fb"); SimulateGattServicesDiscovered(device, services); EXPECT_EQ(3u, device->GetGattServices().size()); + + // Test GetGattService: + std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); + std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); + std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); + EXPECT_TRUE(device->GetGattService(service_id1)); + EXPECT_TRUE(device->GetGattService(service_id2)); + EXPECT_TRUE(device->GetGattService(service_id3)); } #endif // defined(OS_ANDROID) #if defined(OS_ANDROID) -TEST_F(BluetoothTest, SimulateGattServicesDiscoveryError) { +TEST_F(BluetoothTest, GetGattServices_DiscoveryError) { InitWithFakeAdapter(); StartLowEnergyDiscoverySession(); BluetoothDevice* device = DiscoverLowEnergyDevice(3); diff --git a/device/bluetooth/bluetooth_gatt_characteristic.h b/device/bluetooth/bluetooth_gatt_characteristic.h index f3a36e8..91d753f4 100644 --- a/device/bluetooth/bluetooth_gatt_characteristic.h +++ b/device/bluetooth/bluetooth_gatt_characteristic.h @@ -117,8 +117,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristic { // Identifier used to uniquely identify a GATT characteristic object. This is // different from the characteristic UUID: while multiple characteristics with // the same UUID can exist on a Bluetooth device, the identifier returned from - // this method is unique among all characteristics of a device. The contents - // of the identifier are platform specific. + // this method is unique among all characteristics on the adapter. The + // contents of the identifier are platform specific. virtual std::string GetIdentifier() const = 0; // The Bluetooth-specific UUID of the characteristic. diff --git a/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc b/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc new file mode 100644 index 0000000..132a918 --- /dev/null +++ b/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc @@ -0,0 +1,85 @@ +// 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. + +#include "device/bluetooth/bluetooth_gatt_service.h" + +#include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" +#include "testing/gtest/include/gtest/gtest.h" + +#if defined(OS_ANDROID) +#include "device/bluetooth/test/bluetooth_test_android.h" +#elif defined(OS_MACOSX) +#include "device/bluetooth/test/bluetooth_test_mac.h" +#endif + +namespace device { + +#if defined(OS_ANDROID) || defined(OS_MACOSX) +class BluetoothGattCharacteristicTest : public BluetoothTest {}; +#endif + +#if defined(OS_ANDROID) +TEST_F(BluetoothGattCharacteristicTest, GetIdentifier) { + InitWithFakeAdapter(); + StartLowEnergyDiscoverySession(); + // 2 devices to verify unique IDs across them. + BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); + BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); + device1->CreateGattConnection(GetGattConnectionCallback(), + GetConnectErrorCallback()); + device2->CreateGattConnection(GetGattConnectionCallback(), + GetConnectErrorCallback()); + SimulateGattConnection(device1); + SimulateGattConnection(device2); + + // 3 services (all with same UUID). + // 1 on the first device (to test characteristic instances across devices). + // 2 on the second device (to test same device, multiple service instances). + std::vector<std::string> services; + std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; + services.push_back(uuid); + SimulateGattServicesDiscovered(device1, services); + services.push_back(uuid); + SimulateGattServicesDiscovered(device2, services); + BluetoothGattService* service1 = device1->GetGattServices()[0]; + BluetoothGattService* service2 = device2->GetGattServices()[0]; + BluetoothGattService* service3 = device2->GetGattServices()[1]; + // 6 characteristics (same UUID), 2 on each service. + SimulateGattCharacteristic(service1, uuid); + SimulateGattCharacteristic(service1, uuid); + SimulateGattCharacteristic(service2, uuid); + SimulateGattCharacteristic(service2, uuid); + SimulateGattCharacteristic(service3, uuid); + SimulateGattCharacteristic(service3, uuid); + BluetoothGattCharacteristic* char1 = service1->GetCharacteristics()[0]; + BluetoothGattCharacteristic* char2 = service1->GetCharacteristics()[1]; + BluetoothGattCharacteristic* char3 = service2->GetCharacteristics()[0]; + BluetoothGattCharacteristic* char4 = service2->GetCharacteristics()[1]; + BluetoothGattCharacteristic* char5 = service3->GetCharacteristics()[0]; + BluetoothGattCharacteristic* char6 = service3->GetCharacteristics()[1]; + + // All IDs are unique, even though they have the same UUID. + EXPECT_NE(char1->GetIdentifier(), char2->GetIdentifier()); + EXPECT_NE(char1->GetIdentifier(), char3->GetIdentifier()); + EXPECT_NE(char1->GetIdentifier(), char4->GetIdentifier()); + EXPECT_NE(char1->GetIdentifier(), char5->GetIdentifier()); + EXPECT_NE(char1->GetIdentifier(), char6->GetIdentifier()); + + EXPECT_NE(char2->GetIdentifier(), char3->GetIdentifier()); + EXPECT_NE(char2->GetIdentifier(), char4->GetIdentifier()); + EXPECT_NE(char2->GetIdentifier(), char5->GetIdentifier()); + EXPECT_NE(char2->GetIdentifier(), char6->GetIdentifier()); + + EXPECT_NE(char3->GetIdentifier(), char4->GetIdentifier()); + EXPECT_NE(char3->GetIdentifier(), char5->GetIdentifier()); + EXPECT_NE(char3->GetIdentifier(), char6->GetIdentifier()); + + EXPECT_NE(char4->GetIdentifier(), char5->GetIdentifier()); + EXPECT_NE(char4->GetIdentifier(), char6->GetIdentifier()); + + EXPECT_NE(char5->GetIdentifier(), char6->GetIdentifier()); +} +#endif // defined(OS_ANDROID) + +} // namespace device diff --git a/device/bluetooth/bluetooth_gatt_descriptor.h b/device/bluetooth/bluetooth_gatt_descriptor.h index c91de1c..00e49ea 100644 --- a/device/bluetooth/bluetooth_gatt_descriptor.h +++ b/device/bluetooth/bluetooth_gatt_descriptor.h @@ -138,7 +138,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothGattDescriptor { // Identifier used to uniquely identify a GATT descriptor object. This is // different from the descriptor UUID: while multiple descriptors with the // same UUID can exist on a Bluetooth device, the identifier returned from - // this method is unique among all descriptors of a device. The contents of + // this method is unique among all descriptors on the adapter. The contents of // the identifier are platform specific. virtual std::string GetIdentifier() const = 0; diff --git a/device/bluetooth/bluetooth_gatt_service.h b/device/bluetooth/bluetooth_gatt_service.h index 58464fc..35ec16d 100644 --- a/device/bluetooth/bluetooth_gatt_service.h +++ b/device/bluetooth/bluetooth_gatt_service.h @@ -159,7 +159,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothGattService { // Identifier used to uniquely identify a GATT service object. This is // different from the service UUID: while multiple services with the same UUID // can exist on a Bluetooth device, the identifier returned from this method - // is unique among all services of a device. The contents of the identifier + // is unique among all services on the adapter. The contents of the identifier // are platform specific. virtual std::string GetIdentifier() const = 0; diff --git a/device/bluetooth/bluetooth_gatt_service_unittest.cc b/device/bluetooth/bluetooth_gatt_service_unittest.cc index 595905d..c3816f0 100644 --- a/device/bluetooth/bluetooth_gatt_service_unittest.cc +++ b/device/bluetooth/bluetooth_gatt_service_unittest.cc @@ -1,4 +1,4 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. +// 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. @@ -15,8 +15,50 @@ namespace device { +#if defined(OS_ANDROID) || defined(OS_MACOSX) +class BluetoothGattServiceTest : public BluetoothTest {}; +#endif + +#if defined(OS_ANDROID) +TEST_F(BluetoothGattServiceTest, GetIdentifier) { + InitWithFakeAdapter(); + StartLowEnergyDiscoverySession(); + // 2 devices to verify unique IDs across them. + BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); + BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); + device1->CreateGattConnection(GetGattConnectionCallback(), + GetConnectErrorCallback()); + device2->CreateGattConnection(GetGattConnectionCallback(), + GetConnectErrorCallback()); + SimulateGattConnection(device1); + SimulateGattConnection(device2); + + // 2 duplicate UUIDs creating 2 service instances on each device. + std::vector<std::string> services; + std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; + services.push_back(uuid); + services.push_back(uuid); + SimulateGattServicesDiscovered(device1, services); + SimulateGattServicesDiscovered(device2, services); + BluetoothGattService* service1 = device1->GetGattServices()[0]; + BluetoothGattService* service2 = device1->GetGattServices()[1]; + BluetoothGattService* service3 = device2->GetGattServices()[0]; + BluetoothGattService* service4 = device2->GetGattServices()[1]; + + // All IDs are unique, even though they have the same UUID. + EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); + EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); + EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); + + EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); + EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); + + EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); +} +#endif // defined(OS_ANDROID) + #if defined(OS_ANDROID) -TEST_F(BluetoothTest, GetUUIDAndGetIdentifier) { +TEST_F(BluetoothGattServiceTest, GetUUID) { InitWithFakeAdapter(); StartLowEnergyDiscoverySession(); BluetoothDevice* device = DiscoverLowEnergyDevice(3); @@ -34,15 +76,11 @@ TEST_F(BluetoothTest, GetUUIDAndGetIdentifier) { // Each has the same UUID. EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); - - // Instance IDs are unique. - EXPECT_NE(device->GetGattServices()[0]->GetIdentifier(), - device->GetGattServices()[1]->GetIdentifier()); } #endif // defined(OS_ANDROID) #if defined(OS_ANDROID) -TEST_F(BluetoothTest, GetCharacteristics_FindNone) { +TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { InitWithFakeAdapter(); StartLowEnergyDiscoverySession(); BluetoothDevice* device = DiscoverLowEnergyDevice(3); @@ -61,7 +99,7 @@ TEST_F(BluetoothTest, GetCharacteristics_FindNone) { #endif // defined(OS_ANDROID) #if defined(OS_ANDROID) -TEST_F(BluetoothTest, GetCharacteristic_FindSeveral) { +TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { InitWithFakeAdapter(); StartLowEnergyDiscoverySession(); BluetoothDevice* device = DiscoverLowEnergyDevice(3); @@ -69,12 +107,11 @@ TEST_F(BluetoothTest, GetCharacteristic_FindSeveral) { GetConnectErrorCallback()); SimulateGattConnection(device); - // Simulate a service, with no Characteristics: + // Simulate a service, with several Characteristics: std::vector<std::string> services; 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_uuid3 = characteristic_uuid2; // Duplicate UUID. @@ -83,18 +120,12 @@ TEST_F(BluetoothTest, GetCharacteristic_FindSeveral) { SimulateGattCharacteristic(service, characteristic_uuid3); EXPECT_EQ(3u, service->GetCharacteristics().size()); - // TODO(scheib): Implement GetIdentifier. crbug.com/545682 - // std::string characteristic_id1 = - // service->GetCharacteristics()[0]->GetIdentifier(); - // ... - // EXPECT_NE(characteristic_id2, characteristic_id3); - // For now, just hard-code: - std::string characteristic_id1 = "00000001-0000-1000-8000-00805f9b34fb0"; - std::string characteristic_id2 = "00000002-0000-1000-8000-00805f9b34fb0"; - std::string characteristic_id3 = "00000002-0000-1000-8000-00805f9b34fb1"; - EXPECT_TRUE(service->GetCharacteristic(characteristic_id1)); - EXPECT_TRUE(service->GetCharacteristic(characteristic_id2)); - EXPECT_TRUE(service->GetCharacteristic(characteristic_id3)); + 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)); } #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 1c2a99a..5286baa 100644 --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc @@ -10,17 +10,17 @@ namespace device { // static scoped_ptr<BluetoothRemoteGattCharacteristicAndroid> -BluetoothRemoteGattCharacteristicAndroid::Create() { +BluetoothRemoteGattCharacteristicAndroid::Create( + const std::string& instanceId) { return make_scoped_ptr<BluetoothRemoteGattCharacteristicAndroid>( - new BluetoothRemoteGattCharacteristicAndroid()); + new BluetoothRemoteGattCharacteristicAndroid(instanceId)); } BluetoothRemoteGattCharacteristicAndroid:: ~BluetoothRemoteGattCharacteristicAndroid() {} std::string BluetoothRemoteGattCharacteristicAndroid::GetIdentifier() const { - NOTIMPLEMENTED(); - return ""; + return instanceId_; } BluetoothUUID BluetoothRemoteGattCharacteristicAndroid::GetUUID() const { @@ -106,6 +106,7 @@ void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( } BluetoothRemoteGattCharacteristicAndroid:: - BluetoothRemoteGattCharacteristicAndroid() {} + BluetoothRemoteGattCharacteristicAndroid(const std::string& instanceId) + : instanceId_(instanceId) {} } // namespace device diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h index 58e139d..7414977c 100644 --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h @@ -24,7 +24,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid // to |bluetooth_remote_gatt_service_wrapper|. // // TODO(scheib): Actually create the Java object. crbug.com/545682 - static scoped_ptr<BluetoothRemoteGattCharacteristicAndroid> Create(); + static scoped_ptr<BluetoothRemoteGattCharacteristicAndroid> Create( + const std::string& instanceId); ~BluetoothRemoteGattCharacteristicAndroid() override; @@ -51,7 +52,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid const ErrorCallback& error_callback) override; private: - BluetoothRemoteGattCharacteristicAndroid(); + BluetoothRemoteGattCharacteristicAndroid(const std::string& instanceId); + + // Adapter unique instance ID. + std::string instanceId_; std::vector<uint8> value_; diff --git a/device/bluetooth/bluetooth_remote_gatt_service_android.cc b/device/bluetooth/bluetooth_remote_gatt_service_android.cc index b5bc1a5..1f15629 100644 --- a/device/bluetooth/bluetooth_remote_gatt_service_android.cc +++ b/device/bluetooth/bluetooth_remote_gatt_service_android.cc @@ -20,13 +20,15 @@ BluetoothRemoteGattServiceAndroid* BluetoothRemoteGattServiceAndroid::Create( BluetoothAdapterAndroid* adapter, BluetoothDeviceAndroid* device, jobject bluetooth_remote_gatt_service_wrapper, - std::string instanceId) { + const std::string& instanceId) { BluetoothRemoteGattServiceAndroid* service = new BluetoothRemoteGattServiceAndroid(adapter, device, instanceId); + JNIEnv* env = base::android::AttachCurrentThread(); service->j_service_.Reset(Java_ChromeBluetoothRemoteGattService_create( - AttachCurrentThread(), reinterpret_cast<intptr_t>(service), - bluetooth_remote_gatt_service_wrapper)); + env, reinterpret_cast<intptr_t>(service), + bluetooth_remote_gatt_service_wrapper, + base::android::ConvertUTF8ToJavaString(env, instanceId).obj())); return service; } @@ -123,14 +125,15 @@ void BluetoothRemoteGattServiceAndroid::CreateGattRemoteCharacteristic( DCHECK(!characteristics_.contains(instanceIdString)); - characteristics_.set(instanceIdString, - BluetoothRemoteGattCharacteristicAndroid::Create()); + characteristics_.set( + instanceIdString, + BluetoothRemoteGattCharacteristicAndroid::Create(instanceIdString)); } BluetoothRemoteGattServiceAndroid::BluetoothRemoteGattServiceAndroid( BluetoothAdapterAndroid* adapter, BluetoothDeviceAndroid* device, - std::string instanceId) + const std::string& instanceId) : adapter_(adapter), device_(device), instanceId_(instanceId) {} BluetoothRemoteGattServiceAndroid::~BluetoothRemoteGattServiceAndroid() { diff --git a/device/bluetooth/bluetooth_remote_gatt_service_android.h b/device/bluetooth/bluetooth_remote_gatt_service_android.h index b1d19e0..a8f1294 100644 --- a/device/bluetooth/bluetooth_remote_gatt_service_android.h +++ b/device/bluetooth/bluetooth_remote_gatt_service_android.h @@ -40,7 +40,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceAndroid BluetoothDeviceAndroid* device, jobject bluetooth_remote_gatt_service_wrapper, // Java Type: // BluetoothRemoteGattServiceWrapper - std::string instanceId); + const std::string& instanceId); // Register C++ methods exposed to Java using JNI. static bool RegisterJNI(JNIEnv* env); @@ -82,7 +82,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceAndroid BluetoothRemoteGattServiceAndroid(BluetoothAdapterAndroid* adapter, BluetoothDeviceAndroid* device, - std::string instanceId); + const std::string& instanceId); ~BluetoothRemoteGattServiceAndroid() override; // Populates |characteristics_| from Java objects if necessary. @@ -99,7 +99,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceAndroid // here since |device_| owns this instance. BluetoothDeviceAndroid* device_; - // Instance ID, cached from Android object. + // Adapter unique instance ID. std::string instanceId_; // Map of characteristics, keyed by characteristic identifier. diff --git a/device/device_tests.gyp b/device/device_tests.gyp index 3cc4fc0..a49696a 100644 --- a/device/device_tests.gyp +++ b/device/device_tests.gyp @@ -46,6 +46,7 @@ 'bluetooth/bluetooth_device_unittest.cc', 'bluetooth/bluetooth_device_win_unittest.cc', 'bluetooth/bluetooth_discovery_filter_unittest.cc', + 'bluetooth/bluetooth_gatt_characteristic_unittest.cc', 'bluetooth/bluetooth_gatt_chromeos_unittest.cc', 'bluetooth/bluetooth_gatt_service_unittest.cc', 'bluetooth/bluetooth_low_energy_win_unittest.cc', |