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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/location.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" |
15 #include "device/bluetooth/bluetooth_adapter_android.h" | 17 #include "device/bluetooth/bluetooth_adapter_android.h" |
16 #include "device/bluetooth/bluetooth_gatt_notify_session_android.h" | 18 #include "device/bluetooth/bluetooth_gatt_notify_session_android.h" |
17 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_android.h" | 19 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_android.h" |
18 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" | 20 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" |
19 #include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" | 21 #include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" |
20 | 22 |
21 using base::android::AttachCurrentThread; | 23 using base::android::AttachCurrentThread; |
22 | 24 |
23 namespace device { | 25 namespace device { |
24 | 26 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 return; | 135 return; |
134 } | 136 } |
135 | 137 |
136 Properties properties = GetProperties(); | 138 Properties properties = GetProperties(); |
137 | 139 |
138 bool hasNotify = properties & PROPERTY_NOTIFY; | 140 bool hasNotify = properties & PROPERTY_NOTIFY; |
139 bool hasIndicate = properties & PROPERTY_INDICATE; | 141 bool hasIndicate = properties & PROPERTY_INDICATE; |
140 | 142 |
141 if (!hasNotify && !hasIndicate) { | 143 if (!hasNotify && !hasIndicate) { |
142 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; | 144 LOG(ERROR) << "Characteristic needs NOTIFY or INDICATE"; |
143 base::MessageLoop::current()->PostTask( | 145 base::ThreadTaskRunnerHandle::Get()->PostTask( |
144 FROM_HERE, | 146 FROM_HERE, |
145 base::Bind(error_callback, | 147 base::Bind(error_callback, |
146 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); | 148 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
147 return; | 149 return; |
148 } | 150 } |
149 | 151 |
150 std::vector<BluetoothRemoteGattDescriptor*> ccc_descriptor = | 152 std::vector<BluetoothRemoteGattDescriptor*> ccc_descriptor = |
151 GetDescriptorsByUUID(BluetoothRemoteGattDescriptor:: | 153 GetDescriptorsByUUID(BluetoothRemoteGattDescriptor:: |
152 ClientCharacteristicConfigurationUuid()); | 154 ClientCharacteristicConfigurationUuid()); |
153 | 155 |
154 if (ccc_descriptor.size() != 1u) { | 156 if (ccc_descriptor.size() != 1u) { |
155 LOG(ERROR) << "Found " << ccc_descriptor.size() | 157 LOG(ERROR) << "Found " << ccc_descriptor.size() |
156 << " client characteristic configuration descriptors."; | 158 << " client characteristic configuration descriptors."; |
157 base::MessageLoop::current()->PostTask( | 159 base::ThreadTaskRunnerHandle::Get()->PostTask( |
158 FROM_HERE, | 160 FROM_HERE, |
159 base::Bind(error_callback, | 161 base::Bind(error_callback, |
160 (ccc_descriptor.size() == 0) | 162 (ccc_descriptor.size() == 0) |
161 ? BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED | 163 ? BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED |
162 : BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 164 : BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
163 return; | 165 return; |
164 } | 166 } |
165 | 167 |
166 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( | 168 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( |
167 AttachCurrentThread(), j_characteristic_.obj(), true)) { | 169 AttachCurrentThread(), j_characteristic_.obj(), true)) { |
168 LOG(ERROR) << "Error enabling characteristic notification"; | 170 LOG(ERROR) << "Error enabling characteristic notification"; |
169 base::MessageLoop::current()->PostTask( | 171 base::ThreadTaskRunnerHandle::Get()->PostTask( |
170 FROM_HERE, base::Bind(error_callback, | 172 FROM_HERE, base::Bind(error_callback, |
171 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 173 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
172 return; | 174 return; |
173 } | 175 } |
174 | 176 |
175 std::vector<uint8_t> value(2); | 177 std::vector<uint8_t> value(2); |
176 value[0] = hasNotify ? 1 : 2; | 178 value[0] = hasNotify ? 1 : 2; |
177 | 179 |
178 pending_start_notify_calls_.push_back( | 180 pending_start_notify_calls_.push_back( |
179 std::make_pair(callback, error_callback)); | 181 std::make_pair(callback, error_callback)); |
180 ccc_descriptor[0]->WriteRemoteDescriptor( | 182 ccc_descriptor[0]->WriteRemoteDescriptor( |
181 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: | 183 value, base::Bind(&BluetoothRemoteGattCharacteristicAndroid:: |
182 OnStartNotifySessionSuccess, | 184 OnStartNotifySessionSuccess, |
183 base::Unretained(this)), | 185 base::Unretained(this)), |
184 base::Bind( | 186 base::Bind( |
185 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, | 187 &BluetoothRemoteGattCharacteristicAndroid::OnStartNotifySessionError, |
186 base::Unretained(this))); | 188 base::Unretained(this))); |
187 } | 189 } |
188 | 190 |
189 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( | 191 void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( |
190 const ValueCallback& callback, | 192 const ValueCallback& callback, |
191 const ErrorCallback& error_callback) { | 193 const ErrorCallback& error_callback) { |
192 if (read_pending_ || write_pending_) { | 194 if (read_pending_ || write_pending_) { |
193 base::MessageLoop::current()->PostTask( | 195 base::ThreadTaskRunnerHandle::Get()->PostTask( |
194 FROM_HERE, | 196 FROM_HERE, |
195 base::Bind(error_callback, | 197 base::Bind(error_callback, |
196 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | 198 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
197 return; | 199 return; |
198 } | 200 } |
199 | 201 |
200 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( | 202 if (!Java_ChromeBluetoothRemoteGattCharacteristic_readRemoteCharacteristic( |
201 AttachCurrentThread(), j_characteristic_.obj())) { | 203 AttachCurrentThread(), j_characteristic_.obj())) { |
202 base::MessageLoop::current()->PostTask( | 204 base::ThreadTaskRunnerHandle::Get()->PostTask( |
203 FROM_HERE, base::Bind(error_callback, | 205 FROM_HERE, base::Bind(error_callback, |
204 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 206 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
205 return; | 207 return; |
206 } | 208 } |
207 | 209 |
208 read_pending_ = true; | 210 read_pending_ = true; |
209 read_callback_ = callback; | 211 read_callback_ = callback; |
210 read_error_callback_ = error_callback; | 212 read_error_callback_ = error_callback; |
211 } | 213 } |
212 | 214 |
213 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( | 215 void BluetoothRemoteGattCharacteristicAndroid::WriteRemoteCharacteristic( |
214 const std::vector<uint8_t>& new_value, | 216 const std::vector<uint8_t>& new_value, |
215 const base::Closure& callback, | 217 const base::Closure& callback, |
216 const ErrorCallback& error_callback) { | 218 const ErrorCallback& error_callback) { |
217 if (read_pending_ || write_pending_) { | 219 if (read_pending_ || write_pending_) { |
218 base::MessageLoop::current()->PostTask( | 220 base::ThreadTaskRunnerHandle::Get()->PostTask( |
219 FROM_HERE, | 221 FROM_HERE, |
220 base::Bind(error_callback, | 222 base::Bind(error_callback, |
221 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | 223 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
222 return; | 224 return; |
223 } | 225 } |
224 | 226 |
225 JNIEnv* env = AttachCurrentThread(); | 227 JNIEnv* env = AttachCurrentThread(); |
226 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( | 228 if (!Java_ChromeBluetoothRemoteGattCharacteristic_writeRemoteCharacteristic( |
227 env, j_characteristic_.obj(), | 229 env, j_characteristic_.obj(), |
228 base::android::ToJavaByteArray(env, new_value).obj())) { | 230 base::android::ToJavaByteArray(env, new_value).obj())) { |
229 base::MessageLoop::current()->PostTask( | 231 base::ThreadTaskRunnerHandle::Get()->PostTask( |
230 FROM_HERE, base::Bind(error_callback, | 232 FROM_HERE, base::Bind(error_callback, |
231 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 233 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
232 return; | 234 return; |
233 } | 235 } |
234 | 236 |
235 write_pending_ = true; | 237 write_pending_ = true; |
236 write_callback_ = callback; | 238 write_callback_ = callback; |
237 write_error_callback_ = error_callback; | 239 write_error_callback_ = error_callback; |
238 } | 240 } |
239 | 241 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 343 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
342 const { | 344 const { |
343 if (!descriptors_.empty()) | 345 if (!descriptors_.empty()) |
344 return; | 346 return; |
345 | 347 |
346 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 348 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
347 AttachCurrentThread(), j_characteristic_.obj()); | 349 AttachCurrentThread(), j_characteristic_.obj()); |
348 } | 350 } |
349 | 351 |
350 } // namespace device | 352 } // namespace device |
OLD | NEW |