diff options
Diffstat (limited to 'device/bluetooth/bluetooth_remote_gatt_descriptor_android.cc')
-rw-r--r-- | device/bluetooth/bluetooth_remote_gatt_descriptor_android.cc | 102 |
1 files changed, 88 insertions, 14 deletions
diff --git a/device/bluetooth/bluetooth_remote_gatt_descriptor_android.cc b/device/bluetooth/bluetooth_remote_gatt_descriptor_android.cc index d197a78..39b812f 100644 --- a/device/bluetooth/bluetooth_remote_gatt_descriptor_android.cc +++ b/device/bluetooth/bluetooth_remote_gatt_descriptor_android.cc @@ -31,9 +31,7 @@ BluetoothRemoteGattDescriptorAndroid::Create( descriptor->j_descriptor_.Reset( Java_ChromeBluetoothRemoteGattDescriptor_create( - AttachCurrentThread(), - // TODO(scheib) Will eventually need to pass c++ pointer: - // reinterpret_cast<intptr_t>(descriptor.get()), + AttachCurrentThread(), reinterpret_cast<intptr_t>(descriptor.get()), bluetooth_gatt_descriptor_wrapper, chrome_bluetooth_device)); return descriptor; @@ -71,9 +69,7 @@ bool BluetoothRemoteGattDescriptorAndroid::IsLocal() const { const std::vector<uint8_t>& BluetoothRemoteGattDescriptorAndroid::GetValue() const { - NOTIMPLEMENTED(); - static std::vector<uint8_t> empty_value; - return empty_value; + return value_; } BluetoothGattCharacteristic* @@ -91,20 +87,98 @@ BluetoothRemoteGattDescriptorAndroid::GetPermissions() const { void BluetoothRemoteGattDescriptorAndroid::ReadRemoteDescriptor( const ValueCallback& callback, const ErrorCallback& error_callback) { - NOTIMPLEMENTED(); - base::MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(error_callback, BluetoothGattService::GATT_ERROR_FAILED)); + if (read_pending_ || write_pending_) { + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(error_callback, + BluetoothGattService::GATT_ERROR_IN_PROGRESS)); + return; + } + + if (!Java_ChromeBluetoothRemoteGattDescriptor_readRemoteDescriptor( + AttachCurrentThread(), j_descriptor_.obj())) { + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(error_callback, + BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); + return; + } + + read_pending_ = true; + read_callback_ = callback; + read_error_callback_ = error_callback; } void BluetoothRemoteGattDescriptorAndroid::WriteRemoteDescriptor( const std::vector<uint8_t>& new_value, const base::Closure& callback, const ErrorCallback& error_callback) { - NOTIMPLEMENTED(); - base::MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(error_callback, BluetoothGattService::GATT_ERROR_FAILED)); + if (read_pending_ || write_pending_) { + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(error_callback, + BluetoothGattService::GATT_ERROR_IN_PROGRESS)); + return; + } + + JNIEnv* env = AttachCurrentThread(); + if (!Java_ChromeBluetoothRemoteGattDescriptor_writeRemoteDescriptor( + env, j_descriptor_.obj(), + base::android::ToJavaByteArray(env, new_value).obj())) { + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(error_callback, + BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); + return; + } + + write_pending_ = true; + write_callback_ = callback; + write_error_callback_ = error_callback; +} + +void BluetoothRemoteGattDescriptorAndroid::OnRead( + JNIEnv* env, + const JavaParamRef<jobject>& jcaller, + int32_t status, + const JavaParamRef<jbyteArray>& value) { + read_pending_ = false; + + // Clear callbacks before calling to avoid reentrancy issues. + ValueCallback read_callback = read_callback_; + ErrorCallback read_error_callback = read_error_callback_; + read_callback_.Reset(); + read_error_callback_.Reset(); + + if (status == 0 // android.bluetooth.BluetoothGatt.GATT_SUCCESS + && !read_callback.is_null()) { + base::android::JavaByteArrayToByteVector(env, value, &value_); + read_callback.Run(value_); + // TODO(https://crbug.com/584369): Call GattDescriptorValueChanged. + } else if (!read_error_callback.is_null()) { + read_error_callback.Run( + BluetoothRemoteGattServiceAndroid::GetGattErrorCode(status)); + } +} + +void BluetoothRemoteGattDescriptorAndroid::OnWrite( + JNIEnv* env, + const JavaParamRef<jobject>& jcaller, + int32_t status) { + write_pending_ = false; + + // Clear callbacks before calling to avoid reentrancy issues. + base::Closure write_callback = write_callback_; + ErrorCallback write_error_callback = write_error_callback_; + write_callback_.Reset(); + write_error_callback_.Reset(); + + if (status == 0 // android.bluetooth.BluetoothGatt.GATT_SUCCESS + && !write_callback.is_null()) { + write_callback.Run(); + // TODO(https://crbug.com/584369): Call GattDescriptorValueChanged. + } else if (!write_error_callback.is_null()) { + write_error_callback.Run( + BluetoothRemoteGattServiceAndroid::GetGattErrorCode(status)); + } } BluetoothRemoteGattDescriptorAndroid::BluetoothRemoteGattDescriptorAndroid( |