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 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 | 129 |
130 bool BluetoothRemoteGattCharacteristicAndroid::UpdateValue( | 130 bool BluetoothRemoteGattCharacteristicAndroid::UpdateValue( |
131 const std::vector<uint8_t>& value) { | 131 const std::vector<uint8_t>& value) { |
132 NOTIMPLEMENTED(); | 132 NOTIMPLEMENTED(); |
133 return false; | 133 return false; |
134 } | 134 } |
135 | 135 |
136 void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( | 136 void BluetoothRemoteGattCharacteristicAndroid::StartNotifySession( |
137 const NotifySessionCallback& callback, | 137 const NotifySessionCallback& callback, |
138 const ErrorCallback& error_callback) { | 138 const ErrorCallback& error_callback) { |
139 if (Java_ChromeBluetoothRemoteGattCharacteristic_startNotifySession( | 139 if (pending_start_notify_calls_.empty()) { |
140 AttachCurrentThread(), j_characteristic_.obj())) { | 140 if (!Java_ChromeBluetoothRemoteGattCharacteristic_startNotifySession( |
141 // TODO(crbug.com/569664): Wait until descriptor write completes before | 141 AttachCurrentThread(), j_characteristic_.obj())) { |
142 // reporting success via calling |callback|. | 142 base::MessageLoop::current()->PostTask( |
143 scoped_ptr<device::BluetoothGattNotifySession> notify_session( | 143 FROM_HERE, |
144 new BluetoothGattNotifySessionAndroid(instance_id_)); | 144 base::Bind(error_callback, |
145 base::MessageLoop::current()->PostTask( | 145 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); |
146 FROM_HERE, base::Bind(callback, base::Passed(¬ify_session))); | 146 return; |
147 } else { | 147 } |
148 base::MessageLoop::current()->PostTask( | |
149 FROM_HERE, | |
150 base::Bind(error_callback, | |
151 BluetoothRemoteGattServiceAndroid::GATT_ERROR_FAILED)); | |
152 } | 148 } |
149 | |
150 pending_start_notify_calls_.push(std::make_pair(callback, error_callback)); | |
153 } | 151 } |
154 | 152 |
155 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( | 153 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( |
156 const ValueCallback& callback, | 154 const ValueCallback& callback, |
157 const ErrorCallback& error_callback) { | 155 const ErrorCallback& error_callback) { |
158 if (read_pending_ || write_pending_) { | 156 if (read_pending_ || write_pending_) { |
159 base::MessageLoop::current()->PostTask( | 157 base::MessageLoop::current()->PostTask( |
160 FROM_HERE, base::Bind(error_callback, | 158 FROM_HERE, base::Bind(error_callback, |
161 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); | 159 BluetoothGattService::GATT_ERROR_IN_PROGRESS)); |
162 return; | 160 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 } | 202 } |
205 | 203 |
206 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( | 204 void BluetoothRemoteGattCharacteristicAndroid::OnChanged( |
207 JNIEnv* env, | 205 JNIEnv* env, |
208 const JavaParamRef<jobject>& jcaller, | 206 const JavaParamRef<jobject>& jcaller, |
209 const JavaParamRef<jbyteArray>& value) { | 207 const JavaParamRef<jbyteArray>& value) { |
210 base::android::JavaByteArrayToByteVector(env, value, &value_); | 208 base::android::JavaByteArrayToByteVector(env, value, &value_); |
211 adapter_->NotifyGattCharacteristicValueChanged(this, value_); | 209 adapter_->NotifyGattCharacteristicValueChanged(this, value_); |
212 } | 210 } |
213 | 211 |
212 void BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySession( | |
213 JNIEnv* env, | |
214 const JavaParamRef<jobject>& jcaller, | |
215 int32_t status) { | |
216 while (!pending_start_notify_calls_.empty()) { | |
217 PendingStartNotifyCall callbacks = pending_start_notify_calls_.front(); | |
218 pending_start_notify_calls_.pop(); | |
219 if (status == 0) { | |
ortuno
2016/02/19 20:51:02
Comment what 0 means
tommyt
2016/02/22 08:59:56
Done.
| |
220 scoped_ptr<device::BluetoothGattNotifySession> notify_session( | |
221 new BluetoothGattNotifySessionAndroid(instance_id_)); | |
222 callbacks.first.Run(std::move(notify_session)); | |
223 } else { | |
224 callbacks.second.Run( | |
225 BluetoothRemoteGattServiceAndroid::GetGattErrorCode(status)); | |
226 } | |
227 } | |
228 } | |
229 | |
214 void BluetoothRemoteGattCharacteristicAndroid::OnRead( | 230 void BluetoothRemoteGattCharacteristicAndroid::OnRead( |
215 JNIEnv* env, | 231 JNIEnv* env, |
216 const JavaParamRef<jobject>& jcaller, | 232 const JavaParamRef<jobject>& jcaller, |
217 int32_t status, | 233 int32_t status, |
218 const JavaParamRef<jbyteArray>& value) { | 234 const JavaParamRef<jbyteArray>& value) { |
219 read_pending_ = false; | 235 read_pending_ = false; |
220 | 236 |
221 // Clear callbacks before calling to avoid reentrancy issues. | 237 // Clear callbacks before calling to avoid reentrancy issues. |
222 ValueCallback read_callback = read_callback_; | 238 ValueCallback read_callback = read_callback_; |
223 ErrorCallback read_error_callback = read_error_callback_; | 239 ErrorCallback read_error_callback = read_error_callback_; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 302 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
287 const { | 303 const { |
288 if (!descriptors_.empty()) | 304 if (!descriptors_.empty()) |
289 return; | 305 return; |
290 | 306 |
291 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 307 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
292 AttachCurrentThread(), j_characteristic_.obj()); | 308 AttachCurrentThread(), j_characteristic_.obj()); |
293 } | 309 } |
294 | 310 |
295 } // namespace device | 311 } // namespace device |
OLD | NEW |