diff options
author | scheib <scheib@chromium.org> | 2015-11-05 21:00:05 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-11-06 05:00:58 +0000 |
commit | f184bd5c60373345d36545af55bb05ad493945c1 (patch) | |
tree | 9d9c2086a1cce497d5adef26fef6d7262c89b8d7 /device | |
parent | 524ae0d527811ba0c91877753670d24a8dbce19a (diff) | |
download | chromium_src-f184bd5c60373345d36545af55bb05ad493945c1.zip chromium_src-f184bd5c60373345d36545af55bb05ad493945c1.tar.gz chromium_src-f184bd5c60373345d36545af55bb05ad493945c1.tar.bz2 |
bluetooth: android: Implement StartNotifySession.
Basic implementation only starts session. They can not yet be stopped
and the characteristic updates are not propagated yet.
BUG=551634
Review URL: https://codereview.chromium.org/1422463012
Cr-Commit-Position: refs/heads/master@{#358259}
Diffstat (limited to 'device')
14 files changed, 285 insertions, 11 deletions
diff --git a/device/bluetooth/BUILD.gn b/device/bluetooth/BUILD.gn index 5edb17c..f541604 100644 --- a/device/bluetooth/BUILD.gn +++ b/device/bluetooth/BUILD.gn @@ -78,6 +78,8 @@ component("bluetooth") { "bluetooth_gatt_descriptor.h", "bluetooth_gatt_notify_session.cc", "bluetooth_gatt_notify_session.h", + "bluetooth_gatt_notify_session_android.cc", + "bluetooth_gatt_notify_session_android.h", "bluetooth_gatt_notify_session_chromeos.cc", "bluetooth_gatt_notify_session_chromeos.h", "bluetooth_gatt_service.cc", 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 index 845026d..dd0d261 100644 --- a/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java +++ b/device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothRemoteGattCharacteristic.java @@ -91,6 +91,18 @@ final class ChromeBluetoothRemoteGattCharacteristic { return mCharacteristic.getProperties(); } + // Implements BluetoothRemoteGattCharacteristicAndroid::StartNotifySession. + @CalledByNative + private boolean startNotifySession() { + if (!mChromeBluetoothDevice.mBluetoothGatt.setCharacteristicNotification( + mCharacteristic, true)) { + Log.i(TAG, "startNotifySession setCharacteristicNotification failed."); + return false; + } + Log.i(TAG, "startNotifySession."); + return true; + } + // Implements BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic. @CalledByNative private boolean readRemoteCharacteristic() { 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 e8e6306..baeec8e 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 @@ -294,6 +294,11 @@ class Wrappers { return mGatt.readCharacteristic(characteristic.mCharacteristic); } + boolean setCharacteristicNotification( + BluetoothGattCharacteristicWrapper characteristic, boolean enable) { + return mGatt.setCharacteristicNotification(characteristic.mCharacteristic, enable); + } + boolean writeCharacteristic(BluetoothGattCharacteristicWrapper characteristic) { return mGatt.writeCharacteristic(characteristic.mCharacteristic); } diff --git a/device/bluetooth/bluetooth.gyp b/device/bluetooth/bluetooth.gyp index 90306d1..5159521 100644 --- a/device/bluetooth/bluetooth.gyp +++ b/device/bluetooth/bluetooth.gyp @@ -81,6 +81,8 @@ 'bluetooth_gatt_descriptor.h', 'bluetooth_gatt_notify_session.cc', 'bluetooth_gatt_notify_session.h', + 'bluetooth_gatt_notify_session_android.cc', + 'bluetooth_gatt_notify_session_android.h', 'bluetooth_gatt_notify_session_chromeos.cc', 'bluetooth_gatt_notify_session_chromeos.h', 'bluetooth_gatt_service.cc', diff --git a/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc b/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc index 095d2b3..cf2fddd 100644 --- a/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc +++ b/device/bluetooth/bluetooth_gatt_characteristic_unittest.cc @@ -561,4 +561,75 @@ TEST_F(BluetoothGattCharacteristicTest, WriteRemoteCharacteristic_ReadPending) { } #endif // defined(OS_ANDROID) +#if defined(OS_ANDROID) +// Tests StartNotifySession success. +TEST_F(BluetoothGattCharacteristicTest, StartNotifySession) { + ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); + + characteristic1_->StartNotifySession(GetNotifyCallback(), + GetGattErrorCallback()); + EXPECT_EQ(1, gatt_notify_characteristic_attempts_); + EXPECT_EQ(0, callback_count_); + SimulateGattNotifySessionStarted(characteristic1_); + EXPECT_EQ(1, callback_count_); + EXPECT_EQ(0, error_callback_count_); + ASSERT_EQ(1u, notify_sessions_.size()); + ASSERT_TRUE(notify_sessions_[0]); + EXPECT_EQ(characteristic1_->GetIdentifier(), + notify_sessions_[0]->GetCharacteristicIdentifier()); + EXPECT_TRUE(notify_sessions_[0]->IsActive()); +} +#endif // defined(OS_ANDROID) + +#if defined(OS_ANDROID) +// Tests StartNotifySession synchronous failure. +TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_SynchronousError) { + ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); + + SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( + characteristic1_); + characteristic1_->StartNotifySession(GetNotifyCallback(), + GetGattErrorCallback()); + EXPECT_EQ(0, error_callback_count_); + base::RunLoop().RunUntilIdle(); + EXPECT_EQ(0, gatt_notify_characteristic_attempts_); + EXPECT_EQ(0, callback_count_); + EXPECT_EQ(1, error_callback_count_); + ASSERT_EQ(0u, notify_sessions_.size()); +} +#endif // defined(OS_ANDROID) + +#if defined(OS_ANDROID) +// Tests multiple StartNotifySession success. +TEST_F(BluetoothGattCharacteristicTest, StartNotifySession_Multiple) { + ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); + + characteristic1_->StartNotifySession(GetNotifyCallback(), + GetGattErrorCallback()); + characteristic1_->StartNotifySession(GetNotifyCallback(), + GetGattErrorCallback()); +#if defined(OS_ANDROID) + // TODO(crbug.com/551634): Decide when implementing IsNotifying if Android + // should trust the notification request always worked, or if we should always + // redundantly set the value to the OS. + EXPECT_EQ(2, gatt_notify_characteristic_attempts_); +#else + EXPECT_EQ(1, gatt_notify_characteristic_attempts_); +#endif + EXPECT_EQ(0, callback_count_); + SimulateGattNotifySessionStarted(characteristic1_); + EXPECT_EQ(2, callback_count_); + EXPECT_EQ(0, error_callback_count_); + ASSERT_EQ(2u, notify_sessions_.size()); + ASSERT_TRUE(notify_sessions_[0]); + ASSERT_TRUE(notify_sessions_[1]); + EXPECT_EQ(characteristic1_->GetIdentifier(), + notify_sessions_[0]->GetCharacteristicIdentifier()); + EXPECT_EQ(characteristic1_->GetIdentifier(), + notify_sessions_[1]->GetCharacteristicIdentifier()); + EXPECT_TRUE(notify_sessions_[0]->IsActive()); + EXPECT_TRUE(notify_sessions_[1]->IsActive()); +} +#endif // defined(OS_ANDROID) + } // namespace device diff --git a/device/bluetooth/bluetooth_gatt_notify_session_android.cc b/device/bluetooth/bluetooth_gatt_notify_session_android.cc new file mode 100644 index 0000000..9f6e553 --- /dev/null +++ b/device/bluetooth/bluetooth_gatt_notify_session_android.cc @@ -0,0 +1,30 @@ +// Copyright 2014 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_notify_session_android.h" + +#include "base/logging.h" + +namespace device { + +BluetoothGattNotifySessionAndroid::BluetoothGattNotifySessionAndroid( + const std::string& characteristic_identifier) + : characteristic_id_(characteristic_identifier) {} + +BluetoothGattNotifySessionAndroid::~BluetoothGattNotifySessionAndroid() {} + +std::string BluetoothGattNotifySessionAndroid::GetCharacteristicIdentifier() + const { + return characteristic_id_; +} + +bool BluetoothGattNotifySessionAndroid::IsActive() { + return true; +} + +void BluetoothGattNotifySessionAndroid::Stop(const base::Closure& callback) { + NOTIMPLEMENTED(); +} + +} // namespace device diff --git a/device/bluetooth/bluetooth_gatt_notify_session_android.h b/device/bluetooth/bluetooth_gatt_notify_session_android.h new file mode 100644 index 0000000..8413f87 --- /dev/null +++ b/device/bluetooth/bluetooth_gatt_notify_session_android.h @@ -0,0 +1,45 @@ +// Copyright 2014 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. + +#ifndef DEVICE_BLUETOOTH_BLUETOOTH_GATT_NOTIFY_SESSION_ANDROID_H_ +#define DEVICE_BLUETOOTH_BLUETOOTH_GATT_NOTIFY_SESSION_ANDROID_H_ + +#include <string> + +#include "base/callback.h" +#include "base/macros.h" +#include "device/bluetooth/bluetooth_gatt_notify_session.h" + +namespace device { + +class BluetoothAdapter; +class BluetoothRemoteGattCharacteristicAndroid; + +// BluetoothGattNotifySessionAndroid implements +// BluetoothGattNotifySession for the Android platform. +// +// TODO(crbug.com/551634): Detect destroyed Characteristic or parents objects. +// TODO(crbug.com/551634): Implement Stop. +class DEVICE_BLUETOOTH_EXPORT BluetoothGattNotifySessionAndroid + : public device::BluetoothGattNotifySession { + public: + explicit BluetoothGattNotifySessionAndroid( + const std::string& characteristic_identifier); + ~BluetoothGattNotifySessionAndroid() override; + + // BluetoothGattNotifySession overrides. + std::string GetCharacteristicIdentifier() const override; + bool IsActive() override; + void Stop(const base::Closure& callback) override; + + private: + // Identifier of the associated characteristic. + std::string characteristic_id_; + + DISALLOW_COPY_AND_ASSIGN(BluetoothGattNotifySessionAndroid); +}; + +} // namespace device + +#endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_NOTIFY_SESSION_ANDROID_H_ diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc index f0825f6b..8d5868b 100644 --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc @@ -10,6 +10,7 @@ #include "base/bind.h" #include "base/logging.h" #include "base/message_loop/message_loop.h" +#include "device/bluetooth/bluetooth_gatt_notify_session_android.h" #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" #include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" @@ -54,7 +55,7 @@ BluetoothRemoteGattCharacteristicAndroid::GetJavaObject() { } std::string BluetoothRemoteGattCharacteristicAndroid::GetIdentifier() const { - return instanceId_; + return instance_id_; } BluetoothUUID BluetoothRemoteGattCharacteristicAndroid::GetUUID() const { @@ -123,7 +124,20 @@ bool BluetoothRemoteGattCharacteristicAndroid::UpdateValue( void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( const NotifySessionCallback& callback, const ErrorCallback& error_callback) { - NOTIMPLEMENTED(); + // TODO(crbug.com/551634): Check characteristic properties and return a better + // error code if notifications aren't permitted. + if (Java_ChromeBluetoothRemoteGattCharacteristic_startNotifySession( + AttachCurrentThread(), j_characteristic_.obj())) { + scoped_ptr<device::BluetoothGattNotifySession> notify_session( + new BluetoothGattNotifySessionAndroid(instance_id_)); + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(callback, base::Passed(¬ify_session))); + } else { + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(error_callback, + BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); + } } void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( @@ -215,6 +229,6 @@ void BluetoothRemoteGattCharacteristicAndroid::OnWrite(JNIEnv* env, BluetoothRemoteGattCharacteristicAndroid:: BluetoothRemoteGattCharacteristicAndroid(const std::string& instanceId) - : instanceId_(instanceId) {} + : instance_id_(instanceId) {} } // namespace device diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h index b5c85a3..b386e7e 100644 --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_android.h @@ -14,6 +14,9 @@ namespace device { // BluetoothRemoteGattCharacteristicAndroid along with its owned Java class // org.chromium.device.bluetooth.ChromeBluetoothRemoteGattCharacteristic // implement BluetootGattCharacteristic. +// +// TODO(crbug.com/551634): When notifications are enabled characteristic updates +// should call observers' GattCharacteristicValueChanged. class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid : public BluetoothGattCharacteristic { public: @@ -75,7 +78,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid base::android::ScopedJavaGlobalRef<jobject> j_characteristic_; // Adapter unique instance ID. - std::string instanceId_; + std::string instance_id_; // ReadRemoteCharacteristic callbacks and pending state. bool read_pending_ = false; diff --git a/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java b/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java index a38f749..bf3d8a0 100644 --- a/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java +++ b/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java @@ -277,6 +277,7 @@ class Fakes { final FakeBluetoothDevice mDevice; final ArrayList<Wrappers.BluetoothGattServiceWrapper> mServices; boolean mReadCharacteristicWillFailSynchronouslyOnce = false; + boolean mSetCharacteristicNotificationWillFailSynchronouslyOnce = false; boolean mWriteCharacteristicWillFailSynchronouslyOnce = false; public FakeBluetoothGatt(FakeBluetoothDevice device) { @@ -312,6 +313,18 @@ class Fakes { } @Override + boolean setCharacteristicNotification( + Wrappers.BluetoothGattCharacteristicWrapper characteristic, boolean enable) { + if (mSetCharacteristicNotificationWillFailSynchronouslyOnce) { + mSetCharacteristicNotificationWillFailSynchronouslyOnce = false; + return false; + } + nativeOnFakeBluetoothGattSetCharacteristicNotification( + mDevice.mAdapter.mNativeBluetoothTestAndroid); + return true; + } + + @Override boolean writeCharacteristic(Wrappers.BluetoothGattCharacteristicWrapper characteristic) { if (mWriteCharacteristicWillFailSynchronouslyOnce) { mWriteCharacteristicWillFailSynchronouslyOnce = false; @@ -424,6 +437,17 @@ class Fakes { // Cause subsequent value reads of a characteristic to fail synchronously. @CalledByNative("FakeBluetoothGattCharacteristic") + private static void setCharacteristicNotificationWillFailSynchronouslyOnce( + ChromeBluetoothRemoteGattCharacteristic chromeCharacteristic) { + FakeBluetoothGattCharacteristic fakeCharacteristic = + (FakeBluetoothGattCharacteristic) chromeCharacteristic.mCharacteristic; + + fakeCharacteristic.mService.mDevice.mGatt + .mSetCharacteristicNotificationWillFailSynchronouslyOnce = true; + } + + // Cause subsequent value reads of a characteristic to fail synchronously. + @CalledByNative("FakeBluetoothGattCharacteristic") private static void setReadCharacteristicWillFailSynchronouslyOnce( ChromeBluetoothRemoteGattCharacteristic chromeCharacteristic) { FakeBluetoothGattCharacteristic fakeCharacteristic = @@ -477,22 +501,26 @@ class Fakes { // --------------------------------------------------------------------------------------------- // BluetoothTestAndroid C++ methods declared for access from java: - // Binds to BluetoothAdapterAndroid::OnFakeBluetoothDeviceConnectGattCalled. + // Binds to BluetoothTestAndroid::OnFakeBluetoothDeviceConnectGattCalled. private static native void nativeOnFakeBluetoothDeviceConnectGattCalled( long nativeBluetoothTestAndroid); - // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattDisconnect. + // Binds to BluetoothTestAndroid::OnFakeBluetoothGattDisconnect. private static native void nativeOnFakeBluetoothGattDisconnect(long nativeBluetoothTestAndroid); - // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattDiscoverServices. + // Binds to BluetoothTestAndroid::OnFakeBluetoothGattDiscoverServices. private static native void nativeOnFakeBluetoothGattDiscoverServices( long nativeBluetoothTestAndroid); - // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattReadCharacteristic. + // Binds to BluetoothTestAndroid::OnFakeBluetoothGattSetCharacteristicNotification. + private static native void nativeOnFakeBluetoothGattSetCharacteristicNotification( + long nativeBluetoothTestAndroid); + + // Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadCharacteristic. private static native void nativeOnFakeBluetoothGattReadCharacteristic( long nativeBluetoothTestAndroid); - // Binds to BluetoothAdapterAndroid::OnFakeBluetoothGattWriteCharacteristic. + // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteCharacteristic. private static native void nativeOnFakeBluetoothGattWriteCharacteristic( long nativeBluetoothTestAndroid, byte[] value); } diff --git a/device/bluetooth/test/bluetooth_test.cc b/device/bluetooth/test/bluetooth_test.cc index e35ee87..70d457f 100644 --- a/device/bluetooth/test/bluetooth_test.cc +++ b/device/bluetooth/test/bluetooth_test.cc @@ -64,6 +64,12 @@ void BluetoothTestBase::GattConnectionCallback( gatt_connections_.push_back(connection.release()); } +void BluetoothTestBase::NotifyCallback( + scoped_ptr<BluetoothGattNotifySession> notify_session) { + ++callback_count_; + notify_sessions_.push_back(notify_session.release()); +} + void BluetoothTestBase::ReadValueCallback(const std::vector<uint8>& value) { ++callback_count_; last_read_value_ = value; @@ -101,6 +107,12 @@ BluetoothTestBase::GetGattConnectionCallback() { weak_factory_.GetWeakPtr()); } +BluetoothGattCharacteristic::NotifySessionCallback +BluetoothTestBase::GetNotifyCallback() { + return base::Bind(&BluetoothTestBase::NotifyCallback, + weak_factory_.GetWeakPtr()); +} + BluetoothGattCharacteristic::ValueCallback BluetoothTestBase::GetReadValueCallback() { return base::Bind(&BluetoothTestBase::ReadValueCallback, @@ -131,6 +143,7 @@ void BluetoothTestBase::ResetEventCounts() { gatt_connection_attempts_ = 0; gatt_disconnection_attempts_ = 0; gatt_discovery_attempts_ = 0; + gatt_notify_characteristic_attempts_ = 0; gatt_read_characteristic_attempts_ = 0; gatt_write_characteristic_attempts_ = 0; } diff --git a/device/bluetooth/test/bluetooth_test.h b/device/bluetooth/test/bluetooth_test.h index 81de2b7..c4f8687 100644 --- a/device/bluetooth/test/bluetooth_test.h +++ b/device/bluetooth/test/bluetooth_test.h @@ -13,6 +13,7 @@ #include "device/bluetooth/bluetooth_discovery_session.h" #include "device/bluetooth/bluetooth_gatt_characteristic.h" #include "device/bluetooth/bluetooth_gatt_connection.h" +#include "device/bluetooth/bluetooth_gatt_notify_session.h" #include "device/bluetooth/bluetooth_gatt_service.h" #include "testing/gtest/include/gtest/gtest.h" @@ -101,6 +102,15 @@ class BluetoothTestBase : public testing::Test { const std::string& uuid, int properties) {} + // Simulates a Characteristic Set Notify success. + virtual void SimulateGattNotifySessionStarted( + BluetoothGattCharacteristic* characteristic) {} + + // Simulates a Characteristic Set Notify operation failing synchronously once + // for an unknown reason. + virtual void SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( + BluetoothGattCharacteristic* characteristic) {} + // Simulates a Characteristic Read operation succeeding, returning |value|. virtual void SimulateGattCharacteristicRead( BluetoothGattCharacteristic* characteristic, @@ -137,6 +147,7 @@ class BluetoothTestBase : public testing::Test { void Callback(); void DiscoverySessionCallback(scoped_ptr<BluetoothDiscoverySession>); void GattConnectionCallback(scoped_ptr<BluetoothGattConnection>); + void NotifyCallback(scoped_ptr<BluetoothGattNotifySession>); void ReadValueCallback(const std::vector<uint8>& value); void ErrorCallback(); void ConnectErrorCallback(enum BluetoothDevice::ConnectErrorCode); @@ -146,6 +157,7 @@ class BluetoothTestBase : public testing::Test { base::Closure GetCallback(); BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback(); BluetoothDevice::GattConnectionCallback GetGattConnectionCallback(); + BluetoothGattCharacteristic::NotifySessionCallback GetNotifyCallback(); BluetoothGattCharacteristic::ValueCallback GetReadValueCallback(); BluetoothAdapter::ErrorCallback GetErrorCallback(); BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback(); @@ -164,6 +176,7 @@ class BluetoothTestBase : public testing::Test { ScopedVector<BluetoothGattConnection> gatt_connections_; enum BluetoothDevice::ConnectErrorCode last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN; + ScopedVector<BluetoothGattNotifySession> notify_sessions_; std::vector<uint8> last_read_value_; std::vector<uint8> last_write_value_; BluetoothGattService::GattErrorCode last_gatt_error_code_; @@ -172,6 +185,7 @@ class BluetoothTestBase : public testing::Test { int gatt_connection_attempts_ = 0; int gatt_disconnection_attempts_ = 0; int gatt_discovery_attempts_ = 0; + int gatt_notify_characteristic_attempts_ = 0; int gatt_read_characteristic_attempts_ = 0; int gatt_write_characteristic_attempts_ = 0; base::WeakPtrFactory<BluetoothTestBase> weak_factory_; diff --git a/device/bluetooth/test/bluetooth_test_android.cc b/device/bluetooth/test/bluetooth_test_android.cc index 5cd0674..28514f7 100644 --- a/device/bluetooth/test/bluetooth_test_android.cc +++ b/device/bluetooth/test/bluetooth_test_android.cc @@ -10,6 +10,7 @@ #include "base/android/jni_array.h" #include "base/android/jni_string.h" #include "base/logging.h" +#include "base/run_loop.h" #include "device/bluetooth/android/wrappers.h" #include "device/bluetooth/bluetooth_adapter_android.h" #include "device/bluetooth/bluetooth_device_android.h" @@ -156,6 +157,25 @@ void BluetoothTestAndroid::SimulateGattCharacteristic( base::android::ConvertUTF8ToJavaString(env, uuid).obj(), properties); } +void BluetoothTestAndroid::SimulateGattNotifySessionStarted( + BluetoothGattCharacteristic* characteristic) { + // Android doesn't provide any sort of callback for when notifications have + // been enabled. So, just run the message loop to process the success + // callback. + base::RunLoop().RunUntilIdle(); +} + +void BluetoothTestAndroid:: + SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( + BluetoothGattCharacteristic* characteristic) { + BluetoothRemoteGattCharacteristicAndroid* characteristic_android = + static_cast<BluetoothRemoteGattCharacteristicAndroid*>(characteristic); + JNIEnv* env = base::android::AttachCurrentThread(); + + Java_FakeBluetoothGattCharacteristic_setCharacteristicNotificationWillFailSynchronouslyOnce( + env, characteristic_android->GetJavaObject().obj()); +} + void BluetoothTestAndroid::SimulateGattCharacteristicRead( BluetoothGattCharacteristic* characteristic, const std::vector<uint8>& value) { @@ -241,6 +261,12 @@ void BluetoothTestAndroid::OnFakeBluetoothGattDiscoverServices(JNIEnv* env, gatt_discovery_attempts_++; } +void BluetoothTestAndroid::OnFakeBluetoothGattSetCharacteristicNotification( + JNIEnv* env, + jobject caller) { + gatt_notify_characteristic_attempts_++; +} + void BluetoothTestAndroid::OnFakeBluetoothGattReadCharacteristic( JNIEnv* env, jobject caller) { diff --git a/device/bluetooth/test/bluetooth_test_android.h b/device/bluetooth/test/bluetooth_test_android.h index 48939b3..0c2b356 100644 --- a/device/bluetooth/test/bluetooth_test_android.h +++ b/device/bluetooth/test/bluetooth_test_android.h @@ -38,6 +38,10 @@ class BluetoothTestAndroid : public BluetoothTestBase { void SimulateGattCharacteristic(BluetoothGattService* service, const std::string& uuid, int properties) override; + virtual void SimulateGattNotifySessionStarted( + BluetoothGattCharacteristic* characteristic) override; + virtual void SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( + BluetoothGattCharacteristic* characteristic) override; void SimulateGattCharacteristicRead( BluetoothGattCharacteristic* characteristic, const std::vector<uint8>& value) override; @@ -63,10 +67,15 @@ class BluetoothTestAndroid : public BluetoothTestBase { // Records that Java FakeBluetoothGatt discoverServices was called. void OnFakeBluetoothGattDiscoverServices(JNIEnv* env, jobject caller); - // Records that Java FakeBluetoothGatt discoverServices was called. + // Records that Java FakeBluetoothGatt setCharacteristicNotification was + // called. + void OnFakeBluetoothGattSetCharacteristicNotification(JNIEnv* env, + jobject caller); + + // Records that Java FakeBluetoothGatt readCharacteristic was called. void OnFakeBluetoothGattReadCharacteristic(JNIEnv* env, jobject caller); - // Records that Java FakeBluetoothGatt discoverServices was called. + // Records that Java FakeBluetoothGatt writeCharacteristic was called. void OnFakeBluetoothGattWriteCharacteristic(JNIEnv* env, jobject caller, jbyteArray value); |