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/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "device/bluetooth/bluetooth_adapter_android.h" | 17 #include "device/bluetooth/bluetooth_adapter_android.h" |
18 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_android.h" | 18 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_android.h" |
19 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" | 19 #include "device/bluetooth/bluetooth_remote_gatt_service_android.h" |
20 #include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" | 20 #include "jni/ChromeBluetoothRemoteGattCharacteristic_jni.h" |
21 | 21 |
22 using base::android::AttachCurrentThread; | 22 using base::android::AttachCurrentThread; |
23 using base::android::JavaParamRef; | 23 using base::android::JavaParamRef; |
| 24 using base::android::JavaRef; |
24 | 25 |
25 namespace device { | 26 namespace device { |
26 | 27 |
27 // static | 28 // static |
28 std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid> | 29 std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid> |
29 BluetoothRemoteGattCharacteristicAndroid::Create( | 30 BluetoothRemoteGattCharacteristicAndroid::Create( |
30 BluetoothAdapterAndroid* adapter, | 31 BluetoothAdapterAndroid* adapter, |
31 BluetoothRemoteGattServiceAndroid* service, | 32 BluetoothRemoteGattServiceAndroid* service, |
32 const std::string& instance_id, | 33 const std::string& instance_id, |
33 jobject /* BluetoothGattCharacteristicWrapper */ | 34 const JavaRef<jobject>& /* BluetoothGattCharacteristicWrapper */ |
34 bluetooth_gatt_characteristic_wrapper, | 35 bluetooth_gatt_characteristic_wrapper, |
35 jobject /* ChromeBluetoothDevice */ chrome_bluetooth_device) { | 36 const JavaRef< |
| 37 jobject>& /* ChromeBluetoothDevice */ chrome_bluetooth_device) { |
36 std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid> characteristic( | 38 std::unique_ptr<BluetoothRemoteGattCharacteristicAndroid> characteristic( |
37 new BluetoothRemoteGattCharacteristicAndroid(adapter, service, | 39 new BluetoothRemoteGattCharacteristicAndroid(adapter, service, |
38 instance_id)); | 40 instance_id)); |
39 | 41 |
40 JNIEnv* env = AttachCurrentThread(); | 42 JNIEnv* env = AttachCurrentThread(); |
41 characteristic->j_characteristic_.Reset( | 43 characteristic->j_characteristic_.Reset( |
42 Java_ChromeBluetoothRemoteGattCharacteristic_create( | 44 Java_ChromeBluetoothRemoteGattCharacteristic_create( |
43 env, reinterpret_cast<intptr_t>(characteristic.get()), | 45 env, reinterpret_cast<intptr_t>(characteristic.get()), |
44 bluetooth_gatt_characteristic_wrapper, | 46 bluetooth_gatt_characteristic_wrapper, |
45 base::android::ConvertUTF8ToJavaString(env, instance_id), | 47 base::android::ConvertUTF8ToJavaString(env, instance_id), |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 BluetoothRemoteGattDescriptorAndroid::Create( | 238 BluetoothRemoteGattDescriptorAndroid::Create( |
237 instanceIdString, bluetooth_gatt_descriptor_wrapper, | 239 instanceIdString, bluetooth_gatt_descriptor_wrapper, |
238 chrome_bluetooth_device)); | 240 chrome_bluetooth_device)); |
239 } | 241 } |
240 | 242 |
241 void BluetoothRemoteGattCharacteristicAndroid::SubscribeToNotifications( | 243 void BluetoothRemoteGattCharacteristicAndroid::SubscribeToNotifications( |
242 BluetoothRemoteGattDescriptor* ccc_descriptor, | 244 BluetoothRemoteGattDescriptor* ccc_descriptor, |
243 const base::Closure& callback, | 245 const base::Closure& callback, |
244 const ErrorCallback& error_callback) { | 246 const ErrorCallback& error_callback) { |
245 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( | 247 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( |
246 AttachCurrentThread(), j_characteristic_.obj(), true)) { | 248 AttachCurrentThread(), j_characteristic_, true)) { |
247 LOG(ERROR) << "Error enabling characteristic notification"; | 249 LOG(ERROR) << "Error enabling characteristic notification"; |
248 base::ThreadTaskRunnerHandle::Get()->PostTask( | 250 base::ThreadTaskRunnerHandle::Get()->PostTask( |
249 FROM_HERE, base::Bind(error_callback, | 251 FROM_HERE, base::Bind(error_callback, |
250 BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 252 BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
251 return; | 253 return; |
252 } | 254 } |
253 | 255 |
254 bool hasNotify = GetProperties() & PROPERTY_NOTIFY; | 256 bool hasNotify = GetProperties() & PROPERTY_NOTIFY; |
255 std::vector<uint8_t> value(2); | 257 std::vector<uint8_t> value(2); |
256 value[0] = hasNotify ? 1 : 2; | 258 value[0] = hasNotify ? 1 : 2; |
257 | 259 |
258 ccc_descriptor->WriteRemoteDescriptor(value, callback, error_callback); | 260 ccc_descriptor->WriteRemoteDescriptor(value, callback, error_callback); |
259 } | 261 } |
260 | 262 |
261 void BluetoothRemoteGattCharacteristicAndroid::UnsubscribeFromNotifications( | 263 void BluetoothRemoteGattCharacteristicAndroid::UnsubscribeFromNotifications( |
262 BluetoothRemoteGattDescriptor* ccc_descriptor, | 264 BluetoothRemoteGattDescriptor* ccc_descriptor, |
263 const base::Closure& callback, | 265 const base::Closure& callback, |
264 const ErrorCallback& error_callback) { | 266 const ErrorCallback& error_callback) { |
265 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( | 267 if (!Java_ChromeBluetoothRemoteGattCharacteristic_setCharacteristicNotificatio
n( |
266 AttachCurrentThread(), j_characteristic_.obj(), false)) { | 268 AttachCurrentThread(), j_characteristic_, false)) { |
267 LOG(ERROR) << "Error disabling characteristic notification"; | 269 LOG(ERROR) << "Error disabling characteristic notification"; |
268 base::ThreadTaskRunnerHandle::Get()->PostTask( | 270 base::ThreadTaskRunnerHandle::Get()->PostTask( |
269 FROM_HERE, | 271 FROM_HERE, |
270 base::Bind(error_callback, | 272 base::Bind(error_callback, |
271 device::BluetoothRemoteGattService::GATT_ERROR_FAILED)); | 273 device::BluetoothRemoteGattService::GATT_ERROR_FAILED)); |
272 return; | 274 return; |
273 } | 275 } |
274 | 276 |
275 std::vector<uint8_t> value(2); | 277 std::vector<uint8_t> value(2); |
276 value[0] = 0; | 278 value[0] = 0; |
(...skipping 11 matching lines...) Expand all Loading... |
288 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() | 290 void BluetoothRemoteGattCharacteristicAndroid::EnsureDescriptorsCreated() |
289 const { | 291 const { |
290 if (!descriptors_.empty()) | 292 if (!descriptors_.empty()) |
291 return; | 293 return; |
292 | 294 |
293 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( | 295 Java_ChromeBluetoothRemoteGattCharacteristic_createDescriptors( |
294 AttachCurrentThread(), j_characteristic_); | 296 AttachCurrentThread(), j_characteristic_); |
295 } | 297 } |
296 | 298 |
297 } // namespace device | 299 } // namespace device |
OLD | NEW |